smarty_internal_compile_break.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Break
  4. * Compiles the {break} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Break Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $optional_attributes = array('levels');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('levels');
  32. /**
  33. * Compiles code for the {break} tag
  34. *
  35. * @param array $args array with attributes from parser
  36. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  37. * @param array $parameter array with compilation parameter
  38. *
  39. * @return string compiled code
  40. */
  41. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
  42. {
  43. list($levels, $foreachLevels) = $this->checkLevels($args, $compiler);
  44. $output = "<?php\n";
  45. if ($foreachLevels) {
  46. /* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
  47. $foreachCompiler = $compiler->getTagCompiler('foreach');
  48. $output .= $foreachCompiler->compileRestore($foreachLevels);
  49. }
  50. $output .= "break {$levels};?>";
  51. return $output;
  52. }
  53. /**
  54. * check attributes and return array of break and foreach levels
  55. *
  56. * @param array $args array with attributes from parser
  57. * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  58. * @param string $tag tag name
  59. *
  60. * @return array
  61. * @throws \SmartyCompilerException
  62. */
  63. public function checkLevels($args, Smarty_Internal_TemplateCompilerBase $compiler, $tag = 'break')
  64. {
  65. static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true);
  66. // check and get attributes
  67. $_attr = $this->getAttributes($compiler, $args);
  68. if ($_attr[ 'nocache' ] === true) {
  69. $compiler->trigger_template_error('nocache option not allowed', null, true);
  70. }
  71. if (isset($_attr[ 'levels' ])) {
  72. if (!is_numeric($_attr[ 'levels' ])) {
  73. $compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
  74. }
  75. $levels = $_attr[ 'levels' ];
  76. } else {
  77. $levels = 1;
  78. }
  79. $level_count = $levels;
  80. $stack_count = count($compiler->_tag_stack) - 1;
  81. $foreachLevels = 0;
  82. $lastTag = '';
  83. while ($level_count >= 0 && $stack_count >= 0) {
  84. if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) {
  85. $lastTag = $compiler->_tag_stack[ $stack_count ][ 0 ];
  86. if ($level_count === 0) {
  87. break;
  88. }
  89. $level_count --;
  90. if ($compiler->_tag_stack[ $stack_count ][ 0 ] === 'foreach') {
  91. $foreachLevels ++;
  92. }
  93. }
  94. $stack_count --;
  95. }
  96. if ($level_count != 0) {
  97. $compiler->trigger_template_error("cannot {$tag} {$levels} level(s)", null, true);
  98. }
  99. if ($lastTag === 'foreach' && $tag === 'break') {
  100. $foreachLevels --;
  101. }
  102. return array($levels, $foreachLevels);
  103. }
  104. }