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

69 lines
2.7 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;
2025-01-13 09:56:01 +00:00
use Twig\Attribute\FirstClassTwigCallableReady;
2024-06-20 14:10:42 +00:00
use Twig\Compiler;
2025-01-13 09:56:01 +00:00
use Twig\Node\NameDeprecation;
2024-06-20 14:10:42 +00:00
use Twig\Node\Node;
2025-01-13 09:56:01 +00:00
use Twig\TwigFunction;
2024-06-20 14:10:42 +00:00
class FunctionExpression extends CallExpression
{
2025-01-13 09:56:01 +00:00
#[FirstClassTwigCallableReady]
public function __construct(TwigFunction|string $function, Node $arguments, int $lineno)
2024-06-20 14:10:42 +00:00
{
2025-01-13 09:56:01 +00:00
if ($function instanceof TwigFunction) {
$name = $function->getName();
} else {
$name = $function;
trigger_deprecation('twig/twig', '3.12', 'Not passing an instance of "TwigFunction" when creating a "%s" function of type "%s" is deprecated.', $name, static::class);
}
2024-09-05 17:51:48 +00:00
parent::__construct(['arguments' => $arguments], ['name' => $name, 'type' => 'function', 'is_defined_test' => false], $lineno);
2025-01-13 09:56:01 +00:00
if ($function instanceof TwigFunction) {
$this->setAttribute('twig_callable', $function);
}
$this->deprecateAttribute('needs_charset', new NameDeprecation('twig/twig', '3.12'));
$this->deprecateAttribute('needs_environment', new NameDeprecation('twig/twig', '3.12'));
$this->deprecateAttribute('needs_context', new NameDeprecation('twig/twig', '3.12'));
$this->deprecateAttribute('arguments', new NameDeprecation('twig/twig', '3.12'));
$this->deprecateAttribute('callable', new NameDeprecation('twig/twig', '3.12'));
$this->deprecateAttribute('is_variadic', new NameDeprecation('twig/twig', '3.12'));
$this->deprecateAttribute('dynamic_name', new NameDeprecation('twig/twig', '3.12'));
2024-06-20 14:10:42 +00:00
}
public function compile(Compiler $compiler)
{
$name = $this->getAttribute('name');
2025-01-13 09:56:01 +00:00
if ($this->hasAttribute('twig_callable')) {
$name = $this->getAttribute('twig_callable')->getName();
if ($name !== $this->getAttribute('name')) {
trigger_deprecation('twig/twig', '3.12', 'Changing the value of a "function" node in a NodeVisitor class is not supported anymore.');
$this->removeAttribute('twig_callable');
}
}
if (!$this->hasAttribute('twig_callable')) {
$this->setAttribute('twig_callable', $compiler->getEnvironment()->getFunction($name));
}
2024-06-20 14:10:42 +00:00
if ('constant' === $name && $this->getAttribute('is_defined_test')) {
2025-01-13 09:56:01 +00:00
$this->getNode('arguments')->setNode('checkDefined', new ConstantExpression(true, $this->getTemplateLine()));
2024-06-20 14:10:42 +00:00
}
$this->compileCallable($compiler);
}
}