From 96585561a9e91606119cfb1e65905ff5fd4540a0 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Tue, 31 Oct 2017 21:52:50 +0000 Subject: [PATCH 01/12] [TwigBundle] Added priority to twig extensions --- .../Compiler/TwigEnvironmentPass.php | 14 +++- .../Compiler/TwigEnvironmentPassTest.php | 72 +++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php index f520ab11f0096..0aca910b2faf3 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php @@ -28,7 +28,15 @@ public function process(ContainerBuilder $container) return; } + $prioritizedLoaders = array(); + + foreach ($container->findTaggedServiceIds('twig.extension', true) as $id => $attributes) { + $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; + $prioritizedLoaders[$priority][] = $id; + } + $definition = $container->getDefinition('twig'); + krsort($prioritizedLoaders); // Extensions must always be registered before everything else. // For instance, global variable definitions must be registered @@ -36,8 +44,10 @@ 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 ($prioritizedLoaders as $loaders) { + foreach ($loaders as $loader) { + $definition->addMethodCall('addExtension', array(new Reference($loader))); + } } $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..fddc7a17e2a5f --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php @@ -0,0 +1,72 @@ + + * + * 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\Definition; + +class TwigEnvironmentPassTest extends TestCase +{ + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $builder; + /** + * @var Definition + */ + private $definition; + /** + * @var TwigEnvironmentPass + */ + private $pass; + + protected function setUp() + { + $this->builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'setAlias', 'getDefinition'))->getMock(); + $this->definition = new Definition('twig'); + $this->pass = new TwigEnvironmentPass(); + } + + public function testPassWithTwoExtensionsWithPriority() + { + $serviceIds = array( + 'test_extension_1' => array( + array('priority' => 100), + ), + 'test_extension_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.extension') + ->will($this->returnValue($serviceIds)); + $this->builder->expects($this->once()) + ->method('getDefinition') + ->with('twig') + ->will($this->returnValue($this->definition)); + + $this->pass->process($this->builder); + $calls = $this->definition->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]); + } +} From 65cc813fe6fa89f30a014345441165942c87f061 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Wed, 1 Nov 2017 12:47:51 +0000 Subject: [PATCH 02/12] [TwigBundle] Refactor to PriorityTaggedServiceTrait --- .../Compiler/TwigEnvironmentPass.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php index 0aca910b2faf3..1b3391e676562 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler; +use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; @@ -22,21 +23,15 @@ */ class TwigEnvironmentPass implements CompilerPassInterface { + use PriorityTaggedServiceTrait; + public function process(ContainerBuilder $container) { if (false === $container->hasDefinition('twig')) { return; } - $prioritizedLoaders = array(); - - foreach ($container->findTaggedServiceIds('twig.extension', true) as $id => $attributes) { - $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; - $prioritizedLoaders[$priority][] = $id; - } - $definition = $container->getDefinition('twig'); - krsort($prioritizedLoaders); // Extensions must always be registered before everything else. // For instance, global variable definitions must be registered @@ -44,10 +39,8 @@ public function process(ContainerBuilder $container) // be registered. $calls = $definition->getMethodCalls(); $definition->setMethodCalls(array()); - foreach ($prioritizedLoaders as $loaders) { - foreach ($loaders as $loader) { - $definition->addMethodCall('addExtension', array(new Reference($loader))); - } + foreach ($this->findAndSortTaggedServices('twig.extension', $container) as $extension) { + $definition->addMethodCall('addExtension', array(new Reference($extension))); } $definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls)); } From 0d77f4926ab7c117dbc02cb0514c292aeacb26e4 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Wed, 1 Nov 2017 18:40:53 +0000 Subject: [PATCH 03/12] [TwigBundle] Refactor extension variable as already a reference --- .../DependencyInjection/Compiler/TwigEnvironmentPass.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php index 1b3391e676562..c175ad99dd24d 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php @@ -40,7 +40,7 @@ public function process(ContainerBuilder $container) $calls = $definition->getMethodCalls(); $definition->setMethodCalls(array()); foreach ($this->findAndSortTaggedServices('twig.extension', $container) as $extension) { - $definition->addMethodCall('addExtension', array(new Reference($extension))); + $definition->addMethodCall('addExtension', array($extension)); } $definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls)); } From 54d30b12688a8f01744e802dd46d875a43e6bc37 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Thu, 2 Nov 2017 00:00:29 +0000 Subject: [PATCH 04/12] [TwigBundle] Remove unused use statement --- .../DependencyInjection/Compiler/TwigEnvironmentPass.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php index c175ad99dd24d..918290feabce9 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigEnvironmentPass.php @@ -12,7 +12,6 @@ namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; From 3d5a5458495fca6e13bfc1171a54e3af925e5fe6 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Sat, 18 Nov 2017 10:58:03 +0000 Subject: [PATCH 05/12] [TwigBundle] Add changelog entry for priority on extensions --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 67cf39a41bcfb..0fb31b1fbcc36 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * removed `ContainerAwareRuntimeLoader` + * added priority to twig extensions 3.4.0 ----- From 1cf78d2bc54d4b83704723c35604d25459af09be Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Fri, 29 Dec 2017 22:18:38 +0000 Subject: [PATCH 06/12] [TwigBundle] Move changelog entry to 4.1.0 --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 0fb31b1fbcc36..370929201f45e 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -1,11 +1,14 @@ CHANGELOG ========= +4.1.0 +----- + * added priority to twig extensions + 4.0.0 ----- * removed `ContainerAwareRuntimeLoader` - * added priority to twig extensions 3.4.0 ----- From a22d71309b237d6dace438b826a05b5edc4af826 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Tue, 2 Jan 2018 13:35:03 +0000 Subject: [PATCH 07/12] [TwigBundle] Fix capitalisation in changelog --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 370929201f45e..94b2f8db46d75 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -3,7 +3,7 @@ CHANGELOG 4.1.0 ----- - * added priority to twig extensions + * added priority to Twig extensions 4.0.0 ----- From 65f4e891eb8f71ba3f3fe424e9741b8d9c6bbef9 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Tue, 2 Jan 2018 13:35:17 +0000 Subject: [PATCH 08/12] [TwigBundle] Remove mock container builder in test --- .../Compiler/TwigEnvironmentPassTest.php | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php index fddc7a17e2a5f..bc76a7fc3e5d9 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php @@ -13,12 +13,13 @@ 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 { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var ContainerBuilder */ private $builder; /** @@ -32,34 +33,23 @@ class TwigEnvironmentPassTest extends TestCase protected function setUp() { - $this->builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'setAlias', 'getDefinition'))->getMock(); + $this->builder = new ContainerBuilder; $this->definition = new Definition('twig'); + $this->definition->setPublic(true); + $this->builder->setDefinition('twig', $this->definition); + $this->pass = new TwigEnvironmentPass(); } public function testPassWithTwoExtensionsWithPriority() { - $serviceIds = array( - 'test_extension_1' => array( - array('priority' => 100), - ), - 'test_extension_2' => array( - array('priority' => 200), - ), - ); + $definition = new Definition('test_extension_1'); + $definition->addTag('twig.extension', ['priority' => 100]); + $this->builder->setDefinition('test_extension_1', $definition); - $this->builder->expects($this->once()) - ->method('hasDefinition') - ->with('twig') - ->will($this->returnValue(true)); - $this->builder->expects($this->once()) - ->method('findTaggedServiceIds') - ->with('twig.extension') - ->will($this->returnValue($serviceIds)); - $this->builder->expects($this->once()) - ->method('getDefinition') - ->with('twig') - ->will($this->returnValue($this->definition)); + $definition = new Definition('test_extension_2'); + $definition->addTag('twig.extension', ['priority' => 200]); + $this->builder->setDefinition('test_extension_2', $definition); $this->pass->process($this->builder); $calls = $this->definition->getMethodCalls(); From a00ffadcc03a3d5dc402721c1c08a264b2a0444a Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Tue, 2 Jan 2018 13:38:15 +0000 Subject: [PATCH 09/12] [TwigBundle] Fix code style errors --- .../Compiler/TwigEnvironmentPassTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php index bc76a7fc3e5d9..7fe946adc0a98 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php @@ -33,7 +33,7 @@ class TwigEnvironmentPassTest extends TestCase protected function setUp() { - $this->builder = new ContainerBuilder; + $this->builder = new ContainerBuilder(); $this->definition = new Definition('twig'); $this->definition->setPublic(true); $this->builder->setDefinition('twig', $this->definition); @@ -44,11 +44,11 @@ protected function setUp() public function testPassWithTwoExtensionsWithPriority() { $definition = new Definition('test_extension_1'); - $definition->addTag('twig.extension', ['priority' => 100]); + $definition->addTag('twig.extension', array('priority' => 100)); $this->builder->setDefinition('test_extension_1', $definition); $definition = new Definition('test_extension_2'); - $definition->addTag('twig.extension', ['priority' => 200]); + $definition->addTag('twig.extension', array('priority' => 200)); $this->builder->setDefinition('test_extension_2', $definition); $this->pass->process($this->builder); From ff2aba2bb52ad4336bba112ddd963fe900dd0053 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Tue, 16 Jan 2018 21:53:52 +0000 Subject: [PATCH 10/12] [TwigBundle] Update changelog for newline formatting --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 94b2f8db46d75..040ee68a4a582 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -3,6 +3,7 @@ CHANGELOG 4.1.0 ----- + * added priority to Twig extensions 4.0.0 From 9e8d1f5107cc39a2ede42029cba0d4c74fcbe5b2 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Tue, 16 Jan 2018 21:54:31 +0000 Subject: [PATCH 11/12] [TwigBundle] Remove TwigEngironmentPassTest setUp method Moves the properties on the test class to variables inside the single test method it has. --- .../Compiler/TwigEnvironmentPassTest.php | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php index 7fe946adc0a98..69dd2980fa0a3 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php @@ -18,41 +18,25 @@ class TwigEnvironmentPassTest extends TestCase { - /** - * @var ContainerBuilder - */ - private $builder; - /** - * @var Definition - */ - private $definition; - /** - * @var TwigEnvironmentPass - */ - private $pass; - - protected function setUp() - { - $this->builder = new ContainerBuilder(); - $this->definition = new Definition('twig'); - $this->definition->setPublic(true); - $this->builder->setDefinition('twig', $this->definition); - - $this->pass = new TwigEnvironmentPass(); - } 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)); - $this->builder->setDefinition('test_extension_1', $definition); + $builder->setDefinition('test_extension_1', $definition); $definition = new Definition('test_extension_2'); $definition->addTag('twig.extension', array('priority' => 200)); - $this->builder->setDefinition('test_extension_2', $definition); + $builder->setDefinition('test_extension_2', $definition); - $this->pass->process($this->builder); - $calls = $this->definition->getMethodCalls(); + $pass->process($builder); + $calls = $twigDefinition->getMethodCalls(); $this->assertCount(2, $calls); $this->assertEquals('addExtension', $calls[0][0]); $this->assertEquals('addExtension', $calls[1][0]); From 5399083ad3fdd6850955f2f2b246d14df416ff30 Mon Sep 17 00:00:00 2001 From: Matt Brunt Date: Tue, 16 Jan 2018 21:59:32 +0000 Subject: [PATCH 12/12] [TwigBundle] Fix code style in TwigEnvironmentPassTest --- .../DependencyInjection/Compiler/TwigEnvironmentPassTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php index 69dd2980fa0a3..11b5077b41bd3 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigEnvironmentPassTest.php @@ -18,7 +18,6 @@ class TwigEnvironmentPassTest extends TestCase { - public function testPassWithTwoExtensionsWithPriority() { $twigDefinition = new Definition('twig');