8000 [TwigBundle] Added priority to twig extensions by Brunty · Pull Request #24777 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Added priority to twig extensions #24777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

8000
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
-----

* added priority to Twig extensions

4.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,6 +22,8 @@
*/
class TwigEnvironmentPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;

public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('twig')) {
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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]);
}
}
0