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;
19 * Replaces aliases with actual service definitions, effectively removing these
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface
30 * Process the Container to replace aliases with service definitions.
32 * @param ContainerBuilder $container
34 * @throws InvalidArgumentException if the service definition does not exist
36 public function process(ContainerBuilder $container)
39 $this->compiler = $container->getCompiler();
40 $this->formatter = $this->compiler->getLoggingFormatter();
41 // First collect all alias targets that need to be replaced
42 $seenAliasTargets = array();
43 $replacements = array();
44 foreach ($container->getAliases() as $definitionId => $target) {
45 $targetId = (string) $target;
46 // Special case: leave this target alone
47 if ('service_container' === $targetId) {
50 // Check if target needs to be replaces
51 if (isset($replacements[$targetId])) {
52 $container->setAlias($definitionId, $replacements[$targetId]);
54 // No need to process the same target twice
55 if (isset($seenAliasTargets[$targetId])) {
59 $seenAliasTargets[$targetId] = true;
61 $definition = $container->getDefinition($targetId);
62 } catch (InvalidArgumentException $e) {
63 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
65 if ($definition->isPublic()) {
68 // Remove private definition and schedule for replacement
69 $definition->setPublic(true);
70 $container->setDefinition($definitionId, $definition);
71 $container->removeDefinition($targetId);
72 $replacements[$targetId] = $definitionId;
75 // Now replace target instances in all definitions
76 foreach ($container->getDefinitions() as $definitionId => $definition) {
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()));
85 * Recursively updates references in an array.
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
93 private function updateArgumentReferences(array $replacements, $definitionId, array $arguments)
95 foreach ($arguments as $k => $argument) {
96 // Handle recursion step
97 if (is_array($argument)) {
98 $arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument);
101 // Skip arguments that don't need replacement
102 if (!$argument instanceof Reference) {
105 $referenceId = (string) $argument;
106 if (!isset($replacements[$referenceId])) {
109 // Perform the replacement
110 $newId = $replacements[$referenceId];
111 $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior());
112 $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId));
118 private function updateFactoryReference(array $replacements, $factory)
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());