PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Smarty/sysplugins/smarty_internal_compile_block.php

https://gitlab.com/dleonov/my-framework-two
PHP | 249 lines | 140 code | 12 blank | 97 comment | 17 complexity | 4de1235bad21264c882130d23ae724a7 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Smarty.
  4. *
  5. * (c) 2015 Uwe Tews
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Block Class
  12. *
  13. * @author Uwe Tews <uwe.tews@googlemail.com>
  14. */
  15. class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inheritance
  16. {
  17. /**
  18. * Attribute definition: Overwrites base class.
  19. *
  20. * @var array
  21. * @see Smarty_Internal_CompileBase
  22. */
  23. public $required_attributes = array('name');
  24. /**
  25. * Attribute definition: Overwrites base class.
  26. *
  27. * @var array
  28. * @see Smarty_Internal_CompileBase
  29. */
  30. public $shorttag_order = array('name');
  31. /**
  32. * Attribute definition: Overwrites base class.
  33. *
  34. * @var array
  35. * @see Smarty_Internal_CompileBase
  36. */
  37. public $option_flags = array('hide', 'nocache');
  38. /**
  39. * Attribute definition: Overwrites base class.
  40. *
  41. * @var array
  42. * @see Smarty_Internal_CompileBase
  43. */
  44. public $optional_attributes = array('assign');
  45. /**
  46. * nesting level of block tags
  47. *
  48. * @var int
  49. */
  50. public static $blockTagNestingLevel = 0;
  51. /**
  52. * Saved compiler object
  53. *
  54. * @var Smarty_Internal_TemplateCompilerBase
  55. */
  56. public $compiler = null;
  57. /**
  58. * Compiles code for the {block} tag
  59. *
  60. * @param array $args array with attributes from parser
  61. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  62. * @param array $parameter array with compilation parameter
  63. *
  64. * @return bool true
  65. */
  66. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  67. {
  68. if (!isset($compiler->_cache[ 'blockNesting' ])) {
  69. $compiler->_cache[ 'blockNesting' ] = 0;
  70. }
  71. if ($compiler->_cache[ 'blockNesting' ] == 0) {
  72. // make sure that inheritance gets initialized in template code
  73. $this->registerInit($compiler);
  74. $this->option_flags = array('hide', 'nocache', 'append', 'prepend');
  75. } else {
  76. $this->option_flags = array('hide', 'nocache');
  77. }
  78. // check and get attributes
  79. $_attr = $this->getAttributes($compiler, $args);
  80. $compiler->_cache[ 'blockNesting' ] ++;
  81. $compiler->_cache[ 'blockName' ][ $compiler->_cache[ 'blockNesting' ] ] = $_attr[ 'name' ];
  82. $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 'name' ] = "{$_attr['name']}";
  83. $this->openTag($compiler, 'block', array($_attr, $compiler->nocache, $compiler->parser->current_buffer,
  84. $compiler->template->compiled->has_nocache_code,
  85. $compiler->template->caching));
  86. // must whole block be nocache ?
  87. if ($compiler->tag_nocache) {
  88. $i = 0;
  89. }
  90. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  91. // $compiler->suppressNocacheProcessing = true;
  92. if ($_attr[ 'nocache' ] === true) {
  93. //$compiler->trigger_template_error('nocache option not allowed', $compiler->parser->lex->taglineno);
  94. }
  95. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  96. $compiler->template->compiled->has_nocache_code = false;
  97. $compiler->suppressNocacheProcessing = true;
  98. }
  99. /**
  100. * Compile saved child block source
  101. *
  102. * @param \Smarty_Internal_TemplateCompilerBase compiler object
  103. * @param string $_name optional name of child block
  104. *
  105. * @return string compiled code of child block
  106. */
  107. static function compileChildBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null)
  108. {
  109. if (!isset($compiler->_cache[ 'blockNesting' ])) {
  110. $compiler->trigger_template_error(' tag {$smarty.block.child} used outside {block} tags ',
  111. $compiler->parser->lex->taglineno);
  112. }
  113. $compiler->has_code = true;
  114. $compiler->suppressNocacheProcessing = true;
  115. $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 'callsChild' ] = 'true';
  116. $output = "<?php \n\$this->callChild(\$_smarty_tpl);\n?>\n";
  117. return $output;
  118. }
  119. /**
  120. * Compile $smarty.block.parent
  121. *
  122. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  123. * @param string $_name optional name of child block
  124. *
  125. * @return string compiled code of child block
  126. */
  127. static function compileParentBlock(Smarty_Internal_TemplateCompilerBase $compiler, $_name = null)
  128. {
  129. if (!isset($compiler->_cache[ 'blockNesting' ])) {
  130. $compiler->trigger_template_error(' tag {$smarty.block.parent} used outside {block} tags ',
  131. $compiler->parser->lex->taglineno);
  132. }
  133. $compiler->suppressNocacheProcessing = true;
  134. $compiler->has_code = true;
  135. $output = "<?php \n\$this->callParent(\$_smarty_tpl);\n?>\n";
  136. return $output;
  137. }
  138. }
  139. /**
  140. * Smarty Internal Plugin Compile BlockClose Class
  141. *
  142. */
  143. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_Inheritance
  144. {
  145. /**
  146. * Compiles code for the {/block} tag
  147. *
  148. * @param array $args array with attributes from parser
  149. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  150. * @param array $parameter array with compilation parameter
  151. *
  152. * @return bool true
  153. */
  154. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  155. {
  156. list($_attr, $_nocache, $_buffer, $_has_nocache_code, $_caching) = $this->closeTag($compiler, array('block'));
  157. // init block parameter
  158. $_block = $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ];
  159. unset($compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]);
  160. $_name = trim($_attr[ 'name' ], "'\"");
  161. $_assign = isset($_attr[ 'assign' ]) ? $_attr[ 'assign' ] : null;
  162. unset($_attr[ 'assign' ], $_attr[ 'name' ]);
  163. foreach ($_attr as $name => $stat) {
  164. if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat != 'false')) {
  165. $_block[ $name ] = 'true';
  166. }
  167. }
  168. $_className = 'Block_' . preg_replace('#[^\w\|]+#S', '_', $_name) . '_' .
  169. preg_replace('![^\w]+!', '_', uniqid(rand(), true));
  170. // get compiled block code
  171. $_functionCode = $compiler->parser->current_buffer;
  172. // setup buffer for template function code
  173. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  174. $output = "<?php\n";
  175. $output .= "/* {block '{$_name}'} {$compiler->template->source->type}:{$compiler->template->source->name} */\n";
  176. $output .= "class {$_className} extends Smarty_Internal_Block\n";
  177. $output .= "{\n";
  178. foreach ($_block as $property => $value) {
  179. $output .= "public \${$property} = {$value};\n";
  180. }
  181. $output .= "public function callBlock(Smarty_Internal_Template \$_smarty_tpl) {\n";
  182. //$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
  183. if ($compiler->template->compiled->has_nocache_code) {
  184. $output .= "\$_smarty_tpl->cached->hashes['{$compiler->template->compiled->nocache_hash}'] = true;\n";
  185. }
  186. if (isset($_assign)) {
  187. $output .= "ob_start();\n";
  188. }
  189. $output .= "?>\n";
  190. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  191. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  192. $output));
  193. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  194. $output = "<?php\n";
  195. if (isset($_assign)) {
  196. $output .= "\$_smarty_tpl->tpl_vars[{$_assign}] = new Smarty_Variable(ob_get_clean());\n";
  197. }
  198. //$output .= "/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n";
  199. $output .= "}\n";
  200. $output .= "}\n";
  201. $output .= "/* {/block '{$_name}'} */\n\n";
  202. $output .= "?>\n";
  203. $compiler->parser->current_buffer->append_subtree($compiler->parser,
  204. new Smarty_Internal_ParseTree_Tag($compiler->parser,
  205. $output));
  206. $compiler->blockOrFunctionCode .= $f = $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
  207. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  208. // nocache plugins must be copied
  209. if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) {
  210. foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) {
  211. foreach ($tmp as $type => $data) {
  212. $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin ][ $type ] =
  213. $data;
  214. }
  215. }
  216. }
  217. // restore old status
  218. $compiler->template->compiled->has_nocache_code = $_has_nocache_code;
  219. $compiler->tag_nocache = $compiler->nocache;
  220. $compiler->nocache = $_nocache;
  221. $compiler->parser->current_buffer = $_buffer;
  222. $output = "<?php \n";
  223. if ($compiler->_cache[ 'blockNesting' ] == 1) {
  224. $output .= "new {$_className}(\$_smarty_tpl);\n";
  225. } else {
  226. $output .= "new {$_className}(\$_smarty_tpl, \$this->tplIndex);\n";
  227. }
  228. $output .= "?>\n";
  229. $compiler->_cache[ 'blockNesting' ] --;
  230. if ($compiler->_cache[ 'blockNesting' ] == 0) {
  231. unset($compiler->_cache[ 'blockNesting' ]);
  232. }
  233. $compiler->has_code = true;
  234. $compiler->suppressNocacheProcessing = true;
  235. return $output;
  236. }
  237. }