From 6764dcdf390c2fc68abe53d74629c1942d625c68 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 12 Apr 2017 14:44:35 +0200 Subject: [PATCH 1/2] Prevent double registrations related to tag priorities --- .../Compiler/SerializerPass.php | 8 +++--- .../Compiler/SerializerPassTest.php | 6 ++--- .../Compiler/TwigLoaderPass.php | 26 ++++++++----------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index aa449dd4c8..21bc2dac30 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -47,11 +47,9 @@ class SerializerPass implements CompilerPassInterface } $sortedServices = array(); - foreach ($services as $serviceId => $tags) { - foreach ($tags as $tag) { - $priority = isset($tag['priority']) ? $tag['priority'] : 0; - $sortedServices[$priority][] = new Reference($serviceId); - } + foreach ($services as $serviceId => $attributes) { + $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; + $sortedServices[$priority][] = new Reference($serviceId); } krsort($sortedServices); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php index 0df5bfd7cd..6fa27be17a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php @@ -72,9 +72,9 @@ class SerializerPassTest extends TestCase public function testServicesAreOrderedAccordingToPriority() { $services = array( - 'n3' => array('tag' => array()), - 'n1' => array('tag' => array('priority' => 200)), - 'n2' => array('tag' => array('priority' => 100)), + 'n3' => array(array()), + 'n1' => array(array('priority' => 200)), + 'n2' => array(array('priority' => 100)), ); $expected = array( diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php index bc3b71c696..fc670e7cd1 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php @@ -29,27 +29,23 @@ class TwigLoaderPass implements CompilerPassInterface return; } - // register additional template loaders - $loaderIds = $container->findTaggedServiceIds('twig.loader'); + $prioritizedLoaders = array(); + $found = 0; - if (count($loaderIds) === 0) { + foreach ($container->findTaggedServiceIds('twig.loader') as $id => $attributes) { + $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; + $prioritizedLoaders[$priority][] = $id; + ++$found; + } + + if (!$found) { throw new LogicException('No twig loaders found. You need to tag at least one loader with "twig.loader"'); } - if (count($loaderIds) === 1) { - $container->setAlias('twig.loader', key($loaderIds)); + if (1 === $found) { + $container->setAlias('twig.loader', $id); } else { $chainLoader = $container->getDefinition('twig.loader.chain'); - - $prioritizedLoaders = array(); - - foreach ($loaderIds as $id => $tags) { - foreach ($tags as $tag) { - $priority = isset($tag['priority']) ? $tag['priority'] : 0; - $prioritizedLoaders[$priority][] = $id; - } - } - krsort($prioritizedLoaders); foreach ($prioritizedLoaders as $loaders) { From 2be582174375e6377af8504560624f840a98580d Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 12 Apr 2017 17:21:47 +0200 Subject: [PATCH 2/2] [2.8] Prevent double registrations related to tag priorities --- .../DependencyInjection/Compiler/PropertyInfoPass.php | 8 +++----- .../DependencyInjection/Compiler/PropertyInfoPassTest.php | 6 +++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/PropertyInfoPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/PropertyInfoPass.php index 819827ac8c..589af52fd6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/PropertyInfoPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/PropertyInfoPass.php @@ -57,11 +57,9 @@ class PropertyInfoPass implements CompilerPassInterface $services = $container->findTaggedServiceIds($tagName); $sortedServices = array(); - foreach ($services as $serviceId => $tags) { - foreach ($tags as $attributes) { - $priority = isset($attributes['priority']) ? $attributes['priority'] : 0; - $sortedServices[$priority][] = new Reference($serviceId); - } + foreach ($services as $serviceId => $attributes) { + $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; + $sortedServices[$priority][] = new Reference($serviceId); } if (empty($sortedServices)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/PropertyInfoPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/PropertyInfoPassTest.php index 46b7b57102..a7801e0f69 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/PropertyInfoPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/PropertyInfoPassTest.php @@ -20,9 +20,9 @@ class PropertyInfoPassTest extends TestCase public function testServicesAreOrderedAccordingToPriority() { $services = array( - 'n3' => array('tag' => array()), - 'n1' => array('tag' => array('priority' => 200)), - 'n2' => array('tag' => array('priority' => 100)), + 'n3' => array(array()), + 'n1' => array(array('priority' => 200)), + 'n2' => array(array('priority' => 100)), ); $expected = array(