From 8729f9fd6a95130948fc33f796a6672a49eaf9b9 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 13 Sep 2017 13:10:11 +0200 Subject: [PATCH] [DI] Resolve nested refs to aliases --- .../FrameworkExtension.php | 2 +- .../Bundle/SecurityBundle/composer.json | 4 +- .../ResolveReferencesToAliasesPass.php | 75 ++++--------------- 3 files changed, 19 insertions(+), 62 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 0ead938a1b..ea6dec7fd6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1463,7 +1463,7 @@ class FrameworkExtension extends Extension ->replaceArgument(2, $config['debug']) ->addTag('annotations.cached_reader', array('provider' => $cacheService)) ; - $container->setAlias('annotation_reader', 'annotations.cached_reader'); + $container->setAlias('annotation_reader', 'annotations.cached_reader')->setPrivate(true); $container->setAlias(Reader::class, new Alias('annotations.cached_reader', false)); } else { $container->removeDefinition('annotations.cached_reader'); diff --git a/src/Symfony/Bundle/SecurityBundle/composer.json b/src/Symfony/Bundle/SecurityBundle/composer.json index 9690cf325a..3e7caac2ec 100644 --- a/src/Symfony/Bundle/SecurityBundle/composer.json +++ b/src/Symfony/Bundle/SecurityBundle/composer.json @@ -35,8 +35,8 @@ "symfony/http-foundation": "~2.8|~3.0|~4.0", "symfony/security-acl": "~2.8|~3.0", "symfony/translation": "~2.8|~3.0|~4.0", - "symfony/twig-bundle": "~2.8|~3.0|~4.0", - "symfony/twig-bridge": "~2.8|~3.0|~4.0", + "symfony/twig-bundle": "~3.4|~4.0", + "symfony/twig-bridge": "~3.4|~4.0", "symfony/process": "~2.8|~3.0|~4.0", "symfony/validator": "^3.2.5|~4.0", "symfony/var-dumper": "~3.3|~4.0", diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php index 6946969a2c..6d42f7b463 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php @@ -11,8 +11,6 @@ namespace Symfony\Component\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Alias; -use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -22,97 +20,56 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; * * @author Johannes M. Schmitt */ -class ResolveReferencesToAliasesPass implements CompilerPassInterface +class ResolveReferencesToAliasesPass extends AbstractRecursivePass { - private $container; - /** - * Processes the ContainerBuilder to replace references to aliases with actual service references. - * - * @param ContainerBuilder $container + * {@inheritdoc} */ public function process(ContainerBuilder $container) { - $this->container = $container; - - foreach ($container->getDefinitions() as $definition) { - if ($definition->isSynthetic() || $definition->isAbstract()) { - continue; - } - - $definition->setArguments($this->processArguments($definition->getArguments())); - $definition->setMethodCalls($this->processArguments($definition->getMethodCalls())); - $definition->setProperties($this->processArguments($definition->getProperties())); - if (isset($definition->getChanges()['factory'])) { - $definition->setFactory($this->processFactory($definition->getFactory())); - } - } + parent::process($container); foreach ($container->getAliases() as $id => $alias) { $aliasId = (string) $alias; - if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) { + if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) { $container->setAlias($id, $defId)->setPublic($alias->isPublic() && !$alias->isPrivate())->setPrivate($alias->isPrivate()); } } } /** - * Processes the arguments to replace aliases. - * - * @param array $arguments An array of References - * - * @return array An array of References + * {@inheritdoc} */ - private function processArguments(array $arguments) + protected function processValue($value, $isRoot = false) { - foreach ($arguments as $k => $argument) { - if (is_array($argument)) { - $arguments[$k] = $this->processArguments($argument); - } elseif ($argument instanceof ArgumentInterface) { - $argument->setValues($this->processArguments($argument->getValues())); - } elseif ($argument instanceof Reference) { - $defId = $this->getDefinitionId($id = (string) $argument); + if ($value instanceof Reference) { + $defId = $this->getDefinitionId($id = (string) $value, $this->container); - if ($defId !== $id) { - $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior()); - } + if ($defId !== $id) { + return new Reference($defId, $value->getInvalidBehavior()); } } - return $arguments; - } - - private function processFactory($factory) - { - if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) { - return $factory; - } - - $defId = $this->getDefinitionId($id = (string) $factory[0]); - - if ($defId !== $id) { - $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior()); - } - - return $factory; + return parent::processValue($value); } /** * Resolves an alias into a definition id. * - * @param string $id The definition or alias id to resolve + * @param string $id The definition or alias id to resolve + * @param ContainerBuilder $container * * @return string The definition id with aliases resolved */ - private function getDefinitionId($id) + private function getDefinitionId($id, $container) { $seen = array(); - while ($this->container->hasAlias($id)) { + while ($container->hasAlias($id)) { if (isset($seen[$id])) { throw new ServiceCircularReferenceException($id, array_keys($seen)); } $seen[$id] = true; - $id = (string) $this->container->getAlias($id); + $id = (string) $container->getAlias($id); } return $id;