|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Symplify |
|
7
|
|
|
* Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz). |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Symplify\ControllerAutowire\DependencyInjection\Compiler; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
|
17
|
|
|
use Symplify\ControllerAutowire\Contract\DependencyInjection\ControllerClassMapInterface; |
|
18
|
|
|
use Symplify\ControllerAutowire\HttpKernel\Controller\ControllerResolver; |
|
19
|
|
|
|
|
20
|
|
|
final class DecorateControllerResolverPass implements CompilerPassInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
const CONTROLLER_RESOLVER_SERVICE_NAME = 'controller_resolver'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ControllerClassMapInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $controllerClassMap; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(ControllerClassMapInterface $controllerClassMap) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->controllerClassMap = $controllerClassMap; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function process(ContainerBuilder $containerBuilder) |
|
38
|
|
|
{ |
|
39
|
|
|
$controllerResolverServiceName = $this->getCurrentControllerResolverServiceName($containerBuilder); |
|
40
|
|
|
|
|
41
|
|
|
$definition = new Definition(ControllerResolver::class, [ |
|
42
|
|
|
new Reference($controllerResolverServiceName . '.inner'), |
|
43
|
|
|
new Reference('service_container'), |
|
44
|
|
|
new Reference('controller_name_converter'), |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$definition->setDecoratedService($controllerResolverServiceName, null, 1); |
|
48
|
|
|
$definition->addMethodCall('setControllerClassMap', [$this->controllerClassMap->getControllers()]); |
|
49
|
|
|
$definition->setAutowiringTypes([ControllerResolverInterface::class]); |
|
50
|
|
|
|
|
51
|
|
|
$containerBuilder->setDefinition('symplify.controller_resolver', $definition); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function getCurrentControllerResolverServiceName(ContainerBuilder $containerBuilder) : string |
|
55
|
|
|
{ |
|
56
|
|
|
if ($containerBuilder->has('debug.' . self::CONTROLLER_RESOLVER_SERVICE_NAME)) { |
|
57
|
|
|
return 'debug.' . self::CONTROLLER_RESOLVER_SERVICE_NAME; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return self::CONTROLLER_RESOLVER_SERVICE_NAME; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|