Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. GuardTokenParser.php

class GuardTokenParser

@internal

Hierarchy

  • class \Twig\TokenParser\AbstractTokenParser implements \Twig\TokenParser\TokenParserInterface
    • class \Twig\TokenParser\GuardTokenParser extends \Twig\TokenParser\AbstractTokenParser

Expanded class hierarchy of GuardTokenParser

1 file declares its use of GuardTokenParser
CoreExtension.php in vendor/twig/twig/src/Extension/CoreExtension.php

File

vendor/twig/twig/src/TokenParser/GuardTokenParser.php, line 23

Namespace

Twig\TokenParser
View source
final class GuardTokenParser extends AbstractTokenParser {
    public function parse(Token $token) : Node {
        $stream = $this->parser
            ->getStream();
        $typeToken = $stream->expect(Token::NAME_TYPE);
        if (!in_array($typeToken->getValue(), [
            'function',
            'filter',
            'test',
        ])) {
            throw new SyntaxError(\sprintf('Supported guard types are function, filter and test, "%s" given.', $typeToken->getValue()), $typeToken->getLine(), $stream->getSourceContext());
        }
        $method = 'get' . $typeToken->getValue();
        $nameToken = $stream->expect(Token::NAME_TYPE);
        $exists = null !== $this->parser
            ->getEnvironment()
            ->{$method}($nameToken->getValue());
        $stream->expect(Token::BLOCK_END_TYPE);
        if ($exists) {
            $body = $this->parser
                ->subparse([
                $this,
                'decideGuardFork',
            ]);
        }
        else {
            $body = new EmptyNode();
            $this->parser
                ->subparseIgnoreUnknownTwigCallables([
                $this,
                'decideGuardFork',
            ]);
        }
        $else = new EmptyNode();
        if ('else' === $stream->next()
            ->getValue()) {
            $stream->expect(Token::BLOCK_END_TYPE);
            $else = $this->parser
                ->subparse([
                $this,
                'decideGuardEnd',
            ], true);
        }
        $stream->expect(Token::BLOCK_END_TYPE);
        return new Nodes([
            $exists ? $body : $else,
        ]);
    }
    public function decideGuardFork(Token $token) : bool {
        return $token->test([
            'else',
            'endguard',
        ]);
    }
    public function decideGuardEnd(Token $token) : bool {
        return $token->test([
            'endguard',
        ]);
    }
    public function getTag() : string {
        return 'guard';
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractTokenParser::$parser protected property
AbstractTokenParser::setParser public function Sets the parser associated with this token parser. Overrides TokenParserInterface::setParser
GuardTokenParser::decideGuardEnd public function
GuardTokenParser::decideGuardFork public function
GuardTokenParser::getTag public function Gets the tag name associated with this token parser. Overrides TokenParserInterface::getTag
GuardTokenParser::parse public function Parses a token and returns a node. Overrides TokenParserInterface::parse
RSS feed
Powered by Drupal