Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/dependency-injection/Compiler/ReplaceAliasByActualDefinitionPass.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 |
---|---|
19 * Replaces aliases with actual service definitions, effectively removing these | 19 * Replaces aliases with actual service definitions, effectively removing these |
20 * aliases. | 20 * aliases. |
21 * | 21 * |
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com> | 22 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
23 */ | 23 */ |
24 class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface | 24 class ReplaceAliasByActualDefinitionPass extends AbstractRecursivePass |
25 { | 25 { |
26 private $compiler; | 26 private $replacements; |
27 private $formatter; | |
28 | 27 |
29 /** | 28 /** |
30 * Process the Container to replace aliases with service definitions. | 29 * Process the Container to replace aliases with service definitions. |
31 * | |
32 * @param ContainerBuilder $container | |
33 * | 30 * |
34 * @throws InvalidArgumentException if the service definition does not exist | 31 * @throws InvalidArgumentException if the service definition does not exist |
35 */ | 32 */ |
36 public function process(ContainerBuilder $container) | 33 public function process(ContainerBuilder $container) |
37 { | 34 { |
38 // Setup | |
39 $this->compiler = $container->getCompiler(); | |
40 $this->formatter = $this->compiler->getLoggingFormatter(); | |
41 // First collect all alias targets that need to be replaced | 35 // First collect all alias targets that need to be replaced |
42 $seenAliasTargets = array(); | 36 $seenAliasTargets = array(); |
43 $replacements = array(); | 37 $replacements = array(); |
44 foreach ($container->getAliases() as $definitionId => $target) { | 38 foreach ($container->getAliases() as $definitionId => $target) { |
45 $targetId = (string) $target; | 39 $targetId = $container->normalizeId($target); |
46 // Special case: leave this target alone | 40 // Special case: leave this target alone |
47 if ('service_container' === $targetId) { | 41 if ('service_container' === $targetId) { |
48 continue; | 42 continue; |
49 } | 43 } |
50 // Check if target needs to be replaces | 44 // Check if target needs to be replaces |
51 if (isset($replacements[$targetId])) { | 45 if (isset($replacements[$targetId])) { |
52 $container->setAlias($definitionId, $replacements[$targetId]); | 46 $container->setAlias($definitionId, $replacements[$targetId])->setPublic($target->isPublic())->setPrivate($target->isPrivate()); |
53 } | 47 } |
54 // No need to process the same target twice | 48 // No need to process the same target twice |
55 if (isset($seenAliasTargets[$targetId])) { | 49 if (isset($seenAliasTargets[$targetId])) { |
56 continue; | 50 continue; |
57 } | 51 } |
60 try { | 54 try { |
61 $definition = $container->getDefinition($targetId); | 55 $definition = $container->getDefinition($targetId); |
62 } catch (InvalidArgumentException $e) { | 56 } catch (InvalidArgumentException $e) { |
63 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e); | 57 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e); |
64 } | 58 } |
65 if ($definition->isPublic()) { | 59 if ($definition->isPublic() || $definition->isPrivate()) { |
66 continue; | 60 continue; |
67 } | 61 } |
68 // Remove private definition and schedule for replacement | 62 // Remove private definition and schedule for replacement |
69 $definition->setPublic(true); | 63 $definition->setPublic(!$target->isPrivate()); |
64 $definition->setPrivate($target->isPrivate()); | |
70 $container->setDefinition($definitionId, $definition); | 65 $container->setDefinition($definitionId, $definition); |
71 $container->removeDefinition($targetId); | 66 $container->removeDefinition($targetId); |
72 $replacements[$targetId] = $definitionId; | 67 $replacements[$targetId] = $definitionId; |
73 } | 68 } |
69 $this->replacements = $replacements; | |
74 | 70 |
75 // Now replace target instances in all definitions | 71 parent::process($container); |
76 foreach ($container->getDefinitions() as $definitionId => $definition) { | 72 $this->replacements = array(); |
77 $definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments())); | |
78 $definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls())); | |
79 $definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties())); | |
80 $definition->setFactory($this->updateFactoryReference($replacements, $definition->getFactory())); | |
81 } | |
82 } | 73 } |
83 | 74 |
84 /** | 75 /** |
85 * Recursively updates references in an array. | 76 * {@inheritdoc} |
86 * | |
87 * @param array $replacements Table of aliases to replace | |
88 * @param string $definitionId Identifier of this definition | |
89 * @param array $arguments Where to replace the aliases | |
90 * | |
91 * @return array | |
92 */ | 77 */ |
93 private function updateArgumentReferences(array $replacements, $definitionId, array $arguments) | 78 protected function processValue($value, $isRoot = false) |
94 { | 79 { |
95 foreach ($arguments as $k => $argument) { | 80 if ($value instanceof Reference && isset($this->replacements[$referenceId = $this->container->normalizeId($value)])) { |
96 // Handle recursion step | |
97 if (is_array($argument)) { | |
98 $arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument); | |
99 continue; | |
100 } | |
101 // Skip arguments that don't need replacement | |
102 if (!$argument instanceof Reference) { | |
103 continue; | |
104 } | |
105 $referenceId = (string) $argument; | |
106 if (!isset($replacements[$referenceId])) { | |
107 continue; | |
108 } | |
109 // Perform the replacement | 81 // Perform the replacement |
110 $newId = $replacements[$referenceId]; | 82 $newId = $this->replacements[$referenceId]; |
111 $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior()); | 83 $value = new Reference($newId, $value->getInvalidBehavior()); |
112 $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId)); | 84 $this->container->log($this, sprintf('Changed reference of service "%s" previously pointing to "%s" to "%s".', $this->currentId, $referenceId, $newId)); |
113 } | 85 } |
114 | 86 |
115 return $arguments; | 87 return parent::processValue($value, $isRoot); |
116 } | |
117 | |
118 private function updateFactoryReference(array $replacements, $factory) | |
119 { | |
120 if (is_array($factory) && $factory[0] instanceof Reference && isset($replacements[$referenceId = (string) $factory[0]])) { | |
121 $factory[0] = new Reference($replacements[$referenceId], $factory[0]->getInvalidBehavior()); | |
122 } | |
123 | |
124 return $factory; | |
125 } | 88 } |
126 } | 89 } |