comparison vendor/symfony/dependency-injection/Compiler/AnalyzeServiceReferencesPass.php @ 0:4c8ae668cc8c

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