1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bundle\Description\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
|
9
|
|
|
class DescriptionFactoryPass implements CompilerPassInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
public function process(ContainerBuilder $container) |
15
|
|
|
{ |
16
|
|
|
if (!$container->hasDefinition('psi_description.factory')) { |
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$factoryDef = $container->getDefinition('psi_description.factory'); |
21
|
|
|
$enhancerIds = $container->findTaggedServiceIds('psi_description.enhancer'); |
22
|
|
|
$resolverIds = $container->findTaggedServiceIds('psi_description.subject_resolver'); |
23
|
|
|
|
24
|
|
|
$refs = [ |
25
|
|
|
'enhancer' => [], |
26
|
|
|
'subject_resolver' => [], |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
foreach ([ |
30
|
|
|
'enhancer' => $enhancerIds, |
31
|
|
|
'subject_resolver' => $resolverIds, |
32
|
|
|
] as $key => $serviceIds) { |
33
|
|
|
$humanKey = str_replace('_', ' ', $key); |
34
|
|
View Code Duplication |
foreach ($serviceIds as $serviceId => $attributes) { |
|
|
|
|
35
|
|
|
$attributes = $attributes[0]; |
36
|
|
|
|
37
|
|
|
if (!isset($attributes['alias'])) { |
38
|
|
|
throw new \InvalidArgumentException(sprintf( |
39
|
|
|
'Description %s "%s" has no "alias" attribute in its tag', |
40
|
|
|
$humanKey, |
41
|
|
|
$serviceId |
42
|
|
|
)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$alias = $attributes['alias']; |
46
|
|
|
|
47
|
|
|
$refs[$key][$alias] = new Reference($serviceId); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$enabled = $container->getParameter(sprintf('psi_description.%ss', $key)); |
51
|
|
|
$diff = array_diff($enabled, array_keys($refs[$key])); |
52
|
|
|
|
53
|
|
|
if ($diff) { |
|
|
|
|
54
|
|
|
throw new \InvalidArgumentException(sprintf( |
55
|
|
|
'Unknown %s(s) "%s" specified. Known %s(s): "%s"', |
56
|
|
|
$humanKey, |
57
|
|
|
implode('", "', $diff), |
58
|
|
|
$humanKey, |
59
|
|
|
implode('", "', array_keys($refs[$key])) |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$orderedRefs = []; |
64
|
|
|
|
65
|
|
|
foreach ($enabled as $enabledAlias) { |
66
|
|
|
$orderedRefs[$enabledAlias] = $refs[$key][$enabledAlias]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$refs[$key] = $orderedRefs; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$factoryDef->replaceArgument(0, array_values($refs['enhancer'])); |
73
|
|
|
$factoryDef->replaceArgument(2, array_values($refs['subject_resolver'])); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.