comparison vendor/symfony/dependency-injection/Compiler/AnalyzeServiceReferencesPass.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\DependencyInjection\Compiler; 12 namespace Symfony\Component\DependencyInjection\Compiler;
13 13
14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\DependencyInjection\Definition; 16 use Symfony\Component\DependencyInjection\Definition;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
15 use Symfony\Component\DependencyInjection\ExpressionLanguage; 18 use Symfony\Component\DependencyInjection\ExpressionLanguage;
16 use Symfony\Component\DependencyInjection\Reference; 19 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\ContainerBuilder; 20 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\ExpressionLanguage\Expression; 21 use Symfony\Component\ExpressionLanguage\Expression;
19 22
24 * This class will populate the ServiceReferenceGraph with information. You can 27 * This class will populate the ServiceReferenceGraph with information. You can
25 * retrieve the graph in other passes from the compiler. 28 * retrieve the graph in other passes from the compiler.
26 * 29 *
27 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 30 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
28 */ 31 */
29 class AnalyzeServiceReferencesPass implements RepeatablePassInterface 32 class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface
30 { 33 {
31 private $graph; 34 private $graph;
32 private $container;
33 private $currentId;
34 private $currentDefinition; 35 private $currentDefinition;
35 private $repeatedPass;
36 private $onlyConstructorArguments; 36 private $onlyConstructorArguments;
37 private $lazy;
37 private $expressionLanguage; 38 private $expressionLanguage;
38 39
39 /** 40 /**
40 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls 41 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
41 */ 42 */
47 /** 48 /**
48 * {@inheritdoc} 49 * {@inheritdoc}
49 */ 50 */
50 public function setRepeatedPass(RepeatedPass $repeatedPass) 51 public function setRepeatedPass(RepeatedPass $repeatedPass)
51 { 52 {
52 $this->repeatedPass = $repeatedPass; 53 // no-op for BC
53 } 54 }
54 55
55 /** 56 /**
56 * Processes a ContainerBuilder object to populate the service reference graph. 57 * Processes a ContainerBuilder object to populate the service reference graph.
57 *
58 * @param ContainerBuilder $container
59 */ 58 */
60 public function process(ContainerBuilder $container) 59 public function process(ContainerBuilder $container)
61 { 60 {
62 $this->container = $container; 61 $this->container = $container;
63 $this->graph = $container->getCompiler()->getServiceReferenceGraph(); 62 $this->graph = $container->getCompiler()->getServiceReferenceGraph();
64 $this->graph->clear(); 63 $this->graph->clear();
64 $this->lazy = false;
65 65
66 foreach ($container->getDefinitions() as $id => $definition) { 66 foreach ($container->getAliases() as $id => $alias) {
67 if ($definition->isSynthetic() || $definition->isAbstract()) { 67 $targetId = $this->getDefinitionId((string) $alias);
68 continue; 68 $this->graph->connect($id, $alias, $targetId, $this->getDefinition($targetId), null);
69 }
70
71 $this->currentId = $id;
72 $this->currentDefinition = $definition;
73
74 $this->processArguments($definition->getArguments());
75 if (is_array($definition->getFactory())) {
76 $this->processArguments($definition->getFactory());
77 }
78
79 if (!$this->onlyConstructorArguments) {
80 $this->processArguments($definition->getMethodCalls());
81 $this->processArguments($definition->getProperties());
82 if ($definition->getConfigurator()) {
83 $this->processArguments(array($definition->getConfigurator()));
84 }
85 }
86 } 69 }
87 70
88 foreach ($container->getAliases() as $id => $alias) { 71 parent::process($container);
89 $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
90 }
91 } 72 }
92 73
93 /** 74 protected function processValue($value, $isRoot = false)
94 * Processes service definitions for arguments to find relationships for the service graph.
95 *
96 * @param array $arguments An array of Reference or Definition objects relating to service definitions
97 */
98 private function processArguments(array $arguments)
99 { 75 {
100 foreach ($arguments as $argument) { 76 $lazy = $this->lazy;
101 if (is_array($argument)) {
102 $this->processArguments($argument);
103 } elseif ($argument instanceof Expression) {
104 $this->getExpressionLanguage()->compile((string) $argument, array('this' => 'container'));
105 } elseif ($argument instanceof Reference) {
106 $this->graph->connect(
107 $this->currentId,
108 $this->currentDefinition,
109 $this->getDefinitionId((string) $argument),
110 $this->getDefinition((string) $argument),
111 $argument
112 );
113 } elseif ($argument instanceof Definition) {
114 $this->processArguments($argument->getArguments());
115 $this->processArguments($argument->getMethodCalls());
116 $this->processArguments($argument->getProperties());
117 77
118 if (is_array($argument->getFactory())) { 78 if ($value instanceof ArgumentInterface) {
119 $this->processArguments($argument->getFactory()); 79 $this->lazy = true;
120 } 80 parent::processValue($value->getValues());
81 $this->lazy = $lazy;
82
83 return $value;
84 }
85 if ($value instanceof Expression) {
86 $this->getExpressionLanguage()->compile((string) $value, array('this' => 'container'));
87
88 return $value;
89 }
90 if ($value instanceof Reference) {
91 $targetId = $this->getDefinitionId((string) $value);
92 $targetDefinition = $this->getDefinition($targetId);
93
94 $this->graph->connect(
95 $this->currentId,
96 $this->currentDefinition,
97 $targetId,
98 $targetDefinition,
99 $value,
100 $this->lazy || ($targetDefinition && $targetDefinition->isLazy()),
101 ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()
102 );
103
104 return $value;
105 }
106 if (!$value instanceof Definition) {
107 return parent::processValue($value, $isRoot);
108 }
109 if ($isRoot) {
110 if ($value->isSynthetic() || $value->isAbstract()) {
111 return $value;
121 } 112 }
113 $this->currentDefinition = $value;
122 } 114 }
115 $this->lazy = false;
116
117 $this->processValue($value->getFactory());
118 $this->processValue($value->getArguments());
119
120 if (!$this->onlyConstructorArguments) {
121 $this->processValue($value->getProperties());
122 $this->processValue($value->getMethodCalls());
123 $this->processValue($value->getConfigurator());
124 }
125 $this->lazy = $lazy;
126
127 return $value;
123 } 128 }
124 129
125 /** 130 /**
126 * Returns a service definition given the full name or an alias. 131 * Returns a service definition given the full name or an alias.
127 * 132 *
129 * 134 *
130 * @return Definition|null The definition related to the supplied id 135 * @return Definition|null The definition related to the supplied id
131 */ 136 */
132 private function getDefinition($id) 137 private function getDefinition($id)
133 { 138 {
134 $id = $this->getDefinitionId($id);
135
136 return null === $id ? null : $this->container->getDefinition($id); 139 return null === $id ? null : $this->container->getDefinition($id);
137 } 140 }
138 141
139 private function getDefinitionId($id) 142 private function getDefinitionId($id)
140 { 143 {
144 147
145 if (!$this->container->hasDefinition($id)) { 148 if (!$this->container->hasDefinition($id)) {
146 return; 149 return;
147 } 150 }
148 151
149 return $id; 152 return $this->container->normalizeId($id);
150 } 153 }
151 154
152 private function getExpressionLanguage() 155 private function getExpressionLanguage()
153 { 156 {
154 if (null === $this->expressionLanguage) { 157 if (null === $this->expressionLanguage) {
158 if (!class_exists(ExpressionLanguage::class)) {
159 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
160 }
161
155 $providers = $this->container->getExpressionLanguageProviders(); 162 $providers = $this->container->getExpressionLanguageProviders();
156 $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) { 163 $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
157 if ('""' === substr_replace($arg, '', 1, -1)) { 164 if ('""' === substr_replace($arg, '', 1, -1)) {
158 $id = stripcslashes(substr($arg, 1, -1)); 165 $id = stripcslashes(substr($arg, 1, -1));
166 $id = $this->getDefinitionId($id);
159 167
160 $this->graph->connect( 168 $this->graph->connect(
161 $this->currentId, 169 $this->currentId,
162 $this->currentDefinition, 170 $this->currentDefinition,
163 $this->getDefinitionId($id), 171 $id,
164 $this->getDefinition($id) 172 $this->getDefinition($id)
165 ); 173 );
166 } 174 }
167 175
168 return sprintf('$this->get(%s)', $arg); 176 return sprintf('$this->get(%s)', $arg);