annotate vendor/symfony/dependency-injection/Compiler/ReplaceAliasByActualDefinitionPass.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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@14 24 class ReplaceAliasByActualDefinitionPass extends AbstractRecursivePass
Chris@0 25 {
Chris@14 26 private $replacements;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Process the Container to replace aliases with service definitions.
Chris@0 30 *
Chris@0 31 * @throws InvalidArgumentException if the service definition does not exist
Chris@0 32 */
Chris@0 33 public function process(ContainerBuilder $container)
Chris@0 34 {
Chris@0 35 // First collect all alias targets that need to be replaced
Chris@17 36 $seenAliasTargets = [];
Chris@17 37 $replacements = [];
Chris@0 38 foreach ($container->getAliases() as $definitionId => $target) {
Chris@14 39 $targetId = $container->normalizeId($target);
Chris@0 40 // Special case: leave this target alone
Chris@0 41 if ('service_container' === $targetId) {
Chris@0 42 continue;
Chris@0 43 }
Chris@0 44 // Check if target needs to be replaces
Chris@0 45 if (isset($replacements[$targetId])) {
Chris@14 46 $container->setAlias($definitionId, $replacements[$targetId])->setPublic($target->isPublic())->setPrivate($target->isPrivate());
Chris@0 47 }
Chris@0 48 // No need to process the same target twice
Chris@0 49 if (isset($seenAliasTargets[$targetId])) {
Chris@0 50 continue;
Chris@0 51 }
Chris@0 52 // Process new target
Chris@0 53 $seenAliasTargets[$targetId] = true;
Chris@0 54 try {
Chris@0 55 $definition = $container->getDefinition($targetId);
Chris@0 56 } catch (InvalidArgumentException $e) {
Chris@0 57 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
Chris@0 58 }
Chris@14 59 if ($definition->isPublic() || $definition->isPrivate()) {
Chris@0 60 continue;
Chris@0 61 }
Chris@0 62 // Remove private definition and schedule for replacement
Chris@14 63 $definition->setPublic(!$target->isPrivate());
Chris@14 64 $definition->setPrivate($target->isPrivate());
Chris@0 65 $container->setDefinition($definitionId, $definition);
Chris@0 66 $container->removeDefinition($targetId);
Chris@0 67 $replacements[$targetId] = $definitionId;
Chris@0 68 }
Chris@14 69 $this->replacements = $replacements;
Chris@0 70
Chris@14 71 parent::process($container);
Chris@17 72 $this->replacements = [];
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@14 76 * {@inheritdoc}
Chris@0 77 */
Chris@14 78 protected function processValue($value, $isRoot = false)
Chris@0 79 {
Chris@14 80 if ($value instanceof Reference && isset($this->replacements[$referenceId = $this->container->normalizeId($value)])) {
Chris@0 81 // Perform the replacement
Chris@14 82 $newId = $this->replacements[$referenceId];
Chris@14 83 $value = new Reference($newId, $value->getInvalidBehavior());
Chris@14 84 $this->container->log($this, sprintf('Changed reference of service "%s" previously pointing to "%s" to "%s".', $this->currentId, $referenceId, $newId));
Chris@0 85 }
Chris@0 86
Chris@14 87 return parent::processValue($value, $isRoot);
Chris@0 88 }
Chris@0 89 }