Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection\Compiler;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\Definition;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Run this pass before passes that need to know more about the relation of
|
Chris@0
|
20 * your services.
|
Chris@0
|
21 *
|
Chris@0
|
22 * This class will populate the ServiceReferenceGraph with information. You can
|
Chris@0
|
23 * retrieve the graph in other passes from the compiler.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
26 */
|
Chris@0
|
27 class AnalyzeServiceReferencesPass implements RepeatablePassInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 private $graph;
|
Chris@0
|
30 private $container;
|
Chris@0
|
31 private $currentId;
|
Chris@0
|
32 private $currentDefinition;
|
Chris@0
|
33 private $repeatedPass;
|
Chris@0
|
34 private $onlyConstructorArguments;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
|
Chris@0
|
38 */
|
Chris@0
|
39 public function __construct($onlyConstructorArguments = false)
|
Chris@0
|
40 {
|
Chris@0
|
41 $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * {@inheritdoc}
|
Chris@0
|
46 */
|
Chris@0
|
47 public function setRepeatedPass(RepeatedPass $repeatedPass)
|
Chris@0
|
48 {
|
Chris@0
|
49 $this->repeatedPass = $repeatedPass;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Processes a ContainerBuilder object to populate the service reference graph.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param ContainerBuilder $container
|
Chris@0
|
56 */
|
Chris@0
|
57 public function process(ContainerBuilder $container)
|
Chris@0
|
58 {
|
Chris@0
|
59 $this->container = $container;
|
Chris@0
|
60 $this->graph = $container->getCompiler()->getServiceReferenceGraph();
|
Chris@0
|
61 $this->graph->clear();
|
Chris@0
|
62
|
Chris@0
|
63 foreach ($container->getDefinitions() as $id => $definition) {
|
Chris@0
|
64 if ($definition->isSynthetic() || $definition->isAbstract()) {
|
Chris@0
|
65 continue;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 $this->currentId = $id;
|
Chris@0
|
69 $this->currentDefinition = $definition;
|
Chris@0
|
70
|
Chris@0
|
71 $this->processArguments($definition->getArguments());
|
Chris@0
|
72 if (is_array($definition->getFactory())) {
|
Chris@0
|
73 $this->processArguments($definition->getFactory());
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 if (!$this->onlyConstructorArguments) {
|
Chris@0
|
77 $this->processArguments($definition->getMethodCalls());
|
Chris@0
|
78 $this->processArguments($definition->getProperties());
|
Chris@0
|
79 if ($definition->getConfigurator()) {
|
Chris@0
|
80 $this->processArguments(array($definition->getConfigurator()));
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 foreach ($container->getAliases() as $id => $alias) {
|
Chris@0
|
86 $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Processes service definitions for arguments to find relationships for the service graph.
|
Chris@0
|
92 *
|
Chris@0
|
93 * @param array $arguments An array of Reference or Definition objects relating to service definitions
|
Chris@0
|
94 */
|
Chris@0
|
95 private function processArguments(array $arguments)
|
Chris@0
|
96 {
|
Chris@0
|
97 foreach ($arguments as $argument) {
|
Chris@0
|
98 if (is_array($argument)) {
|
Chris@0
|
99 $this->processArguments($argument);
|
Chris@0
|
100 } elseif ($argument instanceof Reference) {
|
Chris@0
|
101 $this->graph->connect(
|
Chris@0
|
102 $this->currentId,
|
Chris@0
|
103 $this->currentDefinition,
|
Chris@0
|
104 $this->getDefinitionId((string) $argument),
|
Chris@0
|
105 $this->getDefinition((string) $argument),
|
Chris@0
|
106 $argument
|
Chris@0
|
107 );
|
Chris@0
|
108 } elseif ($argument instanceof Definition) {
|
Chris@0
|
109 $this->processArguments($argument->getArguments());
|
Chris@0
|
110 $this->processArguments($argument->getMethodCalls());
|
Chris@0
|
111 $this->processArguments($argument->getProperties());
|
Chris@0
|
112
|
Chris@0
|
113 if (is_array($argument->getFactory())) {
|
Chris@0
|
114 $this->processArguments($argument->getFactory());
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * Returns a service definition given the full name or an alias.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @param string $id A full id or alias for a service definition
|
Chris@0
|
124 *
|
Chris@0
|
125 * @return Definition|null The definition related to the supplied id
|
Chris@0
|
126 */
|
Chris@0
|
127 private function getDefinition($id)
|
Chris@0
|
128 {
|
Chris@0
|
129 $id = $this->getDefinitionId($id);
|
Chris@0
|
130
|
Chris@0
|
131 return null === $id ? null : $this->container->getDefinition($id);
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 private function getDefinitionId($id)
|
Chris@0
|
135 {
|
Chris@0
|
136 while ($this->container->hasAlias($id)) {
|
Chris@0
|
137 $id = (string) $this->container->getAlias($id);
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 if (!$this->container->hasDefinition($id)) {
|
Chris@0
|
141 return;
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 return $id;
|
Chris@0
|
145 }
|
Chris@0
|
146 }
|