diff --git a/src/DependencyInjection/Compiler/TwigStringExtensionCompilerPass.php b/src/DependencyInjection/Compiler/TwigStringExtensionCompilerPass.php new file mode 100644 index 00000000..17a8dd63 --- /dev/null +++ b/src/DependencyInjection/Compiler/TwigStringExtensionCompilerPass.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\NewsBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Twig\Extra\String\StringExtension; + +final class TwigStringExtensionCompilerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + foreach ($container->findTaggedServiceIds('twig.extension') as $id => $attributes) { + if (StringExtension::class === $container->getDefinition($id)->getClass()) { + return; + } + } + + $definition = new Definition(StringExtension::class); + $definition->addTag('twig.extension'); + $container->setDefinition(StringExtension::class, $definition); + } +} diff --git a/src/DependencyInjection/SonataNewsExtension.php b/src/DependencyInjection/SonataNewsExtension.php index f05406bd..9f7064ef 100644 --- a/src/DependencyInjection/SonataNewsExtension.php +++ b/src/DependencyInjection/SonataNewsExtension.php @@ -21,7 +21,6 @@ use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; -use Twig\Extra\String\StringExtension; /** * @author Thomas Rabaix @@ -104,7 +103,6 @@ public function load(array $configs, ContainerBuilder $container) $this->configureClass($config, $container); $this->configureAdmin($config, $container); - $this->configureStringExtension($container); } /** @@ -263,14 +261,4 @@ public function registerDoctrineMapping(array $config) 'orphanRemoval' => false, ]); } - - private function configureStringExtension(ContainerBuilder $container): void - { - if (!$container->hasDefinition('twig.extension.string') || !is_a($container->getDefinition('twig.extension.string')->getClass(), StringExtension::class)) { - $definition = new Definition(StringExtension::class); - $definition->addTag('twig.extension'); - - $container->setDefinition(StringExtension::class, $definition); - } - } } diff --git a/src/SonataNewsBundle.php b/src/SonataNewsBundle.php index edf0d4e2..7a2510a2 100644 --- a/src/SonataNewsBundle.php +++ b/src/SonataNewsBundle.php @@ -14,6 +14,7 @@ namespace Sonata\NewsBundle; use Sonata\CoreBundle\Form\FormHelper; +use Sonata\NewsBundle\DependencyInjection\Compiler\TwigStringExtensionCompilerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -21,6 +22,8 @@ class SonataNewsBundle extends Bundle { public function build(ContainerBuilder $container) { + $container->addCompilerPass(new TwigStringExtensionCompilerPass()); + $this->registerFormMapping(); } diff --git a/tests/DependencyInjection/Compiler/TwigStringExtensionCompilerPassTest.php b/tests/DependencyInjection/Compiler/TwigStringExtensionCompilerPassTest.php new file mode 100644 index 00000000..c5e219fa --- /dev/null +++ b/tests/DependencyInjection/Compiler/TwigStringExtensionCompilerPassTest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\NewsBundle\Tests\DependencyInjection\Compiler; + +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; +use Sonata\NewsBundle\DependencyInjection\Compiler\TwigStringExtensionCompilerPass; +use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Twig\Extra\String\StringExtension; + +class TwigStringExtensionCompilerPassTest extends AbstractCompilerPassTestCase +{ + public function testLoadTwigStringExtension(): void + { + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithTag(StringExtension::class, 'twig.extension'); + } + + public function testLoadTwigStringExtensionWithExtraBundle(): void + { + $definition = new Definition(StringExtension::class); + $definition->addTag('twig.extension'); + $this->container->setDefinition('twig.extension.string', $definition); + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithTag('twig.extension.string', 'twig.extension'); + $this->assertContainerBuilderNotHasService(StringExtension::class); + } + + protected function registerCompilerPass(ContainerBuilder $container): void + { + $container->addCompilerPass(new TwigEnvironmentPass()); + $container->addCompilerPass(new TwigStringExtensionCompilerPass()); + } +} diff --git a/tests/DependencyInjection/SonataNewsExtensionTest.php b/tests/DependencyInjection/SonataNewsExtensionTest.php index d9ada2c6..cc4180bf 100644 --- a/tests/DependencyInjection/SonataNewsExtensionTest.php +++ b/tests/DependencyInjection/SonataNewsExtensionTest.php @@ -22,7 +22,6 @@ use Sonata\NewsBundle\Model\Comment; use Sonata\NewsBundle\Model\Post; use Sonata\NewsBundle\Tests\Fixtures\UserMock; -use Twig\Extra\String\StringExtension; class SonataNewsExtensionTest extends AbstractExtensionTestCase { @@ -67,13 +66,6 @@ public function testLoadWithTagWithCollection(): void $this->assertCount(1, $postOneToManyAssociation, 'The post model should have 1 one to many association (comment)'); } - public function testLoadTwigStringExtension(): void - { - $this->load($this->getConfigurationWithTagWithCollection()); - - $this->assertContainerBuilderHasServiceDefinitionWithTag(StringExtension::class, 'twig.extension'); - } - protected function getMinimalConfiguration(): array { return [