|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Symplify |
|
5
|
|
|
* Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz). |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Symplify\ControllerAutowire\DependencyInjection\Compiler; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
15
|
|
|
use Symplify\ControllerAutowire\Contract\DependencyInjection\ControllerClassMapInterface; |
|
16
|
|
|
use Symplify\ControllerAutowire\HttpKernel\Controller\ControllerResolver; |
|
17
|
|
|
|
|
18
|
|
|
final class ReplaceControllerResolverPass implements CompilerPassInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
const CONTROLLER_RESOLVER_SERVICE_NAME = 'controller_resolver'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ControllerClassMapInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $controllerClassMap; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $isControllerResolverAliased = false; |
|
34
|
|
|
|
|
35
|
2 |
|
public function __construct(ControllerClassMapInterface $controllerClassMap) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->controllerClassMap = $controllerClassMap; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function process(ContainerBuilder $containerBuilder) |
|
44
|
|
|
{ |
|
45
|
2 |
|
$controllerResolverServiceName = $this->getCurrentControllerResolverServiceName($containerBuilder); |
|
46
|
|
|
|
|
47
|
2 |
|
if ($this->isControllerResolverAliased) { |
|
48
|
2 |
|
$definition = $this->createDefinitionWithDecoratingResolver($controllerResolverServiceName); |
|
49
|
|
|
|
|
50
|
2 |
|
$containerBuilder->setDefinition('default.controller_resolver', $definition); |
|
51
|
|
|
} else { |
|
52
|
|
|
$oldResolver = $containerBuilder->getDefinition($controllerResolverServiceName); |
|
53
|
|
|
$containerBuilder->setDefinition('old.'.$controllerResolverServiceName, $oldResolver); |
|
54
|
|
|
$containerBuilder->removeDefinition($controllerResolverServiceName); |
|
55
|
|
|
|
|
56
|
|
|
$definition = $this->createDefinitionWithDecoratingResolver('old.'.$controllerResolverServiceName); |
|
57
|
|
|
|
|
58
|
|
|
$containerBuilder->setDefinition('symplify.autowire_controller_controller_resolver', $definition); |
|
59
|
|
|
$containerBuilder->setAlias( |
|
60
|
|
|
$controllerResolverServiceName, |
|
61
|
|
|
new Alias('symplify.autowire_controller_controller_resolver', true) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
private function getCurrentControllerResolverServiceName(ContainerBuilder $containerBuilder) : string |
|
67
|
|
|
{ |
|
68
|
2 |
|
if ($containerBuilder->hasAlias(self::CONTROLLER_RESOLVER_SERVICE_NAME)) { |
|
69
|
2 |
|
$this->isControllerResolverAliased = true; |
|
70
|
2 |
|
$alias = $containerBuilder->getAlias(self::CONTROLLER_RESOLVER_SERVICE_NAME); |
|
71
|
|
|
|
|
72
|
2 |
|
return (string) $alias; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return self::CONTROLLER_RESOLVER_SERVICE_NAME; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
private function createDefinitionWithDecoratingResolver(string $controllerResolverServiceName) : Definition |
|
79
|
|
|
{ |
|
80
|
2 |
|
$definition = new Definition(ControllerResolver::class, [ |
|
81
|
2 |
|
new Reference($controllerResolverServiceName), |
|
82
|
2 |
|
new Reference('service_container'), |
|
83
|
2 |
|
new Reference('controller_name_converter'), |
|
84
|
|
|
]); |
|
85
|
2 |
|
$definition->addMethodCall('setControllerClassMap', [$this->controllerClassMap->getControllers()]); |
|
86
|
|
|
|
|
87
|
2 |
|
return $definition; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|