PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/twig/twig/lib/Twig/Node/Module.php

https://gitlab.com/arthur_quiroga/dystawork
PHP | 444 lines | 355 code | 58 blank | 31 comment | 35 complexity | e0b620b3cccb735401154380e56fda3a MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. * (c) Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Represents a module node.
  13. *
  14. * Consider this class as being final. If you need to customize the behavior of
  15. * the generated class, consider adding nodes to the following nodes: display_start,
  16. * display_end, constructor_start, constructor_end, and class_end.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class Twig_Node_Module extends Twig_Node
  21. {
  22. private $source;
  23. public function __construct(Twig_Node $body, Twig_Node_Expression $parent = null, Twig_Node $blocks, Twig_Node $macros, Twig_Node $traits, $embeddedTemplates, Twig_Source $source)
  24. {
  25. $this->source = $source;
  26. $nodes = array(
  27. 'body' => $body,
  28. 'blocks' => $blocks,
  29. 'macros' => $macros,
  30. 'traits' => $traits,
  31. 'display_start' => new Twig_Node(),
  32. 'display_end' => new Twig_Node(),
  33. 'constructor_start' => new Twig_Node(),
  34. 'constructor_end' => new Twig_Node(),
  35. 'class_end' => new Twig_Node(),
  36. );
  37. if (null !== $parent) {
  38. $nodes['parent'] = $parent;
  39. }
  40. // embedded templates are set as attributes so that they are only visited once by the visitors
  41. parent::__construct($nodes, array(
  42. 'index' => null,
  43. 'embedded_templates' => $embeddedTemplates,
  44. ), 1);
  45. // populate the template name of all node children
  46. $this->setTemplateName($this->source->getName());
  47. }
  48. public function setIndex($index)
  49. {
  50. $this->setAttribute('index', $index);
  51. }
  52. public function compile(Twig_Compiler $compiler)
  53. {
  54. $this->compileTemplate($compiler);
  55. foreach ($this->getAttribute('embedded_templates') as $template) {
  56. $compiler->subcompile($template);
  57. }
  58. }
  59. protected function compileTemplate(Twig_Compiler $compiler)
  60. {
  61. if (!$this->getAttribute('index')) {
  62. $compiler->write('<?php');
  63. }
  64. $this->compileClassHeader($compiler);
  65. if (
  66. count($this->getNode('blocks'))
  67. || count($this->getNode('traits'))
  68. || !$this->hasNode('parent')
  69. || $this->getNode('parent') instanceof Twig_Node_Expression_Constant
  70. || count($this->getNode('constructor_start'))
  71. || count($this->getNode('constructor_end'))
  72. ) {
  73. $this->compileConstructor($compiler);
  74. }
  75. $this->compileGetParent($compiler);
  76. $this->compileDisplay($compiler);
  77. $compiler->subcompile($this->getNode('blocks'));
  78. $this->compileMacros($compiler);
  79. $this->compileGetTemplateName($compiler);
  80. $this->compileIsTraitable($compiler);
  81. $this->compileDebugInfo($compiler);
  82. $this->compileGetSourceContext($compiler);
  83. $this->compileClassFooter($compiler);
  84. }
  85. protected function compileGetParent(Twig_Compiler $compiler)
  86. {
  87. if (!$this->hasNode('parent')) {
  88. return;
  89. }
  90. $parent = $this->getNode('parent');
  91. $compiler
  92. ->write("protected function doGetParent(array \$context)\n", "{\n")
  93. ->indent()
  94. ->addDebugInfo($parent)
  95. ->write('return ')
  96. ;
  97. if ($parent instanceof Twig_Node_Expression_Constant) {
  98. $compiler->subcompile($parent);
  99. } else {
  100. $compiler
  101. ->raw('$this->loadTemplate(')
  102. ->subcompile($parent)
  103. ->raw(', ')
  104. ->repr($this->source->getName())
  105. ->raw(', ')
  106. ->repr($parent->getTemplateLine())
  107. ->raw(')')
  108. ;
  109. }
  110. $compiler
  111. ->raw(";\n")
  112. ->outdent()
  113. ->write("}\n\n")
  114. ;
  115. }
  116. protected function compileClassHeader(Twig_Compiler $compiler)
  117. {
  118. $compiler
  119. ->write("\n\n")
  120. // if the template name contains */, add a blank to avoid a PHP parse error
  121. ->write('/* '.str_replace('*/', '* /', $this->source->getName())." */\n")
  122. ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->source->getName(), $this->getAttribute('index')))
  123. ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
  124. ->write("{\n")
  125. ->indent()
  126. ;
  127. }
  128. protected function compileConstructor(Twig_Compiler $compiler)
  129. {
  130. $compiler
  131. ->write("public function __construct(Twig_Environment \$env)\n", "{\n")
  132. ->indent()
  133. ->subcompile($this->getNode('constructor_start'))
  134. ->write("parent::__construct(\$env);\n\n")
  135. ;
  136. // parent
  137. if (!$this->hasNode('parent')) {
  138. $compiler->write("\$this->parent = false;\n\n");
  139. } elseif (($parent = $this->getNode('parent')) && $parent instanceof Twig_Node_Expression_Constant) {
  140. $compiler
  141. ->addDebugInfo($parent)
  142. ->write('$this->parent = $this->loadTemplate(')
  143. ->subcompile($parent)
  144. ->raw(', ')
  145. ->repr($this->source->getName())
  146. ->raw(', ')
  147. ->repr($parent->getTemplateLine())
  148. ->raw(");\n")
  149. ;
  150. }
  151. $countTraits = count($this->getNode('traits'));
  152. if ($countTraits) {
  153. // traits
  154. foreach ($this->getNode('traits') as $i => $trait) {
  155. $node = $trait->getNode('template');
  156. $compiler
  157. ->write(sprintf('$_trait_%s = $this->loadTemplate(', $i))
  158. ->subcompile($node)
  159. ->raw(', ')
  160. ->repr($node->getTemplateName())
  161. ->raw(', ')
  162. ->repr($node->getTemplateLine())
  163. ->raw(");\n")
  164. ;
  165. $compiler
  166. ->addDebugInfo($trait->getNode('template'))
  167. ->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))
  168. ->indent()
  169. ->write("throw new Twig_Error_Runtime('Template \"'.")
  170. ->subcompile($trait->getNode('template'))
  171. ->raw(".'\" cannot be used as a trait.');\n")
  172. ->outdent()
  173. ->write("}\n")
  174. ->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i))
  175. ;
  176. foreach ($trait->getNode('targets') as $key => $value) {
  177. $compiler
  178. ->write(sprintf('if (!isset($_trait_%s_blocks[', $i))
  179. ->string($key)
  180. ->raw("])) {\n")
  181. ->indent()
  182. ->write("throw new Twig_Error_Runtime(sprintf('Block ")
  183. ->string($key)
  184. ->raw(' is not defined in trait ')
  185. ->subcompile($trait->getNode('template'))
  186. ->raw(".'));\n")
  187. ->outdent()
  188. ->write("}\n\n")
  189. ->write(sprintf('$_trait_%s_blocks[', $i))
  190. ->subcompile($value)
  191. ->raw(sprintf('] = $_trait_%s_blocks[', $i))
  192. ->string($key)
  193. ->raw(sprintf(']; unset($_trait_%s_blocks[', $i))
  194. ->string($key)
  195. ->raw("]);\n\n")
  196. ;
  197. }
  198. }
  199. if ($countTraits > 1) {
  200. $compiler
  201. ->write("\$this->traits = array_merge(\n")
  202. ->indent()
  203. ;
  204. for ($i = 0; $i < $countTraits; ++$i) {
  205. $compiler
  206. ->write(sprintf('$_trait_%s_blocks'.($i == $countTraits - 1 ? '' : ',')."\n", $i))
  207. ;
  208. }
  209. $compiler
  210. ->outdent()
  211. ->write(");\n\n")
  212. ;
  213. } else {
  214. $compiler
  215. ->write("\$this->traits = \$_trait_0_blocks;\n\n")
  216. ;
  217. }
  218. $compiler
  219. ->write("\$this->blocks = array_merge(\n")
  220. ->indent()
  221. ->write("\$this->traits,\n")
  222. ->write("array(\n")
  223. ;
  224. } else {
  225. $compiler
  226. ->write("\$this->blocks = array(\n")
  227. ;
  228. }
  229. // blocks
  230. $compiler
  231. ->indent()
  232. ;
  233. foreach ($this->getNode('blocks') as $name => $node) {
  234. $compiler
  235. ->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name))
  236. ;
  237. }
  238. if ($countTraits) {
  239. $compiler
  240. ->outdent()
  241. ->write(")\n")
  242. ;
  243. }
  244. $compiler
  245. ->outdent()
  246. ->write(");\n")
  247. ->outdent()
  248. ->subcompile($this->getNode('constructor_end'))
  249. ->write("}\n\n")
  250. ;
  251. }
  252. protected function compileDisplay(Twig_Compiler $compiler)
  253. {
  254. $compiler
  255. ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n")
  256. ->indent()
  257. ->subcompile($this->getNode('display_start'))
  258. ->subcompile($this->getNode('body'))
  259. ;
  260. if ($this->hasNode('parent')) {
  261. $parent = $this->getNode('parent');
  262. $compiler->addDebugInfo($parent);
  263. if ($parent instanceof Twig_Node_Expression_Constant) {
  264. $compiler->write('$this->parent');
  265. } else {
  266. $compiler->write('$this->getParent($context)');
  267. }
  268. $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
  269. }
  270. $compiler
  271. ->subcompile($this->getNode('display_end'))
  272. ->outdent()
  273. ->write("}\n\n")
  274. ;
  275. }
  276. protected function compileClassFooter(Twig_Compiler $compiler)
  277. {
  278. $compiler
  279. ->subcompile($this->getNode('class_end'))
  280. ->outdent()
  281. ->write("}\n")
  282. ;
  283. }
  284. protected function compileMacros(Twig_Compiler $compiler)
  285. {
  286. $compiler->subcompile($this->getNode('macros'));
  287. }
  288. protected function compileGetTemplateName(Twig_Compiler $compiler)
  289. {
  290. $compiler
  291. ->write("public function getTemplateName()\n", "{\n")
  292. ->indent()
  293. ->write('return ')
  294. ->repr($this->source->getName())
  295. ->raw(";\n")
  296. ->outdent()
  297. ->write("}\n\n")
  298. ;
  299. }
  300. protected function compileIsTraitable(Twig_Compiler $compiler)
  301. {
  302. // A template can be used as a trait if:
  303. // * it has no parent
  304. // * it has no macros
  305. // * it has no body
  306. //
  307. // Put another way, a template can be used as a trait if it
  308. // only contains blocks and use statements.
  309. $traitable = !$this->hasNode('parent') && 0 === count($this->getNode('macros'));
  310. if ($traitable) {
  311. if ($this->getNode('body') instanceof Twig_Node_Body) {
  312. $nodes = $this->getNode('body')->getNode(0);
  313. } else {
  314. $nodes = $this->getNode('body');
  315. }
  316. if (!count($nodes)) {
  317. $nodes = new Twig_Node(array($nodes));
  318. }
  319. foreach ($nodes as $node) {
  320. if (!count($node)) {
  321. continue;
  322. }
  323. if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) {
  324. continue;
  325. }
  326. if ($node instanceof Twig_Node_BlockReference) {
  327. continue;
  328. }
  329. $traitable = false;
  330. break;
  331. }
  332. }
  333. if ($traitable) {
  334. return;
  335. }
  336. $compiler
  337. ->write("public function isTraitable()\n", "{\n")
  338. ->indent()
  339. ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
  340. ->outdent()
  341. ->write("}\n\n")
  342. ;
  343. }
  344. protected function compileDebugInfo(Twig_Compiler $compiler)
  345. {
  346. $compiler
  347. ->write("public function getDebugInfo()\n", "{\n")
  348. ->indent()
  349. ->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
  350. ->outdent()
  351. ->write("}\n\n")
  352. ;
  353. }
  354. protected function compileGetSourceContext(Twig_Compiler $compiler)
  355. {
  356. $compiler
  357. ->write("public function getSourceContext()\n", "{\n")
  358. ->indent()
  359. ->write('return new Twig_Source(')
  360. ->string($compiler->getEnvironment()->isDebug() ? $this->source->getCode() : '')
  361. ->raw(', ')
  362. ->string($this->source->getName())
  363. ->raw(', ')
  364. ->string($this->source->getPath())
  365. ->raw(");\n")
  366. ->outdent()
  367. ->write("}\n")
  368. ;
  369. }
  370. protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
  371. {
  372. if ($node instanceof Twig_Node_Expression_Constant) {
  373. $compiler
  374. ->write(sprintf('%s = $this->loadTemplate(', $var))
  375. ->subcompile($node)
  376. ->raw(', ')
  377. ->repr($node->getTemplateName())
  378. ->raw(', ')
  379. ->repr($node->getTemplateLine())
  380. ->raw(");\n")
  381. ;
  382. } else {
  383. throw new LogicException('Trait templates can only be constant nodes.');
  384. }
  385. }
  386. }