comparison vendor/symfony/dependency-injection/Compiler/ResolveReferencesToAliasesPass.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 4c8ae668cc8c
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\Alias;
15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; 14 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
16 use Symfony\Component\DependencyInjection\Reference; 15 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\ContainerBuilder; 16 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 17
19 /** 18 /**
20 * Replaces all references to aliases with references to the actual service. 19 * Replaces all references to aliases with references to the actual service.
21 * 20 *
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 21 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
23 */ 22 */
24 class ResolveReferencesToAliasesPass implements CompilerPassInterface 23 class ResolveReferencesToAliasesPass extends AbstractRecursivePass
25 { 24 {
26 private $container;
27
28 /** 25 /**
29 * Processes the ContainerBuilder to replace references to aliases with actual service references. 26 * {@inheritdoc}
30 *
31 * @param ContainerBuilder $container
32 */ 27 */
33 public function process(ContainerBuilder $container) 28 public function process(ContainerBuilder $container)
34 { 29 {
35 $this->container = $container; 30 parent::process($container);
36
37 foreach ($container->getDefinitions() as $definition) {
38 if ($definition->isSynthetic() || $definition->isAbstract()) {
39 continue;
40 }
41
42 $definition->setArguments($this->processArguments($definition->getArguments()));
43 $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
44 $definition->setProperties($this->processArguments($definition->getProperties()));
45 $definition->setFactory($this->processFactory($definition->getFactory()));
46 }
47 31
48 foreach ($container->getAliases() as $id => $alias) { 32 foreach ($container->getAliases() as $id => $alias) {
49 $aliasId = (string) $alias; 33 $aliasId = $container->normalizeId($alias);
50 if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) { 34 if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
51 $container->setAlias($id, new Alias($defId, $alias->isPublic())); 35 $container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
52 } 36 }
53 } 37 }
54 } 38 }
55 39
56 /** 40 /**
57 * Processes the arguments to replace aliases. 41 * {@inheritdoc}
58 *
59 * @param array $arguments An array of References
60 *
61 * @return array An array of References
62 */ 42 */
63 private function processArguments(array $arguments) 43 protected function processValue($value, $isRoot = false)
64 { 44 {
65 foreach ($arguments as $k => $argument) { 45 if ($value instanceof Reference) {
66 if (is_array($argument)) { 46 $defId = $this->getDefinitionId($id = $this->container->normalizeId($value), $this->container);
67 $arguments[$k] = $this->processArguments($argument);
68 } elseif ($argument instanceof Reference) {
69 $defId = $this->getDefinitionId($id = (string) $argument);
70 47
71 if ($defId !== $id) { 48 if ($defId !== $id) {
72 $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior()); 49 return new Reference($defId, $value->getInvalidBehavior());
73 }
74 } 50 }
75 } 51 }
76 52
77 return $arguments; 53 return parent::processValue($value);
78 }
79
80 private function processFactory($factory)
81 {
82 if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
83 return $factory;
84 }
85
86 $defId = $this->getDefinitionId($id = (string) $factory[0]);
87
88 if ($defId !== $id) {
89 $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior());
90 }
91
92 return $factory;
93 } 54 }
94 55
95 /** 56 /**
96 * Resolves an alias into a definition id. 57 * Resolves an alias into a definition id.
97 * 58 *
98 * @param string $id The definition or alias id to resolve 59 * @param string $id The definition or alias id to resolve
60 * @param ContainerBuilder $container
99 * 61 *
100 * @return string The definition id with aliases resolved 62 * @return string The definition id with aliases resolved
101 */ 63 */
102 private function getDefinitionId($id) 64 private function getDefinitionId($id, ContainerBuilder $container)
103 { 65 {
104 $seen = array(); 66 $seen = array();
105 while ($this->container->hasAlias($id)) { 67 while ($container->hasAlias($id)) {
106 if (isset($seen[$id])) { 68 if (isset($seen[$id])) {
107 throw new ServiceCircularReferenceException($id, array_keys($seen)); 69 throw new ServiceCircularReferenceException($id, array_keys($seen));
108 } 70 }
109 $seen[$id] = true; 71 $seen[$id] = true;
110 $id = (string) $this->container->getAlias($id); 72 $id = $container->normalizeId($container->getAlias($id));
111 } 73 }
112 74
113 return $id; 75 return $id;
114 } 76 }
115 } 77 }