From 6f726575547ff9889257bed9528f9df25c744d7e Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 14 Jun 2016 11:50:57 +0200 Subject: [PATCH] =?UTF-8?q?[DependencyInjection]=C2=A0fix=20the=20sorting?= =?UTF-8?q?=20by=20priority?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Bundle/FrameworkBundle/composer.json | 2 +- .../Compiler/PriorityTaggedServiceTrait.php | 4 +- .../Compiler/PriorityTaggedServiceTrait.php | 85 ------------------- .../PriorityTaggedServiceTraitTest.php | 78 +++++++++++++++++ 4 files changed, 81 insertions(+), 88 deletions(-) delete mode 100644 src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTrait.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index a08e67bd24..2bbdcad1ca 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -20,7 +20,7 @@ "symfony/asset": "~2.8|~3.0", "symfony/cache": "~3.1", "symfony/class-loader": "~2.8|~3.0", - "symfony/dependency-injection": "~3.1", + "symfony/dependency-injection": "~3.2", "symfony/config": "~2.8|~3.0", "symfony/event-dispatcher": "~2.8|~3.0", "symfony/http-foundation": "~3.1", diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 74c6d19581..123bec9995 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -38,10 +38,10 @@ trait PriorityTaggedServiceTrait foreach ($services as $serviceId => $tags) { foreach ($tags as $attributes) { $priority = isset($attributes['priority']) ? $attributes['priority'] : 0; - $queue->insert(new Reference($serviceId), $priority * -1); + $queue->insert(new Reference($serviceId), $priority); } } - return iterator_to_array($queue); + return iterator_to_array($queue, false); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTrait.php deleted file mode 100644 index cb877d7a6c..0000000000 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTrait.php +++ /dev/null @@ -1,85 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\DependencyInjection\Tests\Compiler; - -use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; - -class PriorityTaggedServiceTraitTest extends \PHPUnit_Framework_TestCase -{ - public function testThatCacheWarmersAreProcessedInPriorityOrder() - { - $services = array( - 'my_service1' => array(array('priority' => 100)), - 'my_service2' => array(array('priority' => 200)), - 'my_service3' => array(array('priority' => -500)), - 'my_service4' => array(array()), - 'my_service5' => array(array()), - 'my_service6' => array(array('priority' => -500)), - 'my_service7' => array(array('priority' => -499)), - 'my_service8' => array(array('priority' => 1)), - 'my_service9' => array(array()), - 'my_service10' => array(array('priority' => -1000)), - 'my_service11' => array(array('priority' => -1000)), - 'my_service12' => array(array('priority' => -1000)), - 'my_service13' => array(array('priority' => -1000)), - ); - - $definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); - $container = $this->getMock( - 'Symfony\Component\DependencyInjection\ContainerBuilder', - array('findTaggedServiceIds', 'getDefinition', 'hasDefinition') - ); - - $container - ->expects($this->atLeastOnce()) - ->method('findTaggedServiceIds') - ->will($this->returnValue($services)); - $container - ->expects($this->atLeastOnce()) - ->method('getDefinition') - ->with('my_custom_tag') - ->will($this->returnValue($definition)); - - $definition - ->expects($this->once()) - ->method('replaceArgument') - ->with(0, array( - new Reference('my_service2'), - new Reference('my_service1'), - new Reference('my_service8'), - new Reference('my_service4'), - new Reference('my_service5'), - new Reference('my_service9'), - new Reference('my_service7'), - new Reference('my_service3'), - new Reference('my_service6'), - new Reference('my_service10'), - new Reference('my_service11'), - new Reference('my_service12'), - new Reference('my_service13'), - )); - - (new PriorityTaggedServiceTraitImplementation())->test('my_custom_tag', $container); - } -} - -class PriorityTaggedServiceTraitImplementation -{ - use PriorityTaggedServiceTrait; - - public function test($tagName, ContainerBuilder $container) - { - return $this->findAndSortTaggedServices($tagName, $container); - } -} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php new file mode 100644 index 0000000000..4150720509 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +class PriorityTaggedServiceTraitTest extends \PHPUnit_Framework_TestCase +{ + public function testThatCacheWarmersAreProcessedInPriorityOrder() + { + $services = array( + 'my_service1' => array('my_custom_tag' => array('priority' => 100)), + 'my_service2' => array('my_custom_tag' => array('priority' => 200)), + 'my_service3' => array('my_custom_tag' => array('priority' => -501)), + 'my_service4' => array('my_custom_tag' => array()), + 'my_service5' => array('my_custom_tag' => array('priority' => -1)), + 'my_service6' => array('my_custom_tag' => array('priority' => -500)), + 'my_service7' => array('my_custom_tag' => array('priority' => -499)), + 'my_service8' => array('my_custom_tag' => array('priority' => 1)), + 'my_service9' => array('my_custom_tag' => array('priority' => -2)), + 'my_service10' => array('my_custom_tag' => array('priority' => -1000)), + '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)), + ); + + $container = new ContainerBuilder(); + + foreach ($services as $id => $tags) { + $definition = $container->register($id); + + foreach ($tags as $name => $attributes) { + $definition->addTag($name, $attributes); + } + } + + $expected = array( + new Reference('my_service2'), + new Reference('my_service1'), + new Reference('my_service8'), + new Reference('my_service4'), + new Reference('my_service5'), + new Reference('my_service9'), + new Reference('my_service7'), + new Reference('my_service6'), + new Reference('my_service3'), + new Reference('my_service10'), + new Reference('my_service11'), + new Reference('my_service12'), + new Reference('my_service13'), + ); + + $priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation(); + + $this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test('my_custom_tag', $container)); + } +} + +class PriorityTaggedServiceTraitImplementation +{ + use PriorityTaggedServiceTrait; + + public function test($tagName, ContainerBuilder $container) + { + return $this->findAndSortTaggedServices($tagName, $container); + } +}