Skip to content

Commit d87af71

Browse files
Bruntyfabpot
authored andcommitted
[TwigBundle] Added priority to twig extensions
1 parent 9e73cc7 commit d87af71

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* added priority to Twig extensions
8+
49
4.0.0
510
-----
611

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\Reference;
14+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1717

@@ -22,6 +22,8 @@
2222
*/
2323
class TwigEnvironmentPass implements CompilerPassInterface
2424
{
25+
use PriorityTaggedServiceTrait;
26+
2527
public function process(ContainerBuilder $container)
2628
{
2729
if (false === $container->hasDefinition('twig')) {
@@ -36,8 +38,8 @@ public function process(ContainerBuilder $container)
3638
// be registered.
3739
$calls = $definition->getMethodCalls();
3840
$definition->setMethodCalls(array());
39-
foreach ($container->findTaggedServiceIds('twig.extension', true) as $id => $attributes) {
40-
$definition->addMethodCall('addExtension', array(new Reference($id)));
41+
foreach ($this->findAndSortTaggedServices('twig.extension', $container) as $extension) {
42+
$definition->addMethodCall('addExtension', array($extension));
4143
}
4244
$definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls));
4345
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
19+
class TwigEnvironmentPassTest extends TestCase
20+
{
21+
public function testPassWithTwoExtensionsWithPriority()
22+
{
23+
$twigDefinition = new Definition('twig');
24+
$twigDefinition->setPublic(true);
25+
$builder = new ContainerBuilder();
26+
$builder->setDefinition('twig', $twigDefinition);
27+
$pass = new TwigEnvironmentPass();
28+
29+
$definition = new Definition('test_extension_1');
30+
$definition->addTag('twig.extension', array('priority' => 100));
31+
$builder->setDefinition('test_extension_1', $definition);
32+
33+
$definition = new Definition('test_extension_2');
34+
$definition->addTag('twig.extension', array('priority' => 200));
35+
$builder->setDefinition('test_extension_2', $definition);
36+
37+
$pass->process($builder);
38+
$calls = $twigDefinition->getMethodCalls();
39+
$this->assertCount(2, $calls);
40+
$this->assertEquals('addExtension', $calls[0][0]);
41+
$this->assertEquals('addExtension', $calls[1][0]);
42+
$this->assertEquals('test_extension_2', (string) $calls[0][1][0]);
43+
$this->assertEquals('test_extension_1', (string) $calls[1][1][0]);
44+
}
45+
}

0 commit comments

Comments
 (0)