getTokenParsers example


        $container = $this->createContainer();
        $container->setParameter('kernel.debug', $debug);
        if ($stopwatchEnabled) {
            $container->register('debug.stopwatch', Stopwatch::class);
        }
        $container->registerExtension(new TwigExtension());
        $container->loadFromExtension('twig');
        $container->setAlias('test.twig.extension.debug.stopwatch', 'twig.extension.debug.stopwatch')->setPublic(true);
        $this->compileContainer($container);

        $tokenParsers = $container->get('test.twig.extension.debug.stopwatch')->getTokenParsers();
        $stopwatchIsAvailable = new \ReflectionProperty($tokenParsers[0], 'stopwatchIsAvailable');

        $this->assertSame($expected$stopwatchIsAvailable->getValue($tokenParsers[0]));
    }

    public static function stopwatchExtensionAvailabilityProvider()
    {
        return [
            'debug-and-stopwatch-enabled' => [true, true, true],
            'only-stopwatch-enabled' => [false, true, false],
            'only-debug-enabled' => [true, false, false],
            


                    if (!$subparser = $this->env->getTokenParser($token->getValue())) {
                        if (null !== $test) {
                            $e = new SyntaxError(sprintf('Unexpected "%s" tag', $token->getValue())$token->getLine()$this->stream->getSourceContext());

                            if (\is_array($test) && isset($test[0]) && $test[0] instanceof TokenParserInterface) {
                                $e->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag()$lineno));
                            }
                        } else {
                            $e = new SyntaxError(sprintf('Unknown "%s" tag.', $token->getValue())$token->getLine()$this->stream->getSourceContext());
                            $e->addSuggestions($token->getValue()array_keys($this->env->getTokenParsers()));
                        }

                        throw $e;
                    }

                    $this->stream->next();

                    $subparser->setParser($this);
                    $node = $subparser->parse($token);
                    if (null !== $node) {
                        $rv[] = $node;
                    }

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

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

    /** * @internal */
    public function getTokenParser(string $name): ?TokenParserInterface
    {
        return $this->extensionSet->getTokenParser($name);
    }

    public function registerUndefinedTokenParserCallback(callable $callable): void
    {

class NodeExtensionTest extends TestCase
{
    public function testGetTokenParsers(): void
    {
        $extension = new NodeExtension(
            $this->createMock(TemplateFinder::class),
            $this->createMock(TemplateScopeDetector::class),
        );
        static::assertCount(3, $extension->getTokenParsers());
        static::assertSame([
            ExtendsTokenParser::class,
            IncludeTokenParser::class,
            ReturnNodeTokenParser::class,
        ]array_map(fn (TokenParserInterface $parser) => $parser::class$extension->getTokenParsers()));
    }

    public function testGetFinder(): void
    {
        $finder = $this->createMock(TemplateFinder::class);
        $extension = new NodeExtension(
            
// functions         foreach ($extension->getFunctions() as $function) {
            $this->functions[$function->getName()] = $function;
        }

        // tests         foreach ($extension->getTests() as $test) {
            $this->tests[$test->getName()] = $test;
        }

        // token parsers         foreach ($extension->getTokenParsers() as $parser) {
            if (!$parser instanceof TokenParserInterface) {
                throw new \LogicException('getTokenParsers() must return an array of \Twig\TokenParser\TokenParserInterface.');
            }

            $this->parsers[$parser->getTag()] = $parser;
        }

        // node visitors         foreach ($extension->getNodeVisitors() as $visitor) {
            $this->visitors[] = $visitor;
        }

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