|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Setono\DAOBundle\DependencyInjection\Compiler; |
|
6
|
|
|
|
|
7
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
8
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
12
|
|
|
|
|
13
|
|
|
final class RegisterFactoriesPass implements CompilerPassInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private const PSR17_FACTORY_SERVICE_ID = 'setono_dao.psr17_factory'; |
|
16
|
|
|
|
|
17
|
|
|
public function process(ContainerBuilder $container): void |
|
18
|
|
|
{ |
|
19
|
|
|
if (class_exists(Psr17Factory::class)) { |
|
20
|
|
|
// this service is used later if the Psr17Factory exists. Else it will be automatically removed by Symfony |
|
21
|
|
|
$container->register(self::PSR17_FACTORY_SERVICE_ID, Psr17Factory::class); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$this->registerFactory($container, 'setono_dao.request_factory', 'setono_dao.request_factory', RequestFactoryInterface::class); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
private function registerFactory(ContainerBuilder $container, string $parameter, string $service, string $factoryInterface): void |
|
28
|
|
|
{ |
|
29
|
|
|
if ($container->hasParameter($parameter)) { |
|
30
|
|
|
if (!$container->has($container->getParameter($parameter))) { |
|
|
|
|
|
|
31
|
|
|
throw new ServiceNotFoundException($container->getParameter($parameter)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$container->setAlias($service, $container->getParameter($parameter)); |
|
|
|
|
|
|
35
|
|
|
} elseif ($container->has($factoryInterface)) { |
|
36
|
|
|
$container->setAlias($service, $factoryInterface); |
|
37
|
|
|
} elseif ($container->has('nyholm.psr7.psr17_factory')) { |
|
38
|
|
|
$container->setAlias($service, 'nyholm.psr7.psr17_factory'); |
|
39
|
|
|
} elseif (class_exists(Psr17Factory::class)) { |
|
40
|
|
|
$container->setAlias($service, self::PSR17_FACTORY_SERVICE_ID); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|