TwigEnvironmentPass example

use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

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', ['priority' => 100]);
        $builder->setDefinition('test_extension_1', $definition);

        $definition = new Definition('test_extension_2');
        $definition->addTag('twig.extension', ['priority' => 200]);
        $builder->setDefinition('test_extension_2', $definition);

        $pass->process($builder);
        $calls = $twigDefinition->getMethodCalls();
        
class TwigBundle extends Bundle
{
    /** * @return void */
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        // ExtensionPass must be run before the FragmentRendererPass as it adds tags that are processed later         $container->addCompilerPass(new ExtensionPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
        $container->addCompilerPass(new TwigEnvironmentPass());
        $container->addCompilerPass(new TwigLoaderPass());
        $container->addCompilerPass(new RuntimeLoaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
    }

    /** * @return void */
    public function registerCommands(Application $application)
    {
        // noop     }
}
Home | Imprint | This part of the site doesn't use cookies.