Skip to content

Commit

Permalink
Define autowiring aliases for PSR-17 conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgarlag committed Feb 3, 2021
1 parent e4894ab commit 3553f2d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
32 changes: 31 additions & 1 deletion src/DependencyInjection/AjgarlagPsrHttpMessageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
}
}
}
}
7 changes: 0 additions & 7 deletions src/Resources/config/psr7.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,5 @@
<argument type="service" id="ajgarlag_psr_http_message.psr7.http_foundation_factory" />
<tag name="kernel.event_subscriber" />
</service>

<!-- provide default aliases for PSR-17 based on nyholm/psr7 -->
<service id="nyholm.psr7.psr17_factory" class="Nyholm\Psr7\Factory\Psr17Factory" />
<service id="Psr\Http\Message\ServerRequestFactoryInterface" alias="nyholm.psr7.psr17_factory" />
<service id="Psr\Http\Message\StreamFactoryInterface" alias="nyholm.psr7.psr17_factory" />
<service id="Psr\Http\Message\UploadedFileFactoryInterface" alias="nyholm.psr7.psr17_factory" />
<service id="Psr\Http\Message\ResponseFactoryInterface" alias="nyholm.psr7.psr17_factory" />
</services>
</container>

0 comments on commit 3553f2d

Please sign in to comment.