| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* (c) Armin Ronacher |
| 8 |
* |
| 9 |
* For the full copyright and license information, please view the LICENSE |
| 10 |
* file that was distributed with this source code. |
| 11 |
*/ |
| 12 |
namespace ElementorDeps\Twig\Node; |
| 13 |
|
| 14 |
use ElementorDeps\Twig\Attribute\YieldReady; |
| 15 |
use ElementorDeps\Twig\Compiler; |
| 16 |
use ElementorDeps\Twig\Node\Expression\AbstractExpression; |
| 17 |
/** |
| 18 |
* Represents an include node. |
| 19 |
* |
| 20 |
* @author Fabien Potencier <fabien@symfony.com> |
| 21 |
*/ |
| 22 |
#[\Twig\Attribute\YieldReady] |
| 23 |
class IncludeNode extends Node implements NodeOutputInterface |
| 24 |
{ |
| 25 |
public function __construct(AbstractExpression $expr, ?AbstractExpression $variables, bool $only, bool $ignoreMissing, int $lineno, ?string $tag = null) |
| 26 |
{ |
| 27 |
$nodes = ['expr' => $expr]; |
| 28 |
if (null !== $variables) { |
| 29 |
$nodes['variables'] = $variables; |
| 30 |
} |
| 31 |
parent::__construct($nodes, ['only' => $only, 'ignore_missing' => $ignoreMissing], $lineno, $tag); |
| 32 |
} |
| 33 |
public function compile(Compiler $compiler) : void |
| 34 |
{ |
| 35 |
$compiler->addDebugInfo($this); |
| 36 |
if ($this->getAttribute('ignore_missing')) { |
| 37 |
$template = $compiler->getVarName(); |
| 38 |
$compiler->write(\sprintf("\$%s = null;\n", $template))->write("try {\n")->indent()->write(\sprintf('$%s = ', $template)); |
| 39 |
$this->addGetTemplate($compiler); |
| 40 |
$compiler->raw(";\n")->outdent()->write("} catch (LoaderError \$e) {\n")->indent()->write("// ignore missing template\n")->outdent()->write("}\n")->write(\sprintf("if (\$%s) {\n", $template))->indent()->write(\sprintf('yield from $%s->unwrap()->yield(', $template)); |
| 41 |
$this->addTemplateArguments($compiler); |
| 42 |
$compiler->raw(");\n")->outdent()->write("}\n"); |
| 43 |
} else { |
| 44 |
$compiler->write('yield from '); |
| 45 |
$this->addGetTemplate($compiler); |
| 46 |
$compiler->raw('->unwrap()->yield('); |
| 47 |
$this->addTemplateArguments($compiler); |
| 48 |
$compiler->raw(");\n"); |
| 49 |
} |
| 50 |
} |
| 51 |
protected function addGetTemplate(Compiler $compiler) |
| 52 |
{ |
| 53 |
$compiler->write('$this->loadTemplate(')->subcompile($this->getNode('expr'))->raw(', ')->repr($this->getTemplateName())->raw(', ')->repr($this->getTemplateLine())->raw(')'); |
| 54 |
} |
| 55 |
protected function addTemplateArguments(Compiler $compiler) |
| 56 |
{ |
| 57 |
if (!$this->hasNode('variables')) { |
| 58 |
$compiler->raw(\false === $this->getAttribute('only') ? '$context' : '[]'); |
| 59 |
} elseif (\false === $this->getAttribute('only')) { |
| 60 |
$compiler->raw('CoreExtension::merge($context, ')->subcompile($this->getNode('variables'))->raw(')'); |
| 61 |
} else { |
| 62 |
$compiler->raw('CoreExtension::toArray('); |
| 63 |
$compiler->subcompile($this->getNode('variables')); |
| 64 |
$compiler->raw(')'); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|