-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ajgarlag/feature/require-psr17
Add compiler pass class to ensure that required PSR-17 factories are defined
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/DependencyInjection/Compiler/AssertDefinedPsr17Factories.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |