|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the TraitorBundle, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2016 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\Bundle\Traitor\DependencyInjection; |
|
11
|
|
|
|
|
12
|
|
|
use RunOpenCode\Bundle\Traitor\Utils\ClassUtils; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
17
|
|
|
|
|
18
|
|
|
class CompilerPass implements CompilerPassInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
8 |
|
public function process(ContainerBuilder $container) |
|
24
|
|
|
{ |
|
25
|
8 |
|
if (!$container->hasParameter('roc.traitor.injection_map') || count($container->getParameter('roc.traitor.injection_map')) === 0) { |
|
26
|
2 |
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
6 |
|
if ($container->hasParameter('roc.traitor.filter.tags') || $container->hasParameter('roc.traitor.filter.namespaces')) { |
|
30
|
|
|
|
|
31
|
4 |
|
$definitions = array_merge( |
|
32
|
4 |
|
$container->hasParameter('roc.traitor.filter.tags') ? $this->getDefinitionsFromTags($container, $container->getParameter('roc.traitor.filter.tags')) : array(), |
|
33
|
4 |
|
$container->hasParameter('roc.traitor.filter.namespaces') ? $this->getDefinitionsFromClassNamespaces($container, $container->getParameter('roc.traitor.filter.namespaces')) : array() |
|
34
|
2 |
|
); |
|
35
|
|
|
|
|
36
|
2 |
|
} else { |
|
37
|
|
|
|
|
38
|
2 |
|
$definitions = $this->getDefinitionsFromClassNamespaces($container, array()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
6 |
|
$definitions = $this->filterExcludedDefinitions($container, $definitions); |
|
42
|
6 |
|
|
|
43
|
|
|
$this->processInjection($definitions, $container->getParameter('roc.traitor.injection_map')); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get definitions from container based on namespace filter |
|
48
|
|
|
* |
|
49
|
|
|
* @param ContainerBuilder $container |
|
50
|
|
|
* @param array $filters Namespace prefixes |
|
51
|
4 |
|
* @return Definition[] Definitions indexed by service ID |
|
52
|
|
|
*/ |
|
53
|
4 |
|
protected function getDefinitionsFromClassNamespaces(ContainerBuilder $container, array $filters) |
|
54
|
|
|
{ |
|
55
|
|
|
$result = array(); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
4 |
|
* @var Definition $definition |
|
59
|
|
|
*/ |
|
60
|
4 |
|
foreach ($container->getDefinitions() as $id => $definition) { |
|
61
|
|
|
|
|
62
|
4 |
|
$class = $definition->getClass(); |
|
63
|
|
|
|
|
64
|
2 |
|
if (count($filters) > 0) { |
|
65
|
|
|
|
|
66
|
2 |
|
$found = false; |
|
67
|
|
|
|
|
68
|
2 |
|
foreach ($filters as $namespace) { |
|
69
|
2 |
|
|
|
70
|
2 |
|
if (ClassUtils::isWithinNamespace($class, $namespace)) { |
|
71
|
|
|
$found = true; |
|
72
|
1 |
|
break; |
|
73
|
|
|
} |
|
74
|
2 |
|
} |
|
75
|
2 |
|
|
|
76
|
|
|
if (!$found) { |
|
77
|
1 |
|
continue; // Go to next definition |
|
78
|
|
|
} |
|
79
|
4 |
|
} |
|
80
|
2 |
|
|
|
81
|
|
|
$result[$id] = $definition; |
|
82
|
4 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get definitions from container based on service tag filter |
|
89
|
|
|
* |
|
90
|
|
|
* @param ContainerBuilder $container |
|
91
|
|
|
* @param array $tags Tag names |
|
92
|
2 |
|
* @return Definition[] Definitions indexed by service ID |
|
93
|
|
|
*/ |
|
94
|
2 |
|
protected function getDefinitionsFromTags(ContainerBuilder $container, array $tags) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$result = array(); |
|
97
|
|
|
|
|
98
|
2 |
|
if (count($tags) > 0) { |
|
99
|
|
|
|
|
100
|
2 |
|
foreach ($tags as $tag) { |
|
101
|
|
|
|
|
102
|
2 |
|
foreach (array_keys($container->findTaggedServiceIds($tag)) as $id) { |
|
103
|
1 |
|
|
|
104
|
1 |
|
$result[$id] = $container->getDefinition($id); |
|
105
|
1 |
|
} |
|
106
|
|
|
} |
|
107
|
2 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $result; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Process injection of services via traits for given definitions. |
|
114
|
|
|
* |
|
115
|
|
|
* @param Definition[] $definitions Definitions to process. |
|
116
|
6 |
|
* @param array $injectionMap Injection map. |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function processInjection(array $definitions, array $injectionMap) |
|
119
|
|
|
{ |
|
120
|
|
|
if (count($injectionMap) > 0 && count($definitions) > 0) { |
|
121
|
6 |
|
|
|
122
|
|
|
/** |
|
123
|
6 |
|
* @var Definition $definition |
|
124
|
|
|
*/ |
|
125
|
6 |
|
foreach ($definitions as $definition) { |
|
126
|
|
|
|
|
127
|
6 |
|
$class = $definition->getClass(); |
|
128
|
|
|
|
|
129
|
6 |
|
foreach ($injectionMap as $trait => $injectionDefinition) { |
|
130
|
|
|
|
|
131
|
6 |
|
if (class_exists($class) && ClassUtils::usesTrait($class, $trait)) { |
|
132
|
|
|
|
|
133
|
6 |
|
$arguments = array(); |
|
134
|
6 |
|
|
|
135
|
3 |
|
foreach ($injectionDefinition[1] as $argument) { |
|
136
|
3 |
|
|
|
137
|
|
|
if ('@' === $argument[0]) { |
|
138
|
3 |
|
$arguments[] = new Reference(ltrim($argument, '@')); |
|
139
|
|
|
} else { |
|
140
|
6 |
|
$arguments[] = $argument; |
|
141
|
3 |
|
} |
|
142
|
3 |
|
} |
|
143
|
3 |
|
|
|
144
|
6 |
|
$definition->addMethodCall($injectionDefinition['0'], $arguments); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Remove excluded definitions from definitions collection. |
|
153
|
|
|
* |
|
154
|
|
|
* @param ContainerBuilder $container |
|
155
|
|
|
* @param Definition[] $definitions |
|
156
|
|
|
* @return Definition[] |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function filterExcludedDefinitions(ContainerBuilder $container, array $definitions) |
|
159
|
|
|
{ |
|
160
|
|
|
$excludedServices = $container->hasParameter('roc.traitor.exclude.services') ? $container->getParameter('roc.traitor.exclude.services') : null; |
|
161
|
|
|
$excludedClasses = $container->hasParameter('roc.traitor.exclude.classes') ? $container->getParameter('roc.traitor.exclude.classes') : null; |
|
162
|
|
|
$excludedNamespaces = $container->hasParameter('roc.traitor.exclude.namespaces') ? $container->getParameter('roc.traitor.exclude.namespaces') : null; |
|
163
|
|
|
|
|
164
|
|
|
$result = array_filter($definitions, \Closure::bind(function(Definition $definition, $serviceId) use ($excludedServices, $excludedClasses, $excludedNamespaces) { |
|
165
|
|
|
|
|
166
|
|
|
if (null !== $excludedServices && in_array($serviceId, $excludedServices, true)) { |
|
167
|
|
|
return false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$serviceFqcn = ltrim($definition->getClass(), '\\'); |
|
171
|
|
|
|
|
172
|
|
|
if (null !== $excludedClasses && in_array($serviceFqcn, $excludedClasses, true)) { |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (null !== $excludedNamespaces) { |
|
177
|
|
|
|
|
178
|
|
|
foreach ($excludedNamespaces as $excludedNamespace) { |
|
179
|
|
|
|
|
180
|
|
|
if (strpos($serviceFqcn, $excludedNamespace) === 0) { |
|
181
|
|
|
return false; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return true; |
|
187
|
|
|
|
|
188
|
|
|
}, $this), ARRAY_FILTER_USE_BOTH); |
|
189
|
|
|
|
|
190
|
|
|
return $result; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|