You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_private_foreachsection.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile ForeachSection
  4. * Shared methods for {foreach} {section} tags
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile ForeachSection Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Private_ForeachSection extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Preg search pattern
  20. *
  21. * @var string
  22. */
  23. private $propertyPreg = '';
  24. /**
  25. * Offsets in preg match result
  26. *
  27. * @var array
  28. */
  29. private $resultOffsets = array();
  30. /**
  31. * Start offset
  32. *
  33. * @var int
  34. */
  35. private $startOffset = 0;
  36. /**
  37. * Name of this tag
  38. *
  39. * @var string
  40. */
  41. public $tagName = '';
  42. /**
  43. * Valid properties of $smarty.xxx variable
  44. *
  45. * @var array
  46. */
  47. public $nameProperties = array();
  48. /**
  49. * {section} tag has no item properties
  50. *
  51. * @var array
  52. */
  53. public $itemProperties = null;
  54. /**
  55. * {section} tag has always name attribute
  56. *
  57. * @var bool
  58. */
  59. public $isNamed = true;
  60. /**
  61. * @var array
  62. */
  63. public $matchResults = array();
  64. /**
  65. * Scan sources for used tag attributes
  66. *
  67. * @param array $attributes
  68. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  69. */
  70. public function scanForProperties($attributes, Smarty_Internal_TemplateCompilerBase $compiler)
  71. {
  72. $this->propertyPreg = '~(';
  73. $this->startOffset = 0;
  74. $this->resultOffsets = array();
  75. $this->matchResults = array('named' => array(), 'item' => array());
  76. if ($this->isNamed) {
  77. $this->buildPropertyPreg(true, $attributes);
  78. }
  79. if (isset($this->itemProperties)) {
  80. if ($this->isNamed) {
  81. $this->propertyPreg .= '|';
  82. }
  83. $this->buildPropertyPreg(false, $attributes);
  84. }
  85. $this->propertyPreg .= ')\W~i';
  86. // Template source
  87. $this->matchTemplateSource($compiler);
  88. // Parent template source
  89. $this->matchParentTemplateSource($compiler);
  90. // {block} source
  91. $this->matchBlockSource($compiler);
  92. }
  93. /**
  94. * Build property preg string
  95. *
  96. * @param bool $named
  97. * @param array $attributes
  98. */
  99. public function buildPropertyPreg($named, $attributes)
  100. {
  101. if ($named) {
  102. $this->resultOffsets['named'] = $this->startOffset + 3;
  103. $this->propertyPreg .= "([\$]smarty[.]{$this->tagName}[.]{$attributes['name']}[.](";
  104. $properties = $this->nameProperties;
  105. } else {
  106. $this->resultOffsets['item'] = $this->startOffset + 3;
  107. $this->propertyPreg .= "([\$]{$attributes['item']}[@](";
  108. $properties = $this->itemProperties;
  109. }
  110. $this->startOffset += count($properties) + 2;
  111. $propName = reset($properties);
  112. while ($propName) {
  113. $this->propertyPreg .= "({$propName})";
  114. $propName = next($properties);
  115. if ($propName) {
  116. $this->propertyPreg .= '|';
  117. }
  118. }
  119. $this->propertyPreg .= '))';
  120. }
  121. /**
  122. * Find matches in source string
  123. *
  124. * @param string $source
  125. */
  126. public function matchProperty($source)
  127. {
  128. preg_match_all($this->propertyPreg, $source, $match, PREG_SET_ORDER);
  129. foreach ($this->resultOffsets as $key => $offset) {
  130. foreach ($match as $m) {
  131. if (isset($m[$offset]) && !empty($m[$offset])) {
  132. $this->matchResults[$key][strtolower($m[$offset])] = true;
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Find matches in template source
  139. *
  140. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  141. */
  142. public function matchTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
  143. {
  144. $this->matchProperty($compiler->parser->lex->data);
  145. }
  146. /**
  147. * Find matches in all parent template source
  148. *
  149. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  150. */
  151. public function matchParentTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
  152. {
  153. // search parent compiler template source
  154. $nextCompiler = $compiler;
  155. while ($nextCompiler !== $nextCompiler->parent_compiler) {
  156. $nextCompiler = $nextCompiler->parent_compiler;
  157. if ($compiler !== $nextCompiler) {
  158. // get template source
  159. $_content = $nextCompiler->template->source->getContent();
  160. if ($_content != '') {
  161. // run pre filter if required
  162. if ((isset($nextCompiler->smarty->autoload_filters['pre']) ||
  163. isset($nextCompiler->smarty->registered_filters['pre']))) {
  164. $_content = $nextCompiler->smarty->ext->_filter_Handler->runFilter('pre', $_content, $nextCompiler->template);
  165. }
  166. $this->matchProperty($_content);
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * Find matches in {block} tag source
  173. *
  174. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  175. */
  176. public function matchBlockSource(Smarty_Internal_TemplateCompilerBase $compiler)
  177. {
  178. }
  179. /**
  180. * Compiles code for the {$smarty.foreach.xxx} or {$smarty.section.xxx}tag
  181. *
  182. * @param array $args array with attributes from parser
  183. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  184. * @param array $parameter array with compilation parameter
  185. *
  186. * @return string compiled code
  187. * @throws \SmartyCompilerException
  188. */
  189. public function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  190. {
  191. $tag = strtolower(trim($parameter[ 0 ], '"\''));
  192. $name = isset($parameter[ 1 ]) ? $compiler->getId($parameter[ 1 ]) : false;
  193. if (!$name) {
  194. $compiler->trigger_template_error("missing or illegal \$smarty.{$tag} name attribute", null, true);
  195. }
  196. $property = isset($parameter[ 2 ]) ? strtolower($compiler->getId($parameter[ 2 ])) : false;
  197. if (!$property || !in_array($property, $this->nameProperties)) {
  198. $compiler->trigger_template_error("missing or illegal \$smarty.{$tag} property attribute", null, true);
  199. }
  200. $tagVar = "'__smarty_{$tag}_{$name}'";
  201. return "(isset(\$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}'] : null)";
  202. }
  203. }