Module.php 13 KB

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