CompilesLoops.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Illuminate\View\Compilers\Concerns;
  3. trait CompilesLoops
  4. {
  5. /**
  6. * Counter to keep track of nested forelse statements.
  7. *
  8. * @var int
  9. */
  10. protected $forElseCounter = 0;
  11. /**
  12. * Compile the for-else statements into valid PHP.
  13. *
  14. * @param string $expression
  15. * @return string
  16. */
  17. protected function compileForelse($expression)
  18. {
  19. $empty = '$__empty_'.++$this->forElseCounter;
  20. preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);
  21. $iteratee = trim($matches[1]);
  22. $iteration = trim($matches[2]);
  23. $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
  24. $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
  25. return "<?php {$empty} = true; {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} {$empty} = false; ?>";
  26. }
  27. /**
  28. * Compile the for-else-empty and empty statements into valid PHP.
  29. *
  30. * @param string $expression
  31. * @return string
  32. */
  33. protected function compileEmpty($expression)
  34. {
  35. if ($expression) {
  36. return "<?php if(empty{$expression}): ?>";
  37. }
  38. $empty = '$__empty_'.$this->forElseCounter--;
  39. return "<?php endforeach; \$__env->popLoop(); \$loop = \$__env->getLastLoop(); if ({$empty}): ?>";
  40. }
  41. /**
  42. * Compile the end-for-else statements into valid PHP.
  43. *
  44. * @return string
  45. */
  46. protected function compileEndforelse()
  47. {
  48. return '<?php endif; ?>';
  49. }
  50. /**
  51. * Compile the end-empty statements into valid PHP.
  52. *
  53. * @return string
  54. */
  55. protected function compileEndEmpty()
  56. {
  57. return '<?php endif; ?>';
  58. }
  59. /**
  60. * Compile the for statements into valid PHP.
  61. *
  62. * @param string $expression
  63. * @return string
  64. */
  65. protected function compileFor($expression)
  66. {
  67. return "<?php for{$expression}: ?>";
  68. }
  69. /**
  70. * Compile the for-each statements into valid PHP.
  71. *
  72. * @param string $expression
  73. * @return string
  74. */
  75. protected function compileForeach($expression)
  76. {
  77. preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);
  78. $iteratee = trim($matches[1]);
  79. $iteration = trim($matches[2]);
  80. $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
  81. $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
  82. return "<?php {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>";
  83. }
  84. /**
  85. * Compile the break statements into valid PHP.
  86. *
  87. * @param string $expression
  88. * @return string
  89. */
  90. protected function compileBreak($expression)
  91. {
  92. if ($expression) {
  93. preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);
  94. return $matches ? '<?php break '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} break; ?>";
  95. }
  96. return '<?php break; ?>';
  97. }
  98. /**
  99. * Compile the continue statements into valid PHP.
  100. *
  101. * @param string $expression
  102. * @return string
  103. */
  104. protected function compileContinue($expression)
  105. {
  106. if ($expression) {
  107. preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);
  108. return $matches ? '<?php continue '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} continue; ?>";
  109. }
  110. return '<?php continue; ?>';
  111. }
  112. /**
  113. * Compile the end-for statements into valid PHP.
  114. *
  115. * @return string
  116. */
  117. protected function compileEndfor()
  118. {
  119. return '<?php endfor; ?>';
  120. }
  121. /**
  122. * Compile the end-for-each statements into valid PHP.
  123. *
  124. * @return string
  125. */
  126. protected function compileEndforeach()
  127. {
  128. return '<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
  129. }
  130. /**
  131. * Compile the while statements into valid PHP.
  132. *
  133. * @param string $expression
  134. * @return string
  135. */
  136. protected function compileWhile($expression)
  137. {
  138. return "<?php while{$expression}: ?>";
  139. }
  140. /**
  141. * Compile the end-while statements into valid PHP.
  142. *
  143. * @return string
  144. */
  145. protected function compileEndwhile()
  146. {
  147. return '<?php endwhile; ?>';
  148. }
  149. }