leilukin-tumbleblog/includes/lib/Twig/TokenParser/DeprecatedTokenParser.php

67 lines
1.9 KiB
PHP
Raw Normal View History

2024-06-20 14:10:42 +00:00
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\TokenParser;
2024-09-05 17:51:48 +00:00
use Twig\Error\SyntaxError;
2024-06-20 14:10:42 +00:00
use Twig\Node\DeprecatedNode;
use Twig\Node\Node;
use Twig\Token;
/**
* Deprecates a section of a template.
*
* {% deprecated 'The "base.twig" template is deprecated, use "layout.twig" instead.' %}
* {% extends 'layout.html.twig' %}
*
2024-09-05 17:51:48 +00:00
* {% deprecated 'The "base.twig" template is deprecated, use "layout.twig" instead.' package="foo/bar" version="1.1" %}
*
2024-06-20 14:10:42 +00:00
* @author Yonel Ceruto <yonelceruto@gmail.com>
*
* @internal
*/
final class DeprecatedTokenParser extends AbstractTokenParser
{
public function parse(Token $token): Node
{
2024-09-05 17:51:48 +00:00
$stream = $this->parser->getStream();
$expressionParser = $this->parser->getExpressionParser();
$expr = $expressionParser->parseExpression();
$node = new DeprecatedNode($expr, $token->getLine(), $this->getTag());
while ($stream->test(Token::NAME_TYPE)) {
$k = $stream->getCurrent()->getValue();
$stream->next();
$stream->expect(Token::OPERATOR_TYPE, '=');
switch ($k) {
case 'package':
$node->setNode('package', $expressionParser->parseExpression());
break;
case 'version':
$node->setNode('version', $expressionParser->parseExpression());
break;
default:
throw new SyntaxError(\sprintf('Unknown "%s" option.', $k), $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
}
2024-06-20 14:10:42 +00:00
2024-09-05 17:51:48 +00:00
$stream->expect(Token::BLOCK_END_TYPE);
2024-06-20 14:10:42 +00:00
2024-09-05 17:51:48 +00:00
return $node;
2024-06-20 14:10:42 +00:00
}
public function getTag(): string
{
return 'deprecated';
}
}