PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/mailpoet/vendor-prefixed/twig/twig/src/Node/ModuleNode.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 199 lines | 187 code | 0 blank | 12 comment | 32 complexity | ee5693d2607d68fba9f52cf320bbd272 MD5 | raw file
  1. <?php
  2. namespace MailPoetVendor\Twig\Node;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Twig\Compiler;
  5. use MailPoetVendor\Twig\Node\Expression\AbstractExpression;
  6. use MailPoetVendor\Twig\Node\Expression\ConstantExpression;
  7. use MailPoetVendor\Twig\Source;
  8. class ModuleNode extends Node
  9. {
  10. public function __construct(Node $body, ?AbstractExpression $parent, Node $blocks, Node $macros, Node $traits, $embeddedTemplates, Source $source)
  11. {
  12. if (__CLASS__ !== static::class) {
  13. @\trigger_error('Overriding ' . __CLASS__ . ' is deprecated since Twig 2.4.0 and the class will be final in 3.0.', \E_USER_DEPRECATED);
  14. }
  15. $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()];
  16. if (null !== $parent) {
  17. $nodes['parent'] = $parent;
  18. }
  19. // embedded templates are set as attributes so that they are only visited once by the visitors
  20. parent::__construct($nodes, ['index' => null, 'embedded_templates' => $embeddedTemplates], 1);
  21. // populate the template name of all node children
  22. $this->setSourceContext($source);
  23. }
  24. public function setIndex($index)
  25. {
  26. $this->setAttribute('index', $index);
  27. }
  28. public function compile(Compiler $compiler)
  29. {
  30. $this->compileTemplate($compiler);
  31. foreach ($this->getAttribute('embedded_templates') as $template) {
  32. $compiler->subcompile($template);
  33. }
  34. }
  35. protected function compileTemplate(Compiler $compiler)
  36. {
  37. if (!$this->getAttribute('index')) {
  38. $compiler->write('<?php');
  39. }
  40. $this->compileClassHeader($compiler);
  41. $this->compileConstructor($compiler);
  42. $this->compileGetParent($compiler);
  43. $this->compileDisplay($compiler);
  44. $compiler->subcompile($this->getNode('blocks'));
  45. $this->compileMacros($compiler);
  46. $this->compileGetTemplateName($compiler);
  47. $this->compileIsTraitable($compiler);
  48. $this->compileDebugInfo($compiler);
  49. $this->compileGetSourceContext($compiler);
  50. $this->compileClassFooter($compiler);
  51. }
  52. protected function compileGetParent(Compiler $compiler)
  53. {
  54. if (!$this->hasNode('parent')) {
  55. return;
  56. }
  57. $parent = $this->getNode('parent');
  58. $compiler->write("protected function doGetParent(array \$context)\n", "{\n")->indent()->addDebugInfo($parent)->write('return ');
  59. if ($parent instanceof ConstantExpression) {
  60. $compiler->subcompile($parent);
  61. } else {
  62. $compiler->raw('$this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($this->getSourceContext()->getName())->raw(', ')->repr($parent->getTemplateLine())->raw(')');
  63. }
  64. $compiler->raw(";\n")->outdent()->write("}\n\n");
  65. }
  66. protected function compileClassHeader(Compiler $compiler)
  67. {
  68. $compiler->write("\n\n");
  69. if (!$this->getAttribute('index')) {
  70. $compiler->write("use MailPoetVendor\\Twig\\Environment;\n")->write("use MailPoetVendor\\Twig\\Error\\LoaderError;\n")->write("use MailPoetVendor\\Twig\\Error\\RuntimeError;\n")->write("use MailPoetVendor\\Twig\\Extension\\SandboxExtension;\n")->write("use MailPoetVendor\\Twig\\Markup;\n")->write("use MailPoetVendor\\Twig\\Sandbox\\SecurityError;\n")->write("use MailPoetVendor\\Twig\\Sandbox\\SecurityNotAllowedTagError;\n")->write("use MailPoetVendor\\Twig\\Sandbox\\SecurityNotAllowedFilterError;\n")->write("use MailPoetVendor\\Twig\\Sandbox\\SecurityNotAllowedFunctionError;\n")->write("use MailPoetVendor\\Twig\\Source;\n")->write("use MailPoetVendor\\Twig\\Template;\n\n");
  71. }
  72. $compiler->write('/* ' . \str_replace('*/', '* /', $this->getSourceContext()->getName()) . " */\n")->write('class ' . $compiler->getEnvironment()->getTemplateClass($this->getSourceContext()->getName(), $this->getAttribute('index')))->raw(\sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass(\false)))->write("{\n")->indent()->write("private \$source;\n")->write("private \$macros = [];\n\n");
  73. }
  74. protected function compileConstructor(Compiler $compiler)
  75. {
  76. $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");
  77. // parent
  78. if (!$this->hasNode('parent')) {
  79. $compiler->write("\$this->parent = false;\n\n");
  80. }
  81. $countTraits = \count($this->getNode('traits'));
  82. if ($countTraits) {
  83. // traits
  84. foreach ($this->getNode('traits') as $i => $trait) {
  85. $node = $trait->getNode('template');
  86. $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->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->getBlocks();\n\n", $i, $i));
  87. foreach ($trait->getNode('targets') as $key => $value) {
  88. $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");
  89. }
  90. }
  91. if ($countTraits > 1) {
  92. $compiler->write("\$this->traits = array_merge(\n")->indent();
  93. for ($i = 0; $i < $countTraits; ++$i) {
  94. $compiler->write(\sprintf('$_trait_%s_blocks' . ($i == $countTraits - 1 ? '' : ',') . "\n", $i));
  95. }
  96. $compiler->outdent()->write(");\n\n");
  97. } else {
  98. $compiler->write("\$this->traits = \$_trait_0_blocks;\n\n");
  99. }
  100. $compiler->write("\$this->blocks = array_merge(\n")->indent()->write("\$this->traits,\n")->write("[\n");
  101. } else {
  102. $compiler->write("\$this->blocks = [\n");
  103. }
  104. // blocks
  105. $compiler->indent();
  106. foreach ($this->getNode('blocks') as $name => $node) {
  107. $compiler->write(\sprintf("'%s' => [\$this, 'block_%s'],\n", $name, $name));
  108. }
  109. if ($countTraits) {
  110. $compiler->outdent()->write("]\n")->outdent()->write(");\n");
  111. } else {
  112. $compiler->outdent()->write("];\n");
  113. }
  114. $compiler->subcompile($this->getNode('constructor_end'))->outdent()->write("}\n\n");
  115. }
  116. protected function compileDisplay(Compiler $compiler)
  117. {
  118. $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'));
  119. if ($this->hasNode('parent')) {
  120. $parent = $this->getNode('parent');
  121. $compiler->addDebugInfo($parent);
  122. if ($parent instanceof ConstantExpression) {
  123. $compiler->write('$this->parent = $this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($this->getSourceContext()->getName())->raw(', ')->repr($parent->getTemplateLine())->raw(");\n");
  124. $compiler->write('$this->parent');
  125. } else {
  126. $compiler->write('$this->getParent($context)');
  127. }
  128. $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
  129. }
  130. $compiler->subcompile($this->getNode('display_end'))->outdent()->write("}\n\n");
  131. }
  132. protected function compileClassFooter(Compiler $compiler)
  133. {
  134. $compiler->subcompile($this->getNode('class_end'))->outdent()->write("}\n");
  135. }
  136. protected function compileMacros(Compiler $compiler)
  137. {
  138. $compiler->subcompile($this->getNode('macros'));
  139. }
  140. protected function compileGetTemplateName(Compiler $compiler)
  141. {
  142. $compiler->write("public function getTemplateName()\n", "{\n")->indent()->write('return ')->repr($this->getSourceContext()->getName())->raw(";\n")->outdent()->write("}\n\n");
  143. }
  144. protected function compileIsTraitable(Compiler $compiler)
  145. {
  146. // A template can be used as a trait if:
  147. // * it has no parent
  148. // * it has no macros
  149. // * it has no body
  150. //
  151. // Put another way, a template can be used as a trait if it
  152. // only contains blocks and use statements.
  153. $traitable = !$this->hasNode('parent') && 0 === \count($this->getNode('macros'));
  154. if ($traitable) {
  155. if ($this->getNode('body') instanceof BodyNode) {
  156. $nodes = $this->getNode('body')->getNode(0);
  157. } else {
  158. $nodes = $this->getNode('body');
  159. }
  160. if (!\count($nodes)) {
  161. $nodes = new Node([$nodes]);
  162. }
  163. foreach ($nodes as $node) {
  164. if (!\count($node)) {
  165. continue;
  166. }
  167. if ($node instanceof TextNode && \ctype_space($node->getAttribute('data'))) {
  168. continue;
  169. }
  170. if ($node instanceof BlockReferenceNode) {
  171. continue;
  172. }
  173. $traitable = \false;
  174. break;
  175. }
  176. }
  177. if ($traitable) {
  178. return;
  179. }
  180. $compiler->write("public function isTraitable()\n", "{\n")->indent()->write(\sprintf("return %s;\n", $traitable ? 'true' : 'false'))->outdent()->write("}\n\n");
  181. }
  182. protected function compileDebugInfo(Compiler $compiler)
  183. {
  184. $compiler->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");
  185. }
  186. protected function compileGetSourceContext(Compiler $compiler)
  187. {
  188. $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");
  189. }
  190. protected function compileLoadTemplate(Compiler $compiler, $node, $var)
  191. {
  192. if ($node instanceof ConstantExpression) {
  193. $compiler->write(\sprintf('%s = $this->loadTemplate(', $var))->subcompile($node)->raw(', ')->repr($node->getTemplateName())->raw(', ')->repr($node->getTemplateLine())->raw(");\n");
  194. } else {
  195. throw new \LogicException('Trait templates can only be constant nodes.');
  196. }
  197. }
  198. }
  199. \class_alias('MailPoetVendor\\Twig\\Node\\ModuleNode', 'MailPoetVendor\\Twig_Node_Module');