/_html-warrior/externals/smarty/libs/sysplugins/smarty_internal_compile_extends.php

https://github.com/halka139/html-warrior · PHP · 113 lines · 69 code · 7 blank · 37 comment · 15 complexity · 1e4d58cb602fd8da930d9f9c4b5c2b07 MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile extend
  4. *
  5. * Compiles the {extends} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile extend Class
  13. *
  14. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Extends 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('file');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('file');
  32. /**
  33. * Compiles code for the {extends} tag
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param object $compiler compiler object
  37. * @return string compiled code
  38. */
  39. public function compile($args, $compiler)
  40. {
  41. $this->_rdl = preg_quote($compiler->smarty->right_delimiter);
  42. $this->_ldl = preg_quote($compiler->smarty->left_delimiter);
  43. $filepath = $compiler->template->source->filepath;
  44. // check and get attributes
  45. $_attr = $this->getAttributes($compiler, $args);
  46. if ($_attr['nocache'] === true) {
  47. $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
  48. }
  49. $_smarty_tpl = $compiler->template;
  50. $include_file = null;
  51. if (strpos($_attr['file'], '$_tmp') !== false) {
  52. $compiler->trigger_template_error('illegal value for file attribute', $compiler->lex->taglineno);
  53. }
  54. eval('$include_file = ' . $_attr['file'] . ';');
  55. // create template object
  56. $_template = new $compiler->smarty->template_class($include_file, $compiler->smarty, $compiler->template);
  57. // save file dependency
  58. if (in_array($_template->source->type, array('eval', 'string'))) {
  59. $template_sha1 = sha1($include_file);
  60. } else {
  61. $template_sha1 = sha1($_template->source->filepath);
  62. }
  63. if (isset($compiler->template->properties['file_dependency'][$template_sha1])) {
  64. $compiler->trigger_template_error("illegal recursive call of \"{$include_file}\"", $compiler->lex->line - 1);
  65. }
  66. $compiler->template->properties['file_dependency'][$template_sha1] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
  67. $_content = substr($compiler->template->source->content, $compiler->lex->counter - 1);
  68. if (preg_match_all("!({$this->_ldl}block\s(.+?){$this->_rdl})!", $_content, $s) !=
  69. preg_match_all("!({$this->_ldl}/block{$this->_rdl})!", $_content, $c)) {
  70. $compiler->trigger_template_error('unmatched {block} {/block} pairs');
  71. }
  72. preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  73. $_result_count = count($_result[0]);
  74. $_start = 0;
  75. while ($_start < $_result_count) {
  76. $_end = 0;
  77. $_level = 1;
  78. while ($_level != 0) {
  79. $_end++;
  80. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  81. $_level++;
  82. } else {
  83. $_level--;
  84. }
  85. }
  86. $_block_content = str_replace($compiler->smarty->left_delimiter . '$smarty.block.parent' . $compiler->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  87. substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
  88. Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template, $filepath);
  89. $_start = $_start + $_end + 1;
  90. }
  91. if ($_template->source->type == 'extends') {
  92. $_template->block_data = $compiler->template->block_data;
  93. }
  94. $compiler->template->source->content = $_template->source->content;
  95. if ($_template->source->type == 'extends') {
  96. $compiler->template->block_data = $_template->block_data;
  97. foreach ($_template->source->components as $key => $component) {
  98. $compiler->template->properties['file_dependency'][$key] = array($component->filepath, $component->timestamp, $component->type);
  99. }
  100. }
  101. $compiler->template->source->filepath = $_template->source->filepath;
  102. $compiler->abort_and_recompile = true;
  103. return '';
  104. }
  105. }
  106. ?>