PageRenderTime 77ms CodeModel.GetById 48ms RepoModel.GetById 0ms app.codeStats 0ms

/smarty/sysplugins/smarty_internal_compile_block.php

https://bitbucket.org/makarewicz/galeria
PHP | 282 lines | 194 code | 11 blank | 77 comment | 44 complexity | d3c7b9a629ef094a9f296bd6b10ea7ce MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Block
  4. *
  5. * Compiles the {block}{/block} tags
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Block Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('name');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('name', 'hide');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $optional_attributes = array('hide');
  39. /**
  40. * Compiles code for the {block} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param object $compiler compiler object
  44. * @return boolean true
  45. */
  46. public function compile($args, $compiler) {
  47. // check and get attributes
  48. $_attr = $this->getAttributes($compiler, $args);
  49. $save = array($_attr, $compiler->parser->current_buffer, $compiler->nocache, $compiler->smarty->merge_compiled_includes, $compiler->merged_templates, $compiler->smarty->merged_templates_func, $compiler->template->properties, $compiler->template->has_nocache_code);
  50. $this->openTag($compiler, 'block', $save);
  51. if ($_attr['nocache'] == true) {
  52. $compiler->nocache = true;
  53. }
  54. // set flag for {block} tag
  55. $compiler->inheritance = true;
  56. // must merge includes
  57. $compiler->smarty->merge_compiled_includes = true;
  58. $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
  59. $compiler->has_code = false;
  60. return true;
  61. }
  62. /**
  63. * Save or replace child block source by block name during parsing
  64. *
  65. * @param string $block_content block source content
  66. * @param string $block_tag opening block tag
  67. * @param object $template template object
  68. * @param string $filepath filepath of template source
  69. */
  70. public static function saveBlockData($block_content, $block_tag, $template, $filepath) {
  71. $_rdl = preg_quote($template->smarty->right_delimiter);
  72. $_ldl = preg_quote($template->smarty->left_delimiter);
  73. if (!$template->smarty->auto_literal) {
  74. $al = '\s*';
  75. } else {
  76. $al = '';
  77. }
  78. if (0 == preg_match("!({$_ldl}{$al}block\s+)(name=)?(\w+|'.*'|\".*\")(\s*?)?((append|prepend|nocache)?(\s*)?(hide)?)?(\s*{$_rdl})!", $block_tag, $_match)) {
  79. $error_text = 'Syntax Error in template "' . $template->source->filepath . '" "' . htmlspecialchars($block_tag) . '" illegal options';
  80. throw new SmartyCompilerException($error_text);
  81. } else {
  82. $_name = trim($_match[3], '\'"');
  83. if ($_match[8] != 'hide' || isset($template->block_data[$_name])) { // replace {$smarty.block.child}
  84. // get nested block tags
  85. if (0 != preg_match_all("!({$_ldl}{$al}block\s+)(name=)?(\w+|'.*'|\".*\")([\s\S]*?)(hide)?(\s*{$_rdl})([\s\S]*?)(.*)?({$_ldl}{$al}/block\s*{$_rdl})!", $block_content, $_match2)) {
  86. foreach ($_match2[3] as $key => $name) {
  87. // get it's replacement
  88. $_name2 = trim($name, '\'"');
  89. if ($_match2[5][$key] != 'hide' || isset($template->block_data[$_name2])) {
  90. if (isset($template->block_data[$_name2])) {
  91. $replacement = $template->block_data[$_name2]['source'];
  92. } else {
  93. $replacement = '';
  94. }
  95. // replace {$smarty.block.child} tag
  96. if (preg_match("!{$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl}!",$_match2[7][$key])) {
  97. $replacement = preg_replace("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $replacement, $_match2[7][$key]);
  98. $block_content = preg_replace("!(({$_ldl}{$al}block)(.*)?{$name}(.*)?({$_rdl}[\s\S]*?{$_ldl}{$al}/block\s*{$_rdl}))!", $replacement, $block_content);
  99. }
  100. if (preg_match("!{$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl}!",$_match2[8][$key])) {
  101. $replacement = preg_replace("!{$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl}!", $replacement, $_match2[8][$key]);
  102. $block_content = preg_replace("!(({$_ldl}{$al}block)(.*)?{$name}(.*)?({$_rdl})(.*)?({$_ldl}{$al}/block\s*{$_rdl}))!", $replacement, $block_content);
  103. }
  104. } else {
  105. // remove hidden blocks
  106. $block_content = preg_replace("!(({$_ldl}{$al}block)(.*)?{$name}(.*)?({$_rdl}[\s\S]*?{$_ldl}{$al}/block\s*{$_rdl}))!", '', $block_content);
  107. }
  108. }
  109. }
  110. // do we have not nested {$smart.block.child}
  111. if (0 != preg_match("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $block_content, $_match2)) {
  112. // get child replacement for this block
  113. if (isset($template->block_data[$_name])) {
  114. $replacement = $template->block_data[$_name]['source'];
  115. unset($template->block_data[$_name]);
  116. } else {
  117. $replacement = '';
  118. }
  119. $block_content = preg_replace("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $replacement, $block_content);
  120. }
  121. if (isset($template->block_data[$_name])) {
  122. if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  123. $template->block_data[$_name]['source'] =
  124. str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
  125. } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
  126. $template->block_data[$_name]['source'] .= $block_content;
  127. } elseif ($template->block_data[$_name]['mode'] == 'append') {
  128. $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
  129. }
  130. } else {
  131. $template->block_data[$_name]['source'] = $block_content;
  132. $template->block_data[$_name]['file'] = $filepath;
  133. }
  134. if ($_match[6] == 'append') {
  135. $template->block_data[$_name]['mode'] = 'append';
  136. } elseif ($_match[6] == 'prepend') {
  137. $template->block_data[$_name]['mode'] = 'prepend';
  138. } else {
  139. $template->block_data[$_name]['mode'] = 'replace';
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * Compile saved child block source
  146. *
  147. * @param object $compiler compiler object
  148. * @param string $_name optional name of child block
  149. * @return string compiled code of schild block
  150. */
  151. public static function compileChildBlock($compiler, $_name = null) {
  152. $_output = '';
  153. // if called by {$smarty.block.child} we must search the name of enclosing {block}
  154. if ($_name == null) {
  155. $stack_count = count($compiler->_tag_stack);
  156. while (--$stack_count >= 0) {
  157. if ($compiler->_tag_stack[$stack_count][0] == 'block') {
  158. $_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'], "'\"");
  159. break;
  160. }
  161. }
  162. // flag that child is already compile by {$smarty.block.child} inclusion
  163. $compiler->template->block_data[$_name]['compiled'] = true;
  164. }
  165. if ($_name == null) {
  166. $compiler->trigger_template_error('{$smarty.block.child} used out of context', $compiler->lex->taglineno);
  167. }
  168. // undefined child?
  169. if (!isset($compiler->template->block_data[$_name]['source'])) {
  170. return '';
  171. }
  172. $_tpl = new Smarty_Internal_template('string:' . $compiler->template->block_data[$_name]['source'], $compiler->smarty, $compiler->template, $compiler->template->cache_id,
  173. $compiler->template->compile_id = null, $compiler->template->caching, $compiler->template->cache_lifetime);
  174. $_tpl->variable_filters = $compiler->template->variable_filters;
  175. $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  176. $_tpl->source->filepath = $compiler->template->block_data[$_name]['file'];
  177. $_tpl->allow_relative_path = true;
  178. if ($compiler->nocache) {
  179. $_tpl->compiler->forceNocache = 2;
  180. } else {
  181. $_tpl->compiler->forceNocache = 1;
  182. }
  183. $_tpl->compiler->suppressHeader = true;
  184. $_tpl->compiler->suppressTemplatePropertyHeader = true;
  185. $_tpl->compiler->suppressMergedTemplates = true;
  186. if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
  187. $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->compiler->compileTemplate($_tpl));
  188. } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
  189. $_output = $_tpl->compiler->compileTemplate($_tpl) . $compiler->parser->current_buffer->to_smarty_php();
  190. } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
  191. $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->compiler->compileTemplate($_tpl);
  192. } elseif (!empty($compiler->template->block_data[$_name])) {
  193. $_output = $_tpl->compiler->compileTemplate($_tpl);
  194. }
  195. $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
  196. $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
  197. $compiler->merged_templates = array_merge($compiler->merged_templates, $_tpl->compiler->merged_templates);
  198. $compiler->template->variable_filters = $_tpl->variable_filters;
  199. if ($_tpl->has_nocache_code) {
  200. $compiler->template->has_nocache_code = true;
  201. }
  202. foreach ($_tpl->required_plugins as $key => $tmp1) {
  203. if ($compiler->nocache && $compiler->template->caching) {
  204. $code = 'nocache';
  205. } else {
  206. $code = $key;
  207. }
  208. foreach ($tmp1 as $name => $tmp) {
  209. foreach ($tmp as $type => $data) {
  210. $compiler->template->required_plugins[$code][$name][$type] = $data;
  211. }
  212. }
  213. }
  214. unset($_tpl);
  215. return $_output;
  216. }
  217. }
  218. /**
  219. * Smarty Internal Plugin Compile BlockClose Class
  220. *
  221. * @package Smarty
  222. * @subpackage Compiler
  223. */
  224. class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
  225. /**
  226. * Compiles code for the {/block} tag
  227. *
  228. * @param array $args array with attributes from parser
  229. * @param object $compiler compiler object
  230. * @return string compiled code
  231. */
  232. public function compile($args, $compiler) {
  233. $compiler->has_code = true;
  234. // check and get attributes
  235. $_attr = $this->getAttributes($compiler, $args);
  236. $saved_data = $this->closeTag($compiler, array('block'));
  237. $_name = trim($saved_data[0]['name'], "\"'");
  238. if (isset($compiler->template->block_data[$_name]) && !isset($compiler->template->block_data[$_name]['compiled'])) {
  239. // restore to status before {block} tag as new subtemplate code of parent {block} is not needed
  240. // TODO: Below code was disabled in 3.1.8 because of problems with {include} in nested {block} tags in child templates
  241. // combined with append/prepend or $smarty.block.parent
  242. // For later versions it should be checked under which conditions it could run for optimisation
  243. //
  244. //$compiler->merged_templates = $saved_data[4];
  245. //$compiler->smarty->merged_templates_func = $saved_data[5];
  246. //$compiler->template->properties = $saved_data[6];
  247. //$compiler->template->has_nocache_code = $saved_data[7];
  248. $_output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $_name);
  249. } else {
  250. if (isset($saved_data[0]['hide']) && !isset($compiler->template->block_data[$_name]['source'])) {
  251. $_output = '';
  252. } else {
  253. $_output = $compiler->parser->current_buffer->to_smarty_php();
  254. }
  255. unset($compiler->template->block_data[$_name]['compiled']);
  256. }
  257. // reset flags
  258. $compiler->parser->current_buffer = $saved_data[1];
  259. $compiler->nocache = $saved_data[2];
  260. $compiler->smarty->merge_compiled_includes = $saved_data[3];
  261. // reset flag for {block} tag
  262. $compiler->inheritance = false;
  263. // $_output content has already nocache code processed
  264. $compiler->suppressNocacheProcessing = true;
  265. return $_output;
  266. }
  267. }
  268. ?>