PageRenderTime 49ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/smarty/sysplugins/smarty_internal_resource_extends.php

https://github.com/kiang/olc_baker
PHP | 178 lines | 121 code | 10 blank | 47 comment | 23 complexity | f2a0e1410a9146d2834675fcbdce7e6c MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource Extends
  4. *
  5. * Implements the file system as resource for Smarty which does extend a chain of template files templates
  6. *
  7. * @package Smarty
  8. * @subpackage TemplateResources
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Resource Extends
  13. */
  14. class Smarty_Internal_Resource_Extends
  15. {
  16. public function __construct($smarty)
  17. {
  18. $this->smarty = $smarty;
  19. $this->_rdl = preg_quote($smarty->right_delimiter);
  20. $this->_ldl = preg_quote($smarty->left_delimiter);
  21. }
  22. // classes used for compiling Smarty templates from file resource
  23. public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
  24. public $template_lexer_class = 'Smarty_Internal_Templatelexer';
  25. public $template_parser_class = 'Smarty_Internal_Templateparser';
  26. // properties
  27. public $usesCompiler = true;
  28. public $isEvaluated = false;
  29. public $allFilepaths = array();
  30. /**
  31. * Return flag if template source is existing
  32. *
  33. * @param object $_template template object
  34. * @return boolean result
  35. */
  36. public function isExisting($_template)
  37. {
  38. $_template->getTemplateFilepath();
  39. foreach ($this->allFilepaths as $_filepath) {
  40. if ($_filepath === false) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. /**
  47. * Get filepath to template source
  48. *
  49. * @param object $_template template object
  50. * @return string filepath to template source file
  51. */
  52. public function getTemplateFilepath($_template)
  53. {
  54. $sha1String = '';
  55. $_files = explode('|', $_template->resource_name);
  56. foreach ($_files as $_file) {
  57. $_filepath = $_template->buildTemplateFilepath ($_file);
  58. if ($_filepath !== false) {
  59. if (is_object($_template->smarty->security_policy)) {
  60. $_template->smarty->security_policy->isTrustedResourceDir($_filepath);
  61. }
  62. }
  63. $sha1String .= $_filepath;
  64. $this->allFilepaths[$_file] = $_filepath;
  65. }
  66. $_template->templateUid = sha1($sha1String);
  67. return $_filepath;
  68. }
  69. /**
  70. * Get timestamp to template source
  71. *
  72. * @param object $_template template object
  73. * @return integer timestamp of template source file
  74. */
  75. public function getTemplateTimestamp($_template)
  76. {
  77. return filemtime($_template->getTemplateFilepath());
  78. }
  79. /**
  80. * Read template source from file
  81. *
  82. * @param object $_template template object
  83. * @return string content of template source file
  84. */
  85. public function getTemplateSource($_template)
  86. {
  87. $this->template = $_template;
  88. $_files = array_reverse($this->allFilepaths);
  89. $_first = reset($_files);
  90. $_last = end($_files);
  91. foreach ($_files as $_file => $_filepath) {
  92. if ($_filepath === false) {
  93. throw new SmartyException("Unable to load template 'file : {$_file}'");
  94. }
  95. // read template file
  96. if ($_filepath != $_first) {
  97. $_template->properties['file_dependency'][sha1($_filepath)] = array($_filepath, filemtime($_filepath),'file');
  98. }
  99. $_template->template_filepath = $_filepath;
  100. $_content = file_get_contents($_filepath);
  101. if ($_filepath != $_last) {
  102. if (preg_match_all("!({$this->_ldl}block\s(.+?) {$this->_rdl})!", $_content, $_open) !=
  103. preg_match_all("!({$this->_ldl}/block{$this->_rdl})!", $_content, $_close)) {
  104. $this->smarty->triggerError("unmatched {block} {/block} pairs in file '$_filepath'");
  105. }
  106. preg_match_all("!{$this->_ldl}block\s(.+?){$this->_rdl}|{$this->_ldl}/block{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  107. $_result_count = count($_result[0]);
  108. $_start = 0;
  109. while ($_start < $_result_count) {
  110. $_end = 0;
  111. $_level = 1;
  112. while ($_level != 0) {
  113. $_end++;
  114. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  115. $_level++;
  116. } else {
  117. $_level--;
  118. }
  119. }
  120. $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  121. 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])));
  122. Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $_template, $_filepath);
  123. $_start = $_start + $_end + 1;
  124. }
  125. } else {
  126. $_template->template_source = $_content;
  127. return true;
  128. }
  129. }
  130. }
  131. /**
  132. * Get filepath to compiled template
  133. *
  134. * @param object $_template template object
  135. * @return string return path to compiled template
  136. */
  137. public function getCompiledFilepath($_template)
  138. {
  139. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
  140. $_files = explode('|', $_template->resource_name);
  141. // calculate Uid if not already done
  142. if ($_template->templateUid == '') {
  143. $_template->getTemplateFilepath();
  144. }
  145. $_filepath = $_template->templateUid;
  146. // if use_sub_dirs, break file into directories
  147. if ($_template->smarty->use_sub_dirs) {
  148. $_filepath = substr($_filepath, 0, 2) . DS
  149. . substr($_filepath, 2, 2) . DS
  150. . substr($_filepath, 4, 2) . DS
  151. . $_filepath;
  152. }
  153. $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
  154. if (isset($_compile_id)) {
  155. $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
  156. }
  157. if ($_template->caching) {
  158. $_cache = '.cache';
  159. } else {
  160. $_cache = '';
  161. }
  162. $_compile_dir = $_template->smarty->compile_dir;
  163. if (substr($_compile_dir, -1) != DS) {
  164. $_compile_dir .= DS;
  165. }
  166. return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
  167. }
  168. }