Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 49f8b73

Browse filesBrowse files
committed
feature #24777 [TwigBundle] Added priority to twig extensions (Brunty)
This PR was squashed before being merged into the 4.1-dev branch (closes #24777). Discussion ---------- [TwigBundle] Added priority to twig extensions | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Don't merge before the docs Added priority to twig extensions in the `TwigEnvironmentPass` to control the order in which they're registered, similar to the `TwigLoaderPass` Though, unsure on what priority to use as a default, and will PR docs when finalised. Commits ------- d87af71 [TwigBundle] Added priority to twig extensions
2 parents 26e21d1 + d87af71 commit 49f8b73
Copy full SHA for 49f8b73

File tree

3 files changed

+55
-3
lines changed
Filter options

3 files changed

+55
-3
lines changed

‎src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/CHANGELOG.md
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php
+5-3Lines 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
}
+45Lines 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 <fabien@symfony.com>
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.