annotate vendor/symfony/dependency-injection/Compiler/ReplaceAliasByActualDefinitionPass.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DependencyInjection\Compiler;
Chris@0 13
Chris@0 14 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@0 15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Chris@0 16 use Symfony\Component\DependencyInjection\Reference;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Replaces aliases with actual service definitions, effectively removing these
Chris@0 20 * aliases.
Chris@0 21 *
Chris@0 22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 23 */
Chris@0 24 class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface
Chris@0 25 {
Chris@0 26 private $compiler;
Chris@0 27 private $formatter;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Process the Container to replace aliases with service definitions.
Chris@0 31 *
Chris@0 32 * @param ContainerBuilder $container
Chris@0 33 *
Chris@0 34 * @throws InvalidArgumentException if the service definition does not exist
Chris@0 35 */
Chris@0 36 public function process(ContainerBuilder $container)
Chris@0 37 {
Chris@0 38 // Setup
Chris@0 39 $this->compiler = $container->getCompiler();
Chris@0 40 $this->formatter = $this->compiler->getLoggingFormatter();
Chris@0 41 // First collect all alias targets that need to be replaced
Chris@0 42 $seenAliasTargets = array();
Chris@0 43 $replacements = array();
Chris@0 44 foreach ($container->getAliases() as $definitionId => $target) {
Chris@0 45 $targetId = (string) $target;
Chris@0 46 // Special case: leave this target alone
Chris@0 47 if ('service_container' === $targetId) {
Chris@0 48 continue;
Chris@0 49 }
Chris@0 50 // Check if target needs to be replaces
Chris@0 51 if (isset($replacements[$targetId])) {
Chris@0 52 $container->setAlias($definitionId, $replacements[$targetId]);
Chris@0 53 }
Chris@0 54 // No need to process the same target twice
Chris@0 55 if (isset($seenAliasTargets[$targetId])) {
Chris@0 56 continue;
Chris@0 57 }
Chris@0 58 // Process new target
Chris@0 59 $seenAliasTargets[$targetId] = true;
Chris@0 60 try {
Chris@0 61 $definition = $container->getDefinition($targetId);
Chris@0 62 } catch (InvalidArgumentException $e) {
Chris@0 63 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
Chris@0 64 }
Chris@0 65 if ($definition->isPublic()) {
Chris@0 66 continue;
Chris@0 67 }
Chris@0 68 // Remove private definition and schedule for replacement
Chris@0 69 $definition->setPublic(true);
Chris@0 70 $container->setDefinition($definitionId, $definition);
Chris@0 71 $container->removeDefinition($targetId);
Chris@0 72 $replacements[$targetId] = $definitionId;
Chris@0 73 }
Chris@0 74
Chris@0 75 // Now replace target instances in all definitions
Chris@0 76 foreach ($container->getDefinitions() as $definitionId => $definition) {
Chris@0 77 $definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments()));
Chris@0 78 $definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls()));
Chris@0 79 $definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties()));
Chris@0 80 $definition->setFactory($this->updateFactoryReference($replacements, $definition->getFactory()));
Chris@0 81 }
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Recursively updates references in an array.
Chris@0 86 *
Chris@0 87 * @param array $replacements Table of aliases to replace
Chris@0 88 * @param string $definitionId Identifier of this definition
Chris@0 89 * @param array $arguments Where to replace the aliases
Chris@0 90 *
Chris@0 91 * @return array
Chris@0 92 */
Chris@0 93 private function updateArgumentReferences(array $replacements, $definitionId, array $arguments)
Chris@0 94 {
Chris@0 95 foreach ($arguments as $k => $argument) {
Chris@0 96 // Handle recursion step
Chris@0 97 if (is_array($argument)) {
Chris@0 98 $arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument);
Chris@0 99 continue;
Chris@0 100 }
Chris@0 101 // Skip arguments that don't need replacement
Chris@0 102 if (!$argument instanceof Reference) {
Chris@0 103 continue;
Chris@0 104 }
Chris@0 105 $referenceId = (string) $argument;
Chris@0 106 if (!isset($replacements[$referenceId])) {
Chris@0 107 continue;
Chris@0 108 }
Chris@0 109 // Perform the replacement
Chris@0 110 $newId = $replacements[$referenceId];
Chris@0 111 $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior());
Chris@0 112 $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId));
Chris@0 113 }
Chris@0 114
Chris@0 115 return $arguments;
Chris@0 116 }
Chris@0 117
Chris@0 118 private function updateFactoryReference(array $replacements, $factory)
Chris@0 119 {
Chris@0 120 if (is_array($factory) && $factory[0] instanceof Reference && isset($replacements[$referenceId = (string) $factory[0]])) {
Chris@0 121 $factory[0] = new Reference($replacements[$referenceId], $factory[0]->getInvalidBehavior());
Chris@0 122 }
Chris@0 123
Chris@0 124 return $factory;
Chris@0 125 }
Chris@0 126 }