| 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 |
use ElementorDeps\Twig\Node\Expression\ConstantExpression; |
| 18 |
use ElementorDeps\Twig\Source; |
| 19 |
/** |
| 20 |
* Represents a module node. |
| 21 |
* |
| 22 |
* Consider this class as being final. If you need to customize the behavior of |
| 23 |
* the generated class, consider adding nodes to the following nodes: display_start, |
| 24 |
* display_end, constructor_start, constructor_end, and class_end. |
| 25 |
* |
| 26 |
* @author Fabien Potencier <fabien@symfony.com> |
| 27 |
*/ |
| 28 |
#[\Twig\Attribute\YieldReady] |
| 29 |
final class ModuleNode extends Node |
| 30 |
{ |
| 31 |
public function __construct(Node $body, ?AbstractExpression $parent, Node $blocks, Node $macros, Node $traits, $embeddedTemplates, Source $source) |
| 32 |
{ |
| 33 |
$nodes = ['body' => $body, 'blocks' => $blocks, 'macros' => $macros, 'traits' => $traits, 'display_start' => new Node(), 'display_end' => new Node(), 'constructor_start' => new Node(), 'constructor_end' => new Node(), 'class_end' => new Node()]; |
| 34 |
if (null !== $parent) { |
| 35 |
$nodes['parent'] = $parent; |
| 36 |
} |
| 37 |
// embedded templates are set as attributes so that they are only visited once by the visitors |
| 38 |
parent::__construct($nodes, ['index' => null, 'embedded_templates' => $embeddedTemplates], 1); |
| 39 |
// populate the template name of all node children |
| 40 |
$this->setSourceContext($source); |
| 41 |
} |
| 42 |
public function setIndex($index) |
| 43 |
{ |
| 44 |
$this->setAttribute('index', $index); |
| 45 |
} |
| 46 |
public function compile(Compiler $compiler) : void |
| 47 |
{ |
| 48 |
$this->compileTemplate($compiler); |
| 49 |
foreach ($this->getAttribute('embedded_templates') as $template) { |
| 50 |
$compiler->subcompile($template); |
| 51 |
} |
| 52 |
} |
| 53 |
protected function compileTemplate(Compiler $compiler) |
| 54 |
{ |
| 55 |
if (!$this->getAttribute('index')) { |
| 56 |
$compiler->write('<?php'); |
| 57 |
} |
| 58 |
$this->compileClassHeader($compiler); |
| 59 |
$this->compileConstructor($compiler); |
| 60 |
$this->compileGetParent($compiler); |
| 61 |
$this->compileDisplay($compiler); |
| 62 |
$compiler->subcompile($this->getNode('blocks')); |
| 63 |
$this->compileMacros($compiler); |
| 64 |
$this->compileGetTemplateName($compiler); |
| 65 |
$this->compileIsTraitable($compiler); |
| 66 |
$this->compileDebugInfo($compiler); |
| 67 |
$this->compileGetSourceContext($compiler); |
| 68 |
$this->compileClassFooter($compiler); |
| 69 |
} |
| 70 |
protected function compileGetParent(Compiler $compiler) |
| 71 |
{ |
| 72 |
if (!$this->hasNode('parent')) { |
| 73 |
return; |
| 74 |
} |
| 75 |
$parent = $this->getNode('parent'); |
| 76 |
$compiler->write("protected function doGetParent(array \$context)\n", "{\n")->indent()->addDebugInfo($parent)->write('return '); |
| 77 |
if ($parent instanceof ConstantExpression) { |
| 78 |
$compiler->subcompile($parent); |
| 79 |
} else { |
| 80 |
$compiler->raw('$this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($this->getSourceContext()->getName())->raw(', ')->repr($parent->getTemplateLine())->raw(')'); |
| 81 |
} |
| 82 |
$compiler->raw(";\n")->outdent()->write("}\n\n"); |
| 83 |
} |
| 84 |
protected function compileClassHeader(Compiler $compiler) |
| 85 |
{ |
| 86 |
$compiler->write("\n\n"); |
| 87 |
if (!$this->getAttribute('index')) { |
| 88 |
$compiler->write("use ElementorDeps\Twig\Environment;\n")->write("use ElementorDeps\Twig\Error\LoaderError;\n")->write("use ElementorDeps\Twig\Error\RuntimeError;\n")->write("use ElementorDeps\Twig\Extension\CoreExtension;\n")->write("use ElementorDeps\Twig\Extension\SandboxExtension;\n")->write("use ElementorDeps\Twig\Markup;\n")->write("use ElementorDeps\Twig\Sandbox\SecurityError;\n")->write("use ElementorDeps\Twig\Sandbox\SecurityNotAllowedTagError;\n")->write("use ElementorDeps\Twig\Sandbox\SecurityNotAllowedFilterError;\n")->write("use ElementorDeps\Twig\Sandbox\SecurityNotAllowedFunctionError;\n")->write("use ElementorDeps\Twig\Source;\n")->write("use ElementorDeps\Twig\Template;\n\n"); |
| 89 |
} |
| 90 |
$compiler->write('/* ' . \str_replace('*/', '* /', $this->getSourceContext()->getName()) . " */\n")->write('class ' . $compiler->getEnvironment()->getTemplateClass($this->getSourceContext()->getName(), $this->getAttribute('index')))->raw(" extends Template\n")->write("{\n")->indent()->write("private \$source;\n")->write("private \$macros = [];\n\n"); |
| 91 |
} |
| 92 |
protected function compileConstructor(Compiler $compiler) |
| 93 |
{ |
| 94 |
$compiler->write("public function __construct(Environment \$env)\n", "{\n")->indent()->subcompile($this->getNode('constructor_start'))->write("parent::__construct(\$env);\n\n")->write("\$this->source = \$this->getSourceContext();\n\n"); |
| 95 |
// parent |
| 96 |
if (!$this->hasNode('parent')) { |
| 97 |
$compiler->write("\$this->parent = false;\n\n"); |
| 98 |
} |
| 99 |
$countTraits = \count($this->getNode('traits')); |
| 100 |
if ($countTraits) { |
| 101 |
// traits |
| 102 |
foreach ($this->getNode('traits') as $i => $trait) { |
| 103 |
$node = $trait->getNode('template'); |
| 104 |
$compiler->addDebugInfo($node)->write(\sprintf('$_trait_%s = $this->loadTemplate(', $i))->subcompile($node)->raw(', ')->repr($node->getTemplateName())->raw(', ')->repr($node->getTemplateLine())->raw(");\n")->write(\sprintf("if (!\$_trait_%s->unwrap()->isTraitable()) {\n", $i))->indent()->write("throw new RuntimeError('Template \"'.")->subcompile($trait->getNode('template'))->raw(".'\" cannot be used as a trait.', ")->repr($node->getTemplateLine())->raw(", \$this->source);\n")->outdent()->write("}\n")->write(\sprintf("\$_trait_%s_blocks = \$_trait_%s->unwrap()->getBlocks();\n\n", $i, $i)); |
| 105 |
foreach ($trait->getNode('targets') as $key => $value) { |
| 106 |
$compiler->write(\sprintf('if (!isset($_trait_%s_blocks[', $i))->string($key)->raw("])) {\n")->indent()->write("throw new RuntimeError('Block ")->string($key)->raw(' is not defined in trait ')->subcompile($trait->getNode('template'))->raw(".', ")->repr($node->getTemplateLine())->raw(", \$this->source);\n")->outdent()->write("}\n\n")->write(\sprintf('$_trait_%s_blocks[', $i))->subcompile($value)->raw(\sprintf('] = $_trait_%s_blocks[', $i))->string($key)->raw(\sprintf(']; unset($_trait_%s_blocks[', $i))->string($key)->raw("]);\n\n"); |
| 107 |
} |
| 108 |
} |
| 109 |
if ($countTraits > 1) { |
| 110 |
$compiler->write("\$this->traits = array_merge(\n")->indent(); |
| 111 |
for ($i = 0; $i < $countTraits; ++$i) { |
| 112 |
$compiler->write(\sprintf('$_trait_%s_blocks' . ($i == $countTraits - 1 ? '' : ',') . "\n", $i)); |
| 113 |
} |
| 114 |
$compiler->outdent()->write(");\n\n"); |
| 115 |
} else { |
| 116 |
$compiler->write("\$this->traits = \$_trait_0_blocks;\n\n"); |
| 117 |
} |
| 118 |
$compiler->write("\$this->blocks = array_merge(\n")->indent()->write("\$this->traits,\n")->write("[\n"); |
| 119 |
} else { |
| 120 |
$compiler->write("\$this->blocks = [\n"); |
| 121 |
} |
| 122 |
// blocks |
| 123 |
$compiler->indent(); |
| 124 |
foreach ($this->getNode('blocks') as $name => $node) { |
| 125 |
$compiler->write(\sprintf("'%s' => [\$this, 'block_%s'],\n", $name, $name)); |
| 126 |
} |
| 127 |
if ($countTraits) { |
| 128 |
$compiler->outdent()->write("]\n")->outdent()->write(");\n"); |
| 129 |
} else { |
| 130 |
$compiler->outdent()->write("];\n"); |
| 131 |
} |
| 132 |
$compiler->subcompile($this->getNode('constructor_end'))->outdent()->write("}\n\n"); |
| 133 |
} |
| 134 |
protected function compileDisplay(Compiler $compiler) |
| 135 |
{ |
| 136 |
$compiler->write("protected function doDisplay(array \$context, array \$blocks = [])\n", "{\n")->indent()->write("\$macros = \$this->macros;\n")->subcompile($this->getNode('display_start'))->subcompile($this->getNode('body')); |
| 137 |
if ($this->hasNode('parent')) { |
| 138 |
$parent = $this->getNode('parent'); |
| 139 |
$compiler->addDebugInfo($parent); |
| 140 |
if ($parent instanceof ConstantExpression) { |
| 141 |
$compiler->write('$this->parent = $this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($this->getSourceContext()->getName())->raw(', ')->repr($parent->getTemplateLine())->raw(");\n"); |
| 142 |
} |
| 143 |
$compiler->write('yield from '); |
| 144 |
if ($parent instanceof ConstantExpression) { |
| 145 |
$compiler->raw('$this->parent'); |
| 146 |
} else { |
| 147 |
$compiler->raw('$this->getParent($context)'); |
| 148 |
} |
| 149 |
$compiler->raw("->unwrap()->yield(\$context, array_merge(\$this->blocks, \$blocks));\n"); |
| 150 |
} |
| 151 |
$compiler->subcompile($this->getNode('display_end')); |
| 152 |
if (!$this->hasNode('parent')) { |
| 153 |
$compiler->write("return; yield '';\n"); |
| 154 |
// ensure at least one yield call even for templates with no output |
| 155 |
} |
| 156 |
$compiler->outdent()->write("}\n\n"); |
| 157 |
} |
| 158 |
protected function compileClassFooter(Compiler $compiler) |
| 159 |
{ |
| 160 |
$compiler->subcompile($this->getNode('class_end'))->outdent()->write("}\n"); |
| 161 |
} |
| 162 |
protected function compileMacros(Compiler $compiler) |
| 163 |
{ |
| 164 |
$compiler->subcompile($this->getNode('macros')); |
| 165 |
} |
| 166 |
protected function compileGetTemplateName(Compiler $compiler) |
| 167 |
{ |
| 168 |
$compiler->write("/**\n")->write(" * @codeCoverageIgnore\n")->write(" */\n")->write("public function getTemplateName()\n", "{\n")->indent()->write('return ')->repr($this->getSourceContext()->getName())->raw(";\n")->outdent()->write("}\n\n"); |
| 169 |
} |
| 170 |
protected function compileIsTraitable(Compiler $compiler) |
| 171 |
{ |
| 172 |
// A template can be used as a trait if: |
| 173 |
// * it has no parent |
| 174 |
// * it has no macros |
| 175 |
// * it has no body |
| 176 |
// |
| 177 |
// Put another way, a template can be used as a trait if it |
| 178 |
// only contains blocks and use statements. |
| 179 |
$traitable = !$this->hasNode('parent') && 0 === \count($this->getNode('macros')); |
| 180 |
if ($traitable) { |
| 181 |
if ($this->getNode('body') instanceof BodyNode) { |
| 182 |
$nodes = $this->getNode('body')->getNode('0'); |
| 183 |
} else { |
| 184 |
$nodes = $this->getNode('body'); |
| 185 |
} |
| 186 |
if (!\count($nodes)) { |
| 187 |
$nodes = new Node([$nodes]); |
| 188 |
} |
| 189 |
foreach ($nodes as $node) { |
| 190 |
if (!\count($node)) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
if ($node instanceof TextNode && \ctype_space($node->getAttribute('data'))) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
if ($node instanceof BlockReferenceNode) { |
| 197 |
continue; |
| 198 |
} |
| 199 |
$traitable = \false; |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
if ($traitable) { |
| 204 |
return; |
| 205 |
} |
| 206 |
$compiler->write("/**\n")->write(" * @codeCoverageIgnore\n")->write(" */\n")->write("public function isTraitable()\n", "{\n")->indent()->write("return false;\n")->outdent()->write("}\n\n"); |
| 207 |
} |
| 208 |
protected function compileDebugInfo(Compiler $compiler) |
| 209 |
{ |
| 210 |
$compiler->write("/**\n")->write(" * @codeCoverageIgnore\n")->write(" */\n")->write("public function getDebugInfo()\n", "{\n")->indent()->write(\sprintf("return %s;\n", \str_replace("\n", '', \var_export(\array_reverse($compiler->getDebugInfo(), \true), \true))))->outdent()->write("}\n\n"); |
| 211 |
} |
| 212 |
protected function compileGetSourceContext(Compiler $compiler) |
| 213 |
{ |
| 214 |
$compiler->write("public function getSourceContext()\n", "{\n")->indent()->write('return new Source(')->string($compiler->getEnvironment()->isDebug() ? $this->getSourceContext()->getCode() : '')->raw(', ')->string($this->getSourceContext()->getName())->raw(', ')->string($this->getSourceContext()->getPath())->raw(");\n")->outdent()->write("}\n"); |
| 215 |
} |
| 216 |
protected function compileLoadTemplate(Compiler $compiler, $node, $var) |
| 217 |
{ |
| 218 |
if ($node instanceof ConstantExpression) { |
| 219 |
$compiler->write(\sprintf('%s = $this->loadTemplate(', $var))->subcompile($node)->raw(', ')->repr($node->getTemplateName())->raw(', ')->repr($node->getTemplateLine())->raw(");\n"); |
| 220 |
} else { |
| 221 |
throw new \LogicException('Trait templates can only be constant nodes.'); |
| 222 |
} |
| 223 |
} |
| 224 |
private function hasNodeOutputNodes(Node $node) : bool |
| 225 |
{ |
| 226 |
if ($node instanceof NodeOutputInterface) { |
| 227 |
return \true; |
| 228 |
} |
| 229 |
foreach ($node as $child) { |
| 230 |
if ($this->hasNodeOutputNodes($child)) { |
| 231 |
return \true; |
| 232 |
} |
| 233 |
} |
| 234 |
return \false; |
| 235 |
} |
| 236 |
} |
| 237 |
|