Twig_Node_Module

Represents a module node.

Defined (1)

The class is defined in the following location(s).

/vendor/twig/twig/lib/Twig/Node/Module.php  
  1. class Twig_Node_Module extends Twig_Node 
  2. private $source; 
  3.  
  4. public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $name, $source = '') 
  5. if (!$name instanceof Twig_Source) { 
  6. @trigger_error(sprintf('Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED); 
  7. $this->source = new Twig_Source($source, $name); 
  8. } else { 
  9. $this->source = $name; 
  10.  
  11. $nodes = array( 
  12. 'body' => $body,  
  13. 'blocks' => $blocks,  
  14. 'macros' => $macros,  
  15. 'traits' => $traits,  
  16. 'display_start' => new Twig_Node(),  
  17. 'display_end' => new Twig_Node(),  
  18. 'constructor_start' => new Twig_Node(),  
  19. 'constructor_end' => new Twig_Node(),  
  20. 'class_end' => new Twig_Node(),  
  21. ); 
  22. if (null !== $parent) { 
  23. $nodes['parent'] = $parent; 
  24.  
  25. // embedded templates are set as attributes so that they are only visited once by the visitors 
  26. parent::__construct($nodes, array( 
  27. // source to be remove in 2.0 
  28. 'source' => $this->source->getCode(),  
  29. // filename to be remove in 2.0 (use getTemplateName() instead) 
  30. 'filename' => $this->source->getName(),  
  31. 'index' => null,  
  32. 'embedded_templates' => $embeddedTemplates,  
  33. ), 1); 
  34.  
  35. // populate the template name of all node children 
  36. $this->setTemplateName($this->source->getName()); 
  37.  
  38. public function setIndex($index) 
  39. $this->setAttribute('index', $index); 
  40.  
  41. public function compile(Twig_Compiler $compiler) 
  42. $this->compileTemplate($compiler); 
  43.  
  44. foreach ($this->getAttribute('embedded_templates') as $template) { 
  45. $compiler->subcompile($template); 
  46.  
  47. protected function compileTemplate(Twig_Compiler $compiler) 
  48. if (!$this->getAttribute('index')) { 
  49. $compiler->write('<?php'); 
  50.  
  51. $this->compileClassHeader($compiler); 
  52.  
  53. if ( 
  54. count($this->getNode('blocks')) 
  55. || count($this->getNode('traits')) 
  56. || !$this->hasNode('parent') 
  57. || $this->getNode('parent') instanceof Twig_Node_Expression_Constant 
  58. || count($this->getNode('constructor_start')) 
  59. || count($this->getNode('constructor_end')) 
  60. ) { 
  61. $this->compileConstructor($compiler); 
  62.  
  63. $this->compileGetParent($compiler); 
  64.  
  65. $this->compileDisplay($compiler); 
  66.  
  67. $compiler->subcompile($this->getNode('blocks')); 
  68.  
  69. $this->compileMacros($compiler); 
  70.  
  71. $this->compileGetTemplateName($compiler); 
  72.  
  73. $this->compileIsTraitable($compiler); 
  74.  
  75. $this->compileDebugInfo($compiler); 
  76.  
  77. $this->compileGetSource($compiler); 
  78.  
  79. $this->compileGetSourceContext($compiler); 
  80.  
  81. $this->compileClassFooter($compiler); 
  82.  
  83. protected function compileGetParent(Twig_Compiler $compiler) 
  84. if (!$this->hasNode('parent')) { 
  85. return; 
  86. $parent = $this->getNode('parent'); 
  87.  
  88. $compiler 
  89. ->write("protected function doGetParent(array \$context)\n", "{\n") 
  90. ->indent() 
  91. ->addDebugInfo($parent) 
  92. ->write('return ') 
  93.  
  94. if ($parent instanceof Twig_Node_Expression_Constant) { 
  95. $compiler->subcompile($parent); 
  96. } else { 
  97. $compiler 
  98. ->raw('$this->loadTemplate(') 
  99. ->subcompile($parent) 
  100. ->raw(', ') 
  101. ->repr($this->source->getName()) 
  102. ->raw(', ') 
  103. ->repr($parent->getTemplateLine()) 
  104. ->raw(')') 
  105.  
  106. $compiler 
  107. ->raw(";\n") 
  108. ->outdent() 
  109. ->write("}\n\n") 
  110.  
  111. protected function compileClassHeader(Twig_Compiler $compiler) 
  112. $compiler 
  113. ->write("\n\n") 
  114. // if the template name contains */, add a blank to avoid a PHP parse error 
  115. ->write('/* '.str_replace('*/', '* /', $this->source->getName())." */\n") 
  116. ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->source->getName(), $this->getAttribute('index'))) 
  117. ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass())) 
  118. ->write("{\n") 
  119. ->indent() 
  120.  
  121. protected function compileConstructor(Twig_Compiler $compiler) 
  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 (!$this->hasNode('parent')) { 
  130. $compiler->write("\$this->parent = false;\n\n"); 
  131. } elseif (($parent = $this->getNode('parent')) && $parent instanceof Twig_Node_Expression_Constant) { 
  132. $compiler 
  133. ->addDebugInfo($parent) 
  134. ->write('$this->parent = $this->loadTemplate(') 
  135. ->subcompile($parent) 
  136. ->raw(', ') 
  137. ->repr($this->source->getName()) 
  138. ->raw(', ') 
  139. ->repr($parent->getTemplateLine()) 
  140. ->raw(");\n") 
  141.  
  142. $countTraits = count($this->getNode('traits')); 
  143. if ($countTraits) { 
  144. // traits 
  145. foreach ($this->getNode('traits') as $i => $trait) { 
  146. $this->compileLoadTemplate($compiler, $trait->getNode('template'), sprintf('$_trait_%s', $i)); 
  147.  
  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.  
  173. ->write(sprintf('$_trait_%s_blocks[', $i)) 
  174. ->subcompile($value) 
  175. ->raw(sprintf('] = $_trait_%s_blocks[', $i)) 
  176. ->string($key) 
  177. ->raw(sprintf(']; unset($_trait_%s_blocks[', $i)) 
  178. ->string($key) 
  179. ->raw("]);\n\n") 
  180.  
  181. if ($countTraits > 1) { 
  182. $compiler 
  183. ->write("\$this->traits = array_merge(\n") 
  184. ->indent() 
  185.  
  186. for ($i = 0; $i < $countTraits; ++$i) { 
  187. $compiler 
  188. ->write(sprintf('$_trait_%s_blocks'.($i == $countTraits - 1 ? '' : ', ')."\n", $i)) 
  189.  
  190. $compiler 
  191. ->outdent() 
  192. ->write(");\n\n") 
  193. } else { 
  194. $compiler 
  195. ->write("\$this->traits = \$_trait_0_blocks;\n\n") 
  196.  
  197. $compiler 
  198. ->write("\$this->blocks = array_merge(\n") 
  199. ->indent() 
  200. ->write("\$this->traits, \n") 
  201. ->write("array(\n") 
  202. } else { 
  203. $compiler 
  204. ->write("\$this->blocks = array(\n") 
  205.  
  206. // blocks 
  207. $compiler 
  208. ->indent() 
  209.  
  210. foreach ($this->getNode('blocks') as $name => $node) { 
  211. $compiler 
  212. ->write(sprintf("'%s' => array(\$this, 'block_%s'), \n", $name, $name)) 
  213.  
  214. if ($countTraits) { 
  215. $compiler 
  216. ->outdent() 
  217. ->write(")\n") 
  218.  
  219. $compiler 
  220. ->outdent() 
  221. ->write(");\n") 
  222. ->outdent() 
  223. ->subcompile($this->getNode('constructor_end')) 
  224. ->write("}\n\n") 
  225.  
  226. protected function compileDisplay(Twig_Compiler $compiler) 
  227. $compiler 
  228. ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n") 
  229. ->indent() 
  230. ->subcompile($this->getNode('display_start')) 
  231. ->subcompile($this->getNode('body')) 
  232.  
  233. if ($this->hasNode('parent')) { 
  234. $parent = $this->getNode('parent'); 
  235. $compiler->addDebugInfo($parent); 
  236. if ($parent instanceof Twig_Node_Expression_Constant) { 
  237. $compiler->write('$this->parent'); 
  238. } else { 
  239. $compiler->write('$this->getParent($context)'); 
  240. $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n"); 
  241.  
  242. $compiler 
  243. ->subcompile($this->getNode('display_end')) 
  244. ->outdent() 
  245. ->write("}\n\n") 
  246.  
  247. protected function compileClassFooter(Twig_Compiler $compiler) 
  248. $compiler 
  249. ->subcompile($this->getNode('class_end')) 
  250. ->outdent() 
  251. ->write("}\n") 
  252.  
  253. protected function compileMacros(Twig_Compiler $compiler) 
  254. $compiler->subcompile($this->getNode('macros')); 
  255.  
  256. protected function compileGetTemplateName(Twig_Compiler $compiler) 
  257. $compiler 
  258. ->write("public function getTemplateName()\n", "{\n") 
  259. ->indent() 
  260. ->write('return ') 
  261. ->repr($this->source->getName()) 
  262. ->raw(";\n") 
  263. ->outdent() 
  264. ->write("}\n\n") 
  265.  
  266. protected function compileIsTraitable(Twig_Compiler $compiler) 
  267. // A template can be used as a trait if: 
  268. // * it has no parent 
  269. // * it has no macros 
  270. // * it has no body 
  271. // 
  272. // Put another way, a template can be used as a trait if it 
  273. // only contains blocks and use statements. 
  274. $traitable = !$this->hasNode('parent') && 0 === count($this->getNode('macros')); 
  275. if ($traitable) { 
  276. if ($this->getNode('body') instanceof Twig_Node_Body) { 
  277. $nodes = $this->getNode('body')->getNode(0); 
  278. } else { 
  279. $nodes = $this->getNode('body'); 
  280.  
  281. if (!count($nodes)) { 
  282. $nodes = new Twig_Node(array($nodes)); 
  283.  
  284. foreach ($nodes as $node) { 
  285. if (!count($node)) { 
  286. continue; 
  287.  
  288. if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) { 
  289. continue; 
  290.  
  291. if ($node instanceof Twig_Node_BlockReference) { 
  292. continue; 
  293.  
  294. $traitable = false; 
  295. break; 
  296.  
  297. if ($traitable) { 
  298. return; 
  299.  
  300. $compiler 
  301. ->write("public function isTraitable()\n", "{\n") 
  302. ->indent() 
  303. ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false')) 
  304. ->outdent() 
  305. ->write("}\n\n") 
  306.  
  307. protected function compileDebugInfo(Twig_Compiler $compiler) 
  308. $compiler 
  309. ->write("public function getDebugInfo()\n", "{\n") 
  310. ->indent() 
  311. ->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true)))) 
  312. ->outdent() 
  313. ->write("}\n\n") 
  314.  
  315. protected function compileGetSource(Twig_Compiler $compiler) 
  316. $compiler 
  317. ->write("/** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */\n") 
  318. ->write("public function getSource()\n", "{\n") 
  319. ->indent() 
  320. ->write("@trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);\n\n") 
  321. ->write('return $this->getSourceContext()->getCode();') 
  322. ->raw("\n") 
  323. ->outdent() 
  324. ->write("}\n\n") 
  325.  
  326. protected function compileGetSourceContext(Twig_Compiler $compiler) 
  327. $compiler 
  328. ->write("public function getSourceContext()\n", "{\n") 
  329. ->indent() 
  330. ->write('return new Twig_Source(') 
  331. ->string($compiler->getEnvironment()->isDebug() ? $this->source->getCode() : '') 
  332. ->raw(', ') 
  333. ->string($this->source->getName()) 
  334. ->raw(', ') 
  335. ->string($this->source->getPath()) 
  336. ->raw(");\n") 
  337. ->outdent() 
  338. ->write("}\n") 
  339.  
  340. protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var) 
  341. if ($node instanceof Twig_Node_Expression_Constant) { 
  342. $compiler 
  343. ->write(sprintf('%s = $this->loadTemplate(', $var)) 
  344. ->subcompile($node) 
  345. ->raw(', ') 
  346. ->repr($node->getTemplateName()) 
  347. ->raw(', ') 
  348. ->repr($node->getTemplateLine()) 
  349. ->raw(");\n") 
  350. } else { 
  351. throw new LogicException('Trait templates can only be constant nodes.');