1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bundle\ObjectAgent\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
|
10
|
|
|
class ObjectAgentPass implements CompilerPassInterface |
11
|
|
|
{ |
12
|
|
|
public function process(ContainerBuilder $container) |
13
|
|
|
{ |
14
|
|
|
if (!$container->has('psi_object_agent.agent_finder')) { |
15
|
|
|
return; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
$taggedIds = $container->findTaggedServiceIds('psi_object_agent.agent'); |
19
|
|
|
$registryDef = $container->getDefinition('psi_object_agent.agent_finder'); |
20
|
|
|
$enabledAgents = $container->getParameter('psi_object_agent.enabled_agents'); |
21
|
|
|
|
22
|
|
|
foreach ($taggedIds as $serviceId => $attributes) { |
23
|
|
|
$attributes = $attributes[0]; |
24
|
|
|
if (!isset($attributes['alias'])) { |
25
|
|
|
throw new InvalidArgumentException(sprintf( |
26
|
|
|
$this->context . ' "%s" has no "alias" attribute in its tag', |
|
|
|
|
27
|
|
|
$serviceId |
28
|
|
|
)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$alias = $attributes['alias']; |
32
|
|
|
|
33
|
|
|
$agents[$alias] = new Reference($serviceId); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($diff = array_diff($enabledAgents, array_keys($agents))) { |
|
|
|
|
37
|
|
|
throw new \RuntimeException(sprintf( |
38
|
|
|
'Unknown agent(s) "%s". Known agents: "%s"', |
39
|
|
|
implode('", "', $diff), implode('", "', array_keys($agents)) |
40
|
|
|
)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$agents = array_filter($agents, function ($key) use ($enabledAgents) { |
44
|
|
|
return in_array($key, $enabledAgents); |
45
|
|
|
}, ARRAY_FILTER_USE_KEY); |
46
|
|
|
|
47
|
|
|
$registryDef->replaceArgument(0, $agents); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: