Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/dependency-injection/Compiler/ResolveReferencesToAliasesPass.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
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\Alias; | |
15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; | |
16 use Symfony\Component\DependencyInjection\Reference; | |
17 use Symfony\Component\DependencyInjection\ContainerBuilder; | |
18 | |
19 /** | |
20 * Replaces all references to aliases with references to the actual service. | |
21 * | |
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com> | |
23 */ | |
24 class ResolveReferencesToAliasesPass implements CompilerPassInterface | |
25 { | |
26 private $container; | |
27 | |
28 /** | |
29 * Processes the ContainerBuilder to replace references to aliases with actual service references. | |
30 * | |
31 * @param ContainerBuilder $container | |
32 */ | |
33 public function process(ContainerBuilder $container) | |
34 { | |
35 $this->container = $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 | |
48 foreach ($container->getAliases() as $id => $alias) { | |
49 $aliasId = (string) $alias; | |
50 if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) { | |
51 $container->setAlias($id, new Alias($defId, $alias->isPublic())); | |
52 } | |
53 } | |
54 } | |
55 | |
56 /** | |
57 * Processes the arguments to replace aliases. | |
58 * | |
59 * @param array $arguments An array of References | |
60 * | |
61 * @return array An array of References | |
62 */ | |
63 private function processArguments(array $arguments) | |
64 { | |
65 foreach ($arguments as $k => $argument) { | |
66 if (is_array($argument)) { | |
67 $arguments[$k] = $this->processArguments($argument); | |
68 } elseif ($argument instanceof Reference) { | |
69 $defId = $this->getDefinitionId($id = (string) $argument); | |
70 | |
71 if ($defId !== $id) { | |
72 $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior()); | |
73 } | |
74 } | |
75 } | |
76 | |
77 return $arguments; | |
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 } | |
94 | |
95 /** | |
96 * Resolves an alias into a definition id. | |
97 * | |
98 * @param string $id The definition or alias id to resolve | |
99 * | |
100 * @return string The definition id with aliases resolved | |
101 */ | |
102 private function getDefinitionId($id) | |
103 { | |
104 $seen = array(); | |
105 while ($this->container->hasAlias($id)) { | |
106 if (isset($seen[$id])) { | |
107 throw new ServiceCircularReferenceException($id, array_keys($seen)); | |
108 } | |
109 $seen[$id] = true; | |
110 $id = (string) $this->container->getAlias($id); | |
111 } | |
112 | |
113 return $id; | |
114 } | |
115 } |