function GuardTokenParser::parse
Overrides TokenParserInterface::parse
File
-
vendor/
twig/ twig/ src/ TokenParser/ GuardTokenParser.php, line 25
Class
- GuardTokenParser
- @internal
Namespace
Twig\TokenParserCode
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,
]);
}