diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 67cf39a41bcfb..040ee68a4a582 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.1.0 +----- + + * added priority to Twig extensions + 4.0.0 ----- diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php index f520ab11f0096..918290feabce9 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php @@ -11,7 +11,7 @@ namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; @@ -22,6 +22,8 @@ */ class TwigEnvironmentPass implements CompilerPassInterface { + use PriorityTaggedServiceTrait; + public function process(ContainerBuilder $container) { if (false === $container->hasDefinition('twig')) { @@ -36,8 +38,8 @@ public function process(ContainerBuilder $container) // be registered. $calls = $definition->getMethodCalls(); $definition->setMethodCalls(array()); - foreach ($container->findTaggedServiceIds('twig.extension', true) as $id => $attributes) { - $definition->addMethodCall('addExtension', array(new Reference($id))); + foreach ($this->findAndSortTaggedServices('twig.extension', $container) as $extension) { + $definition->addMethodCall('addExtension', array($extension)); } $definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls)); } diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php new file mode 100644 index 0000000000000..11b5077b41bd3 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler; + +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; + +class TwigEnvironmentPassTest extends TestCase +{ + public function testPassWithTwoExtensionsWithPriority() + { + $twigDefinition = new Definition('twig'); + $twigDefinition->setPublic(true); + $builder = new ContainerBuilder(); + $builder->setDefinition('twig', $twigDefinition); + $pass = new TwigEnvironmentPass(); + + $definition = new Definition('test_extension_1'); + $definition->addTag('twig.extension', array('priority' => 100)); + $builder->setDefinition('test_extension_1', $definition); + + $definition = new Definition('test_extension_2'); + $definition->addTag('twig.extension', array('priority' => 200)); + $builder->setDefinition('test_extension_2', $definition); + + $pass->process($builder); + $calls = $twigDefinition->getMethodCalls(); + $this->assertCount(2, $calls); + $this->assertEquals('addExtension', $calls[0][0]); + $this->assertEquals('addExtension', $calls[1][0]); + $this->assertEquals('test_extension_2', (string) $calls[0][1][0]); + $this->assertEquals('test_extension_1', (string) $calls[1][1][0]); + } +}