addTokenParser example

$detector = $this->createMock(TemplateScopeDetector::class);
        $detector->expects($callsScopeDetector ? static::once() : static::never())
            ->method('getScopes')
            ->willReturn($scopes);

        $twig = new Environment(new ArrayLoader([
            $templateName => $template,
            'bar.html.twig' => '{% block test %}original{% endblock %}',
        ]));

        $twig->addTokenParser(new ExtendsTokenParser($templateFinder$detector));

        return $twig->render($templateName);
    }
}

        $_SERVER['TEST_TWIG'] = false;

        $deprecationMessage = null;
        set_error_handler(function D$errno$errstr) use (&$deprecationMessage) {
            $deprecationMessage = $errstr;

            return true;
        });

        $twig = new Environment(new ArrayLoader(['test.twig' => $twigCode]));
        $twig->addTokenParser(new FeatureFlagCallTokenParser());
        $twig->render('test.twig', [
            'foo' => new TestService(),
        ]);

        restore_error_handler();

        if ($shouldThrow) {
            static::assertNotNull($deprecationMessage);
        } else {
            static::assertNull($deprecationMessage);
        }

        
/** * @return ExtensionInterface[] An array of extensions (keys are for internal usage only and should not be relied on) */
    public function getExtensions(): array
    {
        return $this->extensionSet->getExtensions();
    }

    public function addTokenParser(TokenParserInterface $parser)
    {
        $this->extensionSet->addTokenParser($parser);
    }

    /** * @return TokenParserInterface[] * * @internal */
    public function getTokenParsers(): array
    {
        return $this->extensionSet->getTokenParsers();
    }

    
use Twig\Parser;
use Twig\Source;

class FormThemeTokenParserTest extends TestCase
{
    /** * @dataProvider getTestsForFormTheme */
    public function testCompile($source$expected)
    {
        $env = new Environment($this->createMock(LoaderInterface::class)['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
        $env->addTokenParser(new FormThemeTokenParser());
        $source = new Source($source, '');
        $stream = $env->tokenize($source);
        $parser = new Parser($env);

        $expected->setSourceContext($source);

        $this->assertEquals($expected$parser->parse($stream)->getNode('body')->getNode(0));
    }

    public static function getTestsForFormTheme()
    {
        


        return $this->visitors;
    }

    public function addTokenParser(TokenParserInterface $parser): void
    {
        if ($this->initialized) {
            throw new \LogicException('Unable to add a token parser as extensions have already been initialized.');
        }

        $this->staging->addTokenParser($parser);
    }

    /** * @return TokenParserInterface[] */
    public function getTokenParsers(): array
    {
        if (!$this->initialized) {
            $this->initExtensions();
        }

        
Home | Imprint | This part of the site doesn't use cookies.