Files
Twig/src/Node/MacroNode.php
T
Fabien Potencier 5da4b58fc5 Merge branch '3.x' into 4.x
* 3.x:
  Check reserved names in TempNameExpression
2024-10-15 21:29:05 +02:00

98 lines
2.8 KiB
PHP

<?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;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\TempNameExpression;
/**
* Represents a macro node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
#[YieldReady]
class MacroNode extends Node
{
public const VARARGS_NAME = 'varargs';
public function __construct(string $name, BodyNode $body, ArrayExpression $arguments, int $lineno)
{
foreach ($arguments->getKeyValuePairs() as $pair) {
if ('_'.self::VARARGS_NAME.'_' === $pair['key']->getAttribute('name')) {
throw new SyntaxError(\sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $pair['value']->getTemplateLine(), $pair['value']->getSourceContext());
}
}
parent::__construct(['body' => $body, 'arguments' => $arguments], ['name' => $name], $lineno);
}
public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->write(\sprintf('public function macro_%s(', $this->getAttribute('name')))
;
foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
$name = $pair['key'];
$default = $pair['value'];
$compiler
->subcompile($name)
->raw(' = ')
->subcompile($default)
->raw(', ')
;
}
$compiler
->raw('...$varargs')
->raw(")\n")
->write("{\n")
->indent()
->write("\$macros = \$this->macros;\n")
->write("\$context = [\n")
->indent()
;
foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
$name = $pair['key'];
$compiler
->write('')
->string(trim($name->getAttribute('name'), '_'))
->raw(' => ')
->subcompile($name)
->raw(",\n")
;
}
$node = new CaptureNode($this->getNode('body'), $this->getNode('body')->lineno);
$compiler
->write('')
->string(self::VARARGS_NAME)
->raw(' => ')
->raw("\$varargs,\n")
->outdent()
->write("] + \$this->env->getGlobals();\n\n")
->write("\$blocks = [];\n\n")
->write('return ')
->subcompile($node)
->raw("\n")
->outdent()
->write("}\n\n")
;
}
}