composer package updates
[openemr.git] / vendor / symfony / dependency-injection / Compiler / ReplaceAliasByActualDefinitionPass.php
blobbb604b9f9c7fca33afdcb00dc3f837aa5e366188
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16 use Symfony\Component\DependencyInjection\Reference;
18 /**
19 * Replaces aliases with actual service definitions, effectively removing these
20 * aliases.
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface
26 private $compiler;
27 private $formatter;
29 /**
30 * Process the Container to replace aliases with service definitions.
32 * @throws InvalidArgumentException if the service definition does not exist
34 public function process(ContainerBuilder $container)
36 // Setup
37 $this->compiler = $container->getCompiler();
38 $this->formatter = $this->compiler->getLoggingFormatter();
39 // First collect all alias targets that need to be replaced
40 $seenAliasTargets = array();
41 $replacements = array();
42 foreach ($container->getAliases() as $definitionId => $target) {
43 $targetId = (string) $target;
44 // Special case: leave this target alone
45 if ('service_container' === $targetId) {
46 continue;
48 // Check if target needs to be replaces
49 if (isset($replacements[$targetId])) {
50 $container->setAlias($definitionId, $replacements[$targetId]);
52 // No need to process the same target twice
53 if (isset($seenAliasTargets[$targetId])) {
54 continue;
56 // Process new target
57 $seenAliasTargets[$targetId] = true;
58 try {
59 $definition = $container->getDefinition($targetId);
60 } catch (InvalidArgumentException $e) {
61 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
63 if ($definition->isPublic()) {
64 continue;
66 // Remove private definition and schedule for replacement
67 $definition->setPublic(true);
68 $container->setDefinition($definitionId, $definition);
69 $container->removeDefinition($targetId);
70 $replacements[$targetId] = $definitionId;
73 // Now replace target instances in all definitions
74 foreach ($container->getDefinitions() as $definitionId => $definition) {
75 $definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments()));
76 $definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls()));
77 $definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties()));
78 $definition->setFactoryService($this->updateFactoryReferenceId($replacements, $definition->getFactoryService(false)), false);
79 $definition->setFactory($this->updateFactoryReference($replacements, $definition->getFactory()));
83 /**
84 * Recursively updates references in an array.
86 * @param array $replacements Table of aliases to replace
87 * @param string $definitionId Identifier of this definition
88 * @param array $arguments Where to replace the aliases
90 * @return array
92 private function updateArgumentReferences(array $replacements, $definitionId, array $arguments)
94 foreach ($arguments as $k => $argument) {
95 // Handle recursion step
96 if (is_array($argument)) {
97 $arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument);
98 continue;
100 // Skip arguments that don't need replacement
101 if (!$argument instanceof Reference) {
102 continue;
104 $referenceId = (string) $argument;
105 if (!isset($replacements[$referenceId])) {
106 continue;
108 // Perform the replacement
109 $newId = $replacements[$referenceId];
110 $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior());
111 $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId));
114 return $arguments;
118 * Returns the updated reference for the factory service.
120 * @param array $replacements Table of aliases to replace
121 * @param string|null $referenceId Factory service reference identifier
123 * @return string|null
125 private function updateFactoryReferenceId(array $replacements, $referenceId)
127 if (null === $referenceId) {
128 return;
131 return isset($replacements[$referenceId]) ? $replacements[$referenceId] : $referenceId;
134 private function updateFactoryReference(array $replacements, $factory)
136 if (is_array($factory) && $factory[0] instanceof Reference && isset($replacements[$referenceId = (string) $factory[0]])) {
137 $factory[0] = new Reference($replacements[$referenceId], $factory[0]->getInvalidBehavior());
140 return $factory;