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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * @throws \SmartyException
  71. */
  72. public function scanForProperties($attributes, Smarty_Internal_TemplateCompilerBase $compiler)
  73. {
  74. $this->propertyPreg = '~(';
  75. $this->startOffset = 0;
  76. $this->resultOffsets = array();
  77. $this->matchResults = array('named' => array(), 'item' => array());
  78. if ($this->isNamed) {
  79. $this->buildPropertyPreg(true, $attributes);
  80. }
  81. if (isset($this->itemProperties)) {
  82. if ($this->isNamed) {
  83. $this->propertyPreg .= '|';
  84. }
  85. $this->buildPropertyPreg(false, $attributes);
  86. }
  87. $this->propertyPreg .= ')\W~i';
  88. // Template source
  89. $this->matchTemplateSource($compiler);
  90. // Parent template source
  91. $this->matchParentTemplateSource($compiler);
  92. // {block} source
  93. $this->matchBlockSource($compiler);
  94. }
  95. /**
  96. * Build property preg string
  97. *
  98. * @param bool $named
  99. * @param array $attributes
  100. */
  101. public function buildPropertyPreg($named, $attributes)
  102. {
  103. if ($named) {
  104. $this->resultOffsets[ 'named' ] = $this->startOffset + 4;
  105. $this->propertyPreg .= "(([\$]smarty[.]{$this->tagName}[.]" . ($this->tagName === 'section' ? "|[\[]\s*" : '')
  106. . "){$attributes['name']}[.](";
  107. $properties = $this->nameProperties;
  108. } else {
  109. $this->resultOffsets[ 'item' ] = $this->startOffset + 3;
  110. $this->propertyPreg .= "([\$]{$attributes['item']}[@](";
  111. $properties = $this->itemProperties;
  112. }
  113. $this->startOffset += count($properties) + 2;
  114. $propName = reset($properties);
  115. while ($propName) {
  116. $this->propertyPreg .= "({$propName})";
  117. $propName = next($properties);
  118. if ($propName) {
  119. $this->propertyPreg .= '|';
  120. }
  121. }
  122. $this->propertyPreg .= '))';
  123. }
  124. /**
  125. * Find matches in source string
  126. *
  127. * @param string $source
  128. */
  129. public function matchProperty($source)
  130. {
  131. preg_match_all($this->propertyPreg, $source, $match, PREG_SET_ORDER);
  132. foreach ($this->resultOffsets as $key => $offset) {
  133. foreach ($match as $m) {
  134. if (isset($m[ $offset ]) && !empty($m[ $offset ])) {
  135. $this->matchResults[ $key ][ strtolower($m[ $offset ]) ] = true;
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * Find matches in template source
  142. *
  143. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  144. */
  145. public function matchTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
  146. {
  147. $this->matchProperty($compiler->parser->lex->data);
  148. }
  149. /**
  150. * Find matches in all parent template source
  151. *
  152. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  153. *
  154. * @throws \SmartyException
  155. */
  156. public function matchParentTemplateSource(Smarty_Internal_TemplateCompilerBase $compiler)
  157. {
  158. // search parent compiler template source
  159. $nextCompiler = $compiler;
  160. while ($nextCompiler !== $nextCompiler->parent_compiler) {
  161. $nextCompiler = $nextCompiler->parent_compiler;
  162. if ($compiler !== $nextCompiler) {
  163. // get template source
  164. $_content = $nextCompiler->template->source->getContent();
  165. if ($_content !== '') {
  166. // run pre filter if required
  167. if ((isset($nextCompiler->smarty->autoload_filters[ 'pre' ]) ||
  168. isset($nextCompiler->smarty->registered_filters[ 'pre' ]))
  169. ) {
  170. $_content = $nextCompiler->smarty->ext->_filterHandler->runFilter('pre', $_content,
  171. $nextCompiler->template);
  172. }
  173. $this->matchProperty($_content);
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * Find matches in {block} tag source
  180. *
  181. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  182. */
  183. public function matchBlockSource(Smarty_Internal_TemplateCompilerBase $compiler)
  184. {
  185. }
  186. /**
  187. * Compiles code for the {$smarty.foreach.xxx} or {$smarty.section.xxx}tag
  188. *
  189. * @param array $args array with attributes from parser
  190. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  191. * @param array $parameter array with compilation parameter
  192. *
  193. * @return string compiled code
  194. * @throws \SmartyCompilerException
  195. */
  196. public function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  197. {
  198. $tag = strtolower(trim($parameter[ 0 ], '"\''));
  199. $name = isset($parameter[ 1 ]) ? $compiler->getId($parameter[ 1 ]) : false;
  200. if (!$name) {
  201. $compiler->trigger_template_error("missing or illegal \$smarty.{$tag} name attribute", null, true);
  202. }
  203. $property = isset($parameter[ 2 ]) ? strtolower($compiler->getId($parameter[ 2 ])) : false;
  204. if (!$property || !in_array($property, $this->nameProperties)) {
  205. $compiler->trigger_template_error("missing or illegal \$smarty.{$tag} property attribute", null, true);
  206. }
  207. $tagVar = "'__smarty_{$tag}_{$name}'";
  208. return "(isset(\$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$tagVar}]->value['{$property}'] : null)";
  209. }
  210. }