leilukin-tumbleblog/includes/lib/Twig/Node/Expression/TempNameExpression.php

50 lines
1.4 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\Node\Expression;
use Twig\Compiler;
2025-01-13 09:56:01 +00:00
use Twig\Error\SyntaxError;
2024-06-20 14:10:42 +00:00
class TempNameExpression extends AbstractExpression
{
2025-01-13 09:56:01 +00:00
public const RESERVED_NAMES = ['varargs', 'context', 'macros', 'blocks', 'this'];
public function __construct(string|int|null $name, int $lineno)
2024-06-20 14:10:42 +00:00
{
2025-01-13 09:56:01 +00:00
// All names supported by ExpressionParser::parsePrimaryExpression() should be excluded
if ($name && \in_array(strtolower($name), ['true', 'false', 'none', 'null'])) {
throw new SyntaxError(\sprintf('You cannot assign a value to "%s".', $name), $lineno);
}
if (self::class === static::class) {
trigger_deprecation('twig/twig', '3.15', 'The "%s" class is deprecated.', self::class);
}
if (null !== $name && (\is_int($name) || ctype_digit($name))) {
$name = (int) $name;
} elseif (\in_array($name, self::RESERVED_NAMES)) {
$name = "\u{035C}".$name;
}
2024-06-20 14:10:42 +00:00
parent::__construct([], ['name' => $name], $lineno);
}
public function compile(Compiler $compiler): void
{
2025-01-13 09:56:01 +00:00
if (null === $this->getAttribute('name')) {
$this->setAttribute('name', $compiler->getVarName());
}
$compiler->raw('$'.$this->getAttribute('name'));
2024-06-20 14:10:42 +00:00
}
}