diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php index c34f958393..bc3b71c696 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php @@ -40,9 +40,24 @@ class TwigLoaderPass implements CompilerPassInterface $container->setAlias('twig.loader', key($loaderIds)); } else { $chainLoader = $container->getDefinition('twig.loader.chain'); - foreach (array_keys($loaderIds) as $id) { - $chainLoader->addMethodCall('addLoader', array(new Reference($id))); + + $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) { + foreach ($loaders as $loader) { + $chainLoader->addMethodCall('addLoader', array(new Reference($loader))); + } + } + $container->setAlias('twig.loader', 'twig.loader.chain'); } } diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigLoaderPassTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigLoaderPassTest.php index 7f3f52fba0..6bf5cd9baa 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigLoaderPassTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigLoaderPassTest.php @@ -43,6 +43,7 @@ class TwigLoaderPassTest extends \PHPUnit_Framework_TestCase { $serviceIds = array( 'test_loader_1' => array( + array(), ), ); @@ -65,8 +66,10 @@ class TwigLoaderPassTest extends \PHPUnit_Framework_TestCase { $serviceIds = array( 'test_loader_1' => array( + array(), ), 'test_loader_2' => array( + array(), ), ); @@ -90,6 +93,45 @@ class TwigLoaderPassTest extends \PHPUnit_Framework_TestCase $calls = $this->chainLoader->getMethodCalls(); $this->assertCount(2, $calls); $this->assertEquals('addLoader', $calls[0][0]); + $this->assertEquals('addLoader', $calls[1][0]); + $this->assertEquals('test_loader_1', (string) $calls[0][1][0]); + $this->assertEquals('test_loader_2', (string) $calls[1][1][0]); + } + + public function testMapperPassWithTwoTaggedLoadersWithPriority() + { + $serviceIds = array( + 'test_loader_1' => array( + array('priority' => 100), + ), + 'test_loader_2' => array( + array('priority' => 200), + ), + ); + + $this->builder->expects($this->once()) + ->method('hasDefinition') + ->with('twig') + ->will($this->returnValue(true)); + $this->builder->expects($this->once()) + ->method('findTaggedServiceIds') + ->with('twig.loader') + ->will($this->returnValue($serviceIds)); + $this->builder->expects($this->once()) + ->method('getDefinition') + ->with('twig.loader.chain') + ->will($this->returnValue($this->chainLoader)); + $this->builder->expects($this->once()) + ->method('setAlias') + ->with('twig.loader', 'twig.loader.chain'); + + $this->pass->process($this->builder); + $calls = $this->chainLoader->getMethodCalls(); + $this->assertCount(2, $calls); + $this->assertEquals('addLoader', $calls[0][0]); + $this->assertEquals('addLoader', $calls[1][0]); + $this->assertEquals('test_loader_2', (string) $calls[0][1][0]); + $this->assertEquals('test_loader_1', (string) $calls[1][1][0]); } /**