Skip to content

Commit

Permalink
Merge pull request #10 from ajgarlag/feature/require-psr17
Browse files Browse the repository at this point in the history
Add compiler pass class to ensure that required PSR-17 factories are defined
  • Loading branch information
ajgarlag authored Mar 10, 2021
2 parents fd166c7 + 872a343 commit 7f7e82f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased](https://github.com/ajgarlag/psr-http-message-bundle/compare/1.2.0...main)

### Added
- Add compiler pass class to ensure that required PSR-17 factories are defined

## [1.2.0](https://github.com/ajgarlag/psr-http-message-bundle/compare/1.1.2...1.2.0) - 2021-02-19

### Added
Expand Down
2 changes: 2 additions & 0 deletions src/AjgarlagPsrHttpMessageBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Ajgarlag\Bundle\PsrHttpMessageBundle;

use Ajgarlag\Bundle\PsrHttpMessageBundle\DependencyInjection\Compiler\AssertDefinedPsr17Factories;
use Ajgarlag\Bundle\PsrHttpMessageBundle\DependencyInjection\Compiler\RegisterHttpMessageFactoriesPass;
use Ajgarlag\Bundle\PsrHttpMessageBundle\DependencyInjection\Compiler\RegisterNyholmPsr17FactoriesPass;
use Ajgarlag\Bundle\PsrHttpMessageBundle\DependencyInjection\Compiler\TagArgumentValueResolverPass;
Expand All @@ -29,6 +30,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new TagArgumentValueResolverPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
$container->addCompilerPass(new RegisterHttpMessageFactoriesPass());
$container->addCompilerPass(new RegisterNyholmPsr17FactoriesPass());
$container->addCompilerPass(new AssertDefinedPsr17Factories());
$container->addCompilerPass(new TagViewEventListenerPass());
}
}
40 changes: 40 additions & 0 deletions src/DependencyInjection/Compiler/AssertDefinedPsr17Factories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* PsrHttpMessageBundle by @ajgarlag
*
* Copyright (c) 2010-2021 Fabien Potencier
* Copyright (c) 2021 Antonio J. García Lagar
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ajgarlag\Bundle\PsrHttpMessageBundle\DependencyInjection\Compiler;

use LogicException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class AssertDefinedPsr17Factories implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$requiredInterfaces = [
ResponseFactoryInterface::class,
ServerRequestFactoryInterface::class,
StreamFactoryInterface::class,
UploadedFileFactoryInterface::class,
];

foreach ($requiredInterfaces as $requiredInterface) {
if (!$container->has($requiredInterface)) {
throw new LogicException(sprintf('Service for PSR-17 factory "%s" not found. Run "composer require nyholm/psr7" to install the recommended implementation.', $requiredInterface));
}
}
}
}

0 comments on commit 7f7e82f

Please sign in to comment.