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

74 lines
2.6 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\TwigTest;
2024-06-20 14:10:42 +00:00
class TestExpression extends CallExpression
{
2025-01-13 09:56:01 +00:00
#[FirstClassTwigCallableReady]
/**
* @param AbstractExpression $node
*/
public function __construct(Node $node, string|TwigTest $test, ?Node $arguments, int $lineno)
2024-06-20 14:10:42 +00:00
{
2025-01-13 09:56:01 +00:00
if (!$node instanceof AbstractExpression) {
trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance to the "node" argument of "%s" is deprecated ("%s" given).', AbstractExpression::class, static::class, \get_class($node));
}
2024-06-20 14:10:42 +00:00
$nodes = ['node' => $node];
if (null !== $arguments) {
$nodes['arguments'] = $arguments;
}
2025-01-13 09:56:01 +00:00
if ($test instanceof TwigTest) {
$name = $test->getName();
} else {
$name = $test;
trigger_deprecation('twig/twig', '3.12', 'Not passing an instance of "TwigTest" when creating a "%s" test of type "%s" is deprecated.', $name, static::class);
}
2024-09-05 17:51:48 +00:00
parent::__construct($nodes, ['name' => $name, 'type' => 'test'], $lineno);
2025-01-13 09:56:01 +00:00
if ($test instanceof TwigTest) {
$this->setAttribute('twig_callable', $test);
}
$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): void
{
2025-01-13 09:56:01 +00:00
$name = $this->getAttribute('name');
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 "test" node in a NodeVisitor class is not supported anymore.');
$this->removeAttribute('twig_callable');
}
}
2024-06-20 14:10:42 +00:00
2025-01-13 09:56:01 +00:00
if (!$this->hasAttribute('twig_callable')) {
$this->setAttribute('twig_callable', $compiler->getEnvironment()->getTest($this->getAttribute('name')));
}
2024-06-20 14:10:42 +00:00
$this->compileCallable($compiler);
}
}