1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the symfonette/class-named-services package. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Symfonette\ClassNamedServices; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
|
20
|
|
|
class RegisterClassNamedServicesPass implements CompilerPassInterface |
21
|
|
|
{ |
22
|
|
|
private $container; |
23
|
|
|
private $types; |
24
|
|
|
private $definedTypes; |
25
|
|
|
private $reflectionClasses = []; |
26
|
|
|
|
27
|
|
|
public function process(ContainerBuilder $container) |
28
|
|
|
{ |
29
|
6 |
|
$throwingAutoloader = function ($class) { |
30
|
|
|
throw new \ReflectionException(sprintf('Class %s does not exist', $class)); |
31
|
6 |
|
}; |
32
|
6 |
|
spl_autoload_register($throwingAutoloader); |
33
|
|
|
|
34
|
|
|
try { |
35
|
6 |
|
$this->container = $container; |
36
|
6 |
|
foreach ($container->getDefinitions() as $id => $definition) { |
37
|
6 |
|
$this->populateAvailableTypes($id, $definition); |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
$this->populateAvailableTypes('service_container', new Definition(get_class($container))); |
41
|
|
|
|
42
|
6 |
|
foreach ($this->types as $type => $services) { |
43
|
6 |
|
$this->registerService($type, $services); |
44
|
|
|
} |
45
|
|
|
} catch (\Exception $e) { |
|
|
|
|
46
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
spl_autoload_unregister($throwingAutoloader); |
50
|
|
|
|
51
|
|
|
// Free memory and remove circular reference to container |
52
|
6 |
|
$this->container = null; |
53
|
6 |
|
$this->reflectionClasses = []; |
54
|
6 |
|
$this->definedTypes = []; |
55
|
6 |
|
$this->types = null; |
56
|
|
|
|
57
|
6 |
|
if (isset($e)) { |
58
|
|
|
throw $e; |
59
|
|
|
} |
60
|
6 |
|
} |
61
|
|
|
|
62
|
6 |
|
private function populateAvailableTypes($id, Definition $definition) |
63
|
|
|
{ |
64
|
6 |
|
if ($definition->isAbstract()) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
foreach ($definition->getAutowiringTypes() as $type) { |
69
|
|
|
$this->definedTypes[$type] = true; |
70
|
|
|
$this->types[$type][] = $id; |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
foreach ($reflectionClass->getInterfaces() as $reflectionInterface) { |
78
|
6 |
|
$this->set($reflectionInterface->name, $id); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
do { |
82
|
6 |
|
$this->set($reflectionClass->name, $id); |
83
|
6 |
|
} while ($reflectionClass = $reflectionClass->getParentClass()); |
84
|
6 |
|
} |
85
|
|
|
|
86
|
6 |
|
private function getReflectionClass($id, Definition $definition) |
87
|
|
|
{ |
88
|
6 |
|
if (isset($this->reflectionClasses[$id])) { |
89
|
|
|
return $this->reflectionClasses[$id]; |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
if (!$class = $definition->getClass()) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
$class = $this->container->getParameterBag()->resolveValue($class); |
97
|
|
|
|
98
|
|
|
try { |
99
|
6 |
|
$reflector = new \ReflectionClass($class); |
100
|
|
|
} catch (\ReflectionException $e) { |
101
|
|
|
$reflector = false; |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
return $this->reflectionClasses[$id] = $reflector; |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
private function set($type, $id) |
108
|
|
|
{ |
109
|
6 |
|
if (isset($this->definedTypes[$type])) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
if (strtolower($type) === strtolower($id)) { |
114
|
1 |
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
6 |
|
$this->types[$type][] = $id; |
118
|
6 |
|
} |
119
|
|
|
|
120
|
6 |
|
private function registerService($type, array $services) |
121
|
|
|
{ |
122
|
6 |
|
if (1 === count($services)) { |
123
|
6 |
|
$service = reset($services); |
124
|
6 |
|
$isPublic = 'service_container' === $service ? true : $this->container->getDefinition($service)->isPublic(); |
125
|
6 |
|
$this->container->setAlias($type, new Alias($service, $isPublic)); |
126
|
|
|
} else { |
127
|
2 |
|
$this->container->setDefinition($type, new AmbiguousDefinition($type, $services)); |
128
|
|
|
} |
129
|
6 |
|
} |
130
|
|
|
} |
131
|
|
|
|