PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/smarty/sysplugins/smarty_internal_resource_extends.php

https://github.com/lewellyn/TrellisDesk
PHP | 162 lines | 101 code | 12 blank | 49 comment | 26 complexity | 81bd2dbe5f113d66064d63ee38d2af7f MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource Extends
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Uwe Tews
  8. * @author Rodney Rehm
  9. */
  10. /**
  11. * Smarty Internal Plugin Resource Extends
  12. *
  13. * Implements the file system as resource for Smarty which {extend}s a chain of template files templates
  14. *
  15. * @package Smarty
  16. * @subpackage TemplateResources
  17. */
  18. class Smarty_Internal_Resource_Extends extends Smarty_Resource {
  19. /**
  20. * mbstring.overload flag
  21. *
  22. * @var int
  23. */
  24. public $mbstring_overload = 0;
  25. /**
  26. * populate Source Object with meta data from Resource
  27. *
  28. * @param Smarty_Template_Source $source source object
  29. * @param Smarty_Internal_Template $_template template object
  30. */
  31. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
  32. {
  33. $uid = '';
  34. $sources = array();
  35. $components = explode('|', $source->name);
  36. $exists = true;
  37. foreach ($components as $component) {
  38. $s = Smarty_Resource::source(null, $source->smarty, $component);
  39. if ($s->type == 'php') {
  40. throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
  41. }
  42. $sources[$s->uid] = $s;
  43. $uid .= $s->filepath;
  44. if ($_template && $_template->smarty->compile_check) {
  45. $exists = $exists && $s->exists;
  46. }
  47. }
  48. $source->components = $sources;
  49. $source->filepath = $s->filepath;
  50. $source->uid = sha1($uid);
  51. if ($_template && $_template->smarty->compile_check) {
  52. $source->timestamp = $s->timestamp;
  53. $source->exists = $exists;
  54. }
  55. // need the template at getContent()
  56. $source->template = $_template;
  57. }
  58. /**
  59. * populate Source Object with timestamp and exists from Resource
  60. *
  61. * @param Smarty_Template_Source $source source object
  62. */
  63. public function populateTimestamp(Smarty_Template_Source $source)
  64. {
  65. $source->exists = true;
  66. foreach ($source->components as $s) {
  67. $source->exists = $source->exists && $s->exists;
  68. }
  69. $source->timestamp = $s->timestamp;
  70. }
  71. /**
  72. * Load template's source from files into current template object
  73. *
  74. * @param Smarty_Template_Source $source source object
  75. * @return string template source
  76. * @throws SmartyException if source cannot be loaded
  77. */
  78. public function getContent(Smarty_Template_Source $source)
  79. {
  80. if (!$source->exists) {
  81. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  82. }
  83. $this->mbstring_overload = ini_get('mbstring.func_overload') & 2;
  84. $_rdl = preg_quote($source->smarty->right_delimiter);
  85. $_ldl = preg_quote($source->smarty->left_delimiter);
  86. if (!$source->smarty->auto_literal) {
  87. $al = '\s*';
  88. } else {
  89. $al = '';
  90. }
  91. $_components = array_reverse($source->components);
  92. $_first = reset($_components);
  93. $_last = end($_components);
  94. foreach ($_components as $_component) {
  95. // register dependency
  96. if ($_component != $_first) {
  97. $source->template->properties['file_dependency'][$_component->uid] = array($_component->filepath, $_component->timestamp, $_component->type);
  98. }
  99. // read content
  100. $source->filepath = $_component->filepath;
  101. $_content = $_component->content;
  102. // extend sources
  103. if ($_component != $_last) {
  104. if (preg_match_all("!({$_ldl}{$al}block\s(.+?)\s*{$_rdl})!", $_content, $_open) !=
  105. preg_match_all("!({$_ldl}{$al}/block\s*{$_rdl})!", $_content, $_close)) {
  106. throw new SmartyException("unmatched {block} {/block} pairs in template {$_component->type} '{$_component->name}'");
  107. }
  108. preg_match_all("!{$_ldl}{$al}block\s(.+?)\s*{$_rdl}|{$_ldl}{$al}/block\s*{$_rdl}|{$_ldl}\*([\S\s]*?)\*{$_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
  109. $_result_count = count($_result[0]);
  110. $_start = 0;
  111. while ($_start+1 < $_result_count) {
  112. $_end = 0;
  113. $_level = 1;
  114. if (($this->mbstring_overload ? mb_substr($_result[0][$_start][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
  115. $_start++;
  116. continue;
  117. }
  118. while ($_level != 0) {
  119. $_end++;
  120. if (($this->mbstring_overload ? mb_substr($_result[0][$_start + $_end][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start + $_end][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
  121. continue;
  122. }
  123. if (!strpos($_result[0][$_start + $_end][0], '/')) {
  124. $_level++;
  125. } else {
  126. $_level--;
  127. }
  128. }
  129. $_block_content = str_replace($source->smarty->left_delimiter . '$smarty.block.parent' . $source->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
  130. ($this->mbstring_overload ? mb_substr($_content, $_result[0][$_start][1] + mb_strlen($_result[0][$_start][0], 'latin1'), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + mb_strlen($_result[0][$_start][0], 'latin1'), 'latin1') : 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]))));
  131. Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $source->template, $_component->filepath);
  132. $_start = $_start + $_end + 1;
  133. }
  134. } else {
  135. return $_content;
  136. }
  137. }
  138. }
  139. /**
  140. * Determine basename for compiled filename
  141. *
  142. * @param Smarty_Template_Source $source source object
  143. * @return string resource's basename
  144. */
  145. public function getBasename(Smarty_Template_Source $source)
  146. {
  147. return str_replace(':', '.', basename($source->filepath));
  148. }
  149. }
  150. ?>