From 3553f2d6d390a8cc0d6b1fbeec078c7741d4884c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20J=2E=20Garc=C3=ADa=20Lagar?= Date: Wed, 3 Feb 2021 13:42:47 +0100 Subject: [PATCH] Define autowiring aliases for PSR-17 conditionally --- .../AjgarlagPsrHttpMessageExtension.php | 32 ++++++++++++++++++- src/Resources/config/psr7.xml | 7 ---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/DependencyInjection/AjgarlagPsrHttpMessageExtension.php b/src/DependencyInjection/AjgarlagPsrHttpMessageExtension.php index b11ffda..9e63104 100644 --- a/src/DependencyInjection/AjgarlagPsrHttpMessageExtension.php +++ b/src/DependencyInjection/AjgarlagPsrHttpMessageExtension.php @@ -12,12 +12,18 @@ namespace Ajgarlag\Bundle\PsrHttpMessageBundle\DependencyInjection; +use Nyholm\Psr7\Factory\Psr17Factory; +use Psr\Http\Message\ResponseFactoryInterface; +use Psr\Http\Message\ServerRequestFactoryInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UploadedFileFactoryInterface; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; -class AjgarlagPsrHttpMessageExtension extends Extension +class AjgarlagPsrHttpMessageExtension extends Extension implements CompilerPassInterface { public function load(array $configs, ContainerBuilder $container) { @@ -32,4 +38,28 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('alias_sensio_framework_extra.xml'); } } + + public function process(ContainerBuilder $container) + { + $requiredInterfaces = [ + ResponseFactoryInterface::class, + ServerRequestFactoryInterface::class, + StreamFactoryInterface::class, + UploadedFileFactoryInterface::class, + ]; + + $nyholmPsr17Id = 'nyholm.psr7.psr17_factory'; + + if (class_exists(Psr17Factory::class) && !$container->has($nyholmPsr17Id)) { + $container->register($nyholmPsr17Id, Psr17Factory::class); + } + + if ($container->has($nyholmPsr17Id)) { + foreach ($requiredInterfaces as $requiredInterface) { + if (!$container->has($requiredInterface)) { + $container->setAlias($requiredInterface, $nyholmPsr17Id); + } + } + } + } } diff --git a/src/Resources/config/psr7.xml b/src/Resources/config/psr7.xml index 6228b8c..4e2d1af 100644 --- a/src/Resources/config/psr7.xml +++ b/src/Resources/config/psr7.xml @@ -25,12 +25,5 @@ - - - - - - -