From aac9a7edfd4960395bebf2cc400ad728315cbfe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pluchino?= Date: Tue, 20 Dec 2016 14:31:21 +0100 Subject: [PATCH] Fix the priority order of compiler pass trait --- .../Compiler/PriorityTaggedServiceTrait.php | 22 ++++++++++++++----- .../PriorityTaggedServiceTraitTest.php | 12 ++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 123bec999575..83f6014c3935 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -24,6 +24,13 @@ trait PriorityTaggedServiceTrait /** * Finds all services with the given tag name and order them by their priority. * + * The order of additions must be respected for services having the same priority, + * and knowing that the \SplPriorityQueue class does not respect the FIFO method, + * we should not use this class. + * + * @see https://bugs.php.net/bug.php?id=53710 + * @see https://bugs.php.net/bug.php?id=60926 + * * @param string $tagName * @param ContainerBuilder $container * @@ -31,17 +38,20 @@ trait PriorityTaggedServiceTrait */ private function findAndSortTaggedServices($tagName, ContainerBuilder $container) { - $services = $container->findTaggedServiceIds($tagName); - - $queue = new \SplPriorityQueue(); + $services = array(); - foreach ($services as $serviceId => $tags) { + foreach ($container->findTaggedServiceIds($tagName) as $serviceId => $tags) { foreach ($tags as $attributes) { $priority = isset($attributes['priority']) ? $attributes['priority'] : 0; - $queue->insert(new Reference($serviceId), $priority); + $services[$priority][] = new Reference($serviceId); } } - return iterator_to_array($queue, false); + if ($services) { + krsort($services); + $services = call_user_func_array('array_merge', $services); + } + + return $services; } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php index 4150720509b5..386c2598a883 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php @@ -33,6 +33,12 @@ public function testThatCacheWarmersAreProcessedInPriorityOrder() 'my_service11' => array('my_custom_tag' => array('priority' => -1001)), 'my_service12' => array('my_custom_tag' => array('priority' => -1002)), 'my_service13' => array('my_custom_tag' => array('priority' => -1003)), + 'my_service14' => array('my_custom_tag' => array('priority' => -1000)), + 'my_service15' => array('my_custom_tag' => array('priority' => 1)), + 'my_service16' => array('my_custom_tag' => array('priority' => -1)), + 'my_service17' => array('my_custom_tag' => array('priority' => 200)), + 'my_service18' => array('my_custom_tag' => array('priority' => 100)), + 'my_service19' => array('my_custom_tag' => array()), ); $container = new ContainerBuilder(); @@ -47,15 +53,21 @@ public function testThatCacheWarmersAreProcessedInPriorityOrder() $expected = array( new Reference('my_service2'), + new Reference('my_service17'), new Reference('my_service1'), + new Reference('my_service18'), new Reference('my_service8'), + new Reference('my_service15'), new Reference('my_service4'), + new Reference('my_service19'), new Reference('my_service5'), + new Reference('my_service16'), new Reference('my_service9'), new Reference('my_service7'), new Reference('my_service6'), new Reference('my_service3'), new Reference('my_service10'), + new Reference('my_service14'), new Reference('my_service11'), new Reference('my_service12'), new Reference('my_service13'),