PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/smarty/sysplugins/smarty_internal_compile_block.php

http://github.com/modxcms/revolution
PHP | 189 lines | 121 code | 6 blank | 62 comment | 13 complexity | d468e6f519625029515443926b39b4f7 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  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. * Compiles code for the {block} tag
  47. *
  48. * @param array $args array with attributes from parser
  49. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  50. * @param array $parameter array with compilation parameter
  51. */
  52. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  53. {
  54. if (!isset($compiler->_cache[ 'blockNesting' ])) {
  55. $compiler->_cache[ 'blockNesting' ] = 0;
  56. }
  57. if ($compiler->_cache[ 'blockNesting' ] === 0) {
  58. // make sure that inheritance gets initialized in template code
  59. $this->registerInit($compiler);
  60. $this->option_flags = array('hide', 'nocache', 'append', 'prepend');
  61. } else {
  62. $this->option_flags = array('hide', 'nocache');
  63. }
  64. // check and get attributes
  65. $_attr = $this->getAttributes($compiler, $args);
  66. ++$compiler->_cache[ 'blockNesting' ];
  67. $_className = 'Block_' . preg_replace('![^\w]+!', '_', uniqid(mt_rand(), true));
  68. $compiler->_cache[ 'blockName' ][ $compiler->_cache[ 'blockNesting' ] ] = $_attr[ 'name' ];
  69. $compiler->_cache[ 'blockClass' ][ $compiler->_cache[ 'blockNesting' ] ] = $_className;
  70. $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ] = array();
  71. $compiler->_cache[ 'blockParams' ][ 1 ][ 'subBlocks' ][ trim($_attr[ 'name' ], '"\'') ][] = $_className;
  72. $this->openTag(
  73. $compiler,
  74. 'block',
  75. array(
  76. $_attr, $compiler->nocache, $compiler->parser->current_buffer,
  77. $compiler->template->compiled->has_nocache_code,
  78. $compiler->template->caching
  79. )
  80. );
  81. $compiler->saveRequiredPlugins(true);
  82. $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
  83. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  84. $compiler->template->compiled->has_nocache_code = false;
  85. $compiler->suppressNocacheProcessing = true;
  86. }
  87. }
  88. /**
  89. * Smarty Internal Plugin Compile BlockClose Class
  90. */
  91. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_Inheritance
  92. {
  93. /**
  94. * Compiles code for the {/block} tag
  95. *
  96. * @param array $args array with attributes from parser
  97. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  98. * @param array $parameter array with compilation parameter
  99. *
  100. * @return bool true
  101. */
  102. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  103. {
  104. list($_attr, $_nocache, $_buffer, $_has_nocache_code, $_caching) = $this->closeTag($compiler, array('block'));
  105. // init block parameter
  106. $_block = $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ];
  107. unset($compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]);
  108. $_name = $_attr[ 'name' ];
  109. $_assign = isset($_attr[ 'assign' ]) ? $_attr[ 'assign' ] : null;
  110. unset($_attr[ 'assign' ], $_attr[ 'name' ]);
  111. foreach ($_attr as $name => $stat) {
  112. if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat !== 'false')) {
  113. $_block[ $name ] = 'true';
  114. }
  115. }
  116. $_className = $compiler->_cache[ 'blockClass' ][ $compiler->_cache[ 'blockNesting' ] ];
  117. // get compiled block code
  118. $_functionCode = $compiler->parser->current_buffer;
  119. // setup buffer for template function code
  120. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  121. $output = "<?php\n";
  122. $output .= "/* {block {$_name}} */\n";
  123. $output .= "class {$_className} extends Smarty_Internal_Block\n";
  124. $output .= "{\n";
  125. foreach ($_block as $property => $value) {
  126. $output .= "public \${$property} = " . var_export($value, true) . ";\n";
  127. }
  128. $output .= "public function callBlock(Smarty_Internal_Template \$_smarty_tpl) {\n";
  129. $output .= $compiler->compileRequiredPlugins();
  130. $compiler->restoreRequiredPlugins();
  131. if ($compiler->template->compiled->has_nocache_code) {
  132. $output .= "\$_smarty_tpl->cached->hashes['{$compiler->template->compiled->nocache_hash}'] = true;\n";
  133. }
  134. if (isset($_assign)) {
  135. $output .= "ob_start();\n";
  136. }
  137. $output .= "?>\n";
  138. $compiler->parser->current_buffer->append_subtree(
  139. $compiler->parser,
  140. new Smarty_Internal_ParseTree_Tag(
  141. $compiler->parser,
  142. $output
  143. )
  144. );
  145. $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
  146. $output = "<?php\n";
  147. if (isset($_assign)) {
  148. $output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
  149. }
  150. $output .= "}\n";
  151. $output .= "}\n";
  152. $output .= "/* {/block {$_name}} */\n\n";
  153. $output .= "?>\n";
  154. $compiler->parser->current_buffer->append_subtree(
  155. $compiler->parser,
  156. new Smarty_Internal_ParseTree_Tag(
  157. $compiler->parser,
  158. $output
  159. )
  160. );
  161. $compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
  162. $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
  163. // restore old status
  164. $compiler->template->compiled->has_nocache_code = $_has_nocache_code;
  165. $compiler->tag_nocache = $compiler->nocache;
  166. $compiler->nocache = $_nocache;
  167. $compiler->parser->current_buffer = $_buffer;
  168. $output = "<?php \n";
  169. if ($compiler->_cache[ 'blockNesting' ] === 1) {
  170. $output .= "\$_smarty_tpl->inheritance->instanceBlock(\$_smarty_tpl, '$_className', $_name);\n";
  171. } else {
  172. $output .= "\$_smarty_tpl->inheritance->instanceBlock(\$_smarty_tpl, '$_className', $_name, \$this->tplIndex);\n";
  173. }
  174. $output .= "?>\n";
  175. --$compiler->_cache[ 'blockNesting' ];
  176. if ($compiler->_cache[ 'blockNesting' ] === 0) {
  177. unset($compiler->_cache[ 'blockNesting' ]);
  178. }
  179. $compiler->has_code = true;
  180. $compiler->suppressNocacheProcessing = true;
  181. return $output;
  182. }
  183. }