CompilesConditionals.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Illuminate\View\Compilers\Concerns;
  3. trait CompilesConditionals
  4. {
  5. /**
  6. * Identifier for the first case in switch statement.
  7. *
  8. * @var bool
  9. */
  10. protected $firstCaseInSwitch = true;
  11. /**
  12. * Compile the if-auth statements into valid PHP.
  13. *
  14. * @param string|null $guard
  15. * @return string
  16. */
  17. protected function compileAuth($guard = null)
  18. {
  19. $guard = is_null($guard) ? '()' : $guard;
  20. return "<?php if(auth()->guard{$guard}->check()): ?>";
  21. }
  22. /**
  23. * Compile the else-auth statements into valid PHP.
  24. *
  25. * @param string|null $guard
  26. * @return string
  27. */
  28. protected function compileElseAuth($guard = null)
  29. {
  30. $guard = is_null($guard) ? '()' : $guard;
  31. return "<?php elseif(auth()->guard{$guard}->check()): ?>";
  32. }
  33. /**
  34. * Compile the end-auth statements into valid PHP.
  35. *
  36. * @return string
  37. */
  38. protected function compileEndAuth()
  39. {
  40. return '<?php endif; ?>';
  41. }
  42. /**
  43. * Compile the if-guest statements into valid PHP.
  44. *
  45. * @param string|null $guard
  46. * @return string
  47. */
  48. protected function compileGuest($guard = null)
  49. {
  50. $guard = is_null($guard) ? '()' : $guard;
  51. return "<?php if(auth()->guard{$guard}->guest()): ?>";
  52. }
  53. /**
  54. * Compile the else-guest statements into valid PHP.
  55. *
  56. * @param string|null $guard
  57. * @return string
  58. */
  59. protected function compileElseGuest($guard = null)
  60. {
  61. $guard = is_null($guard) ? '()' : $guard;
  62. return "<?php elseif(auth()->guard{$guard}->guest()): ?>";
  63. }
  64. /**
  65. * Compile the end-guest statements into valid PHP.
  66. *
  67. * @return string
  68. */
  69. protected function compileEndGuest()
  70. {
  71. return '<?php endif; ?>';
  72. }
  73. /**
  74. * Compile the has-section statements into valid PHP.
  75. *
  76. * @param string $expression
  77. * @return string
  78. */
  79. protected function compileHasSection($expression)
  80. {
  81. return "<?php if (! empty(trim(\$__env->yieldContent{$expression}))): ?>";
  82. }
  83. /**
  84. * Compile the if statements into valid PHP.
  85. *
  86. * @param string $expression
  87. * @return string
  88. */
  89. protected function compileIf($expression)
  90. {
  91. return "<?php if{$expression}: ?>";
  92. }
  93. /**
  94. * Compile the unless statements into valid PHP.
  95. *
  96. * @param string $expression
  97. * @return string
  98. */
  99. protected function compileUnless($expression)
  100. {
  101. return "<?php if (! {$expression}): ?>";
  102. }
  103. /**
  104. * Compile the else-if statements into valid PHP.
  105. *
  106. * @param string $expression
  107. * @return string
  108. */
  109. protected function compileElseif($expression)
  110. {
  111. return "<?php elseif{$expression}: ?>";
  112. }
  113. /**
  114. * Compile the else statements into valid PHP.
  115. *
  116. * @return string
  117. */
  118. protected function compileElse()
  119. {
  120. return '<?php else: ?>';
  121. }
  122. /**
  123. * Compile the end-if statements into valid PHP.
  124. *
  125. * @return string
  126. */
  127. protected function compileEndif()
  128. {
  129. return '<?php endif; ?>';
  130. }
  131. /**
  132. * Compile the end-unless statements into valid PHP.
  133. *
  134. * @return string
  135. */
  136. protected function compileEndunless()
  137. {
  138. return '<?php endif; ?>';
  139. }
  140. /**
  141. * Compile the if-isset statements into valid PHP.
  142. *
  143. * @param string $expression
  144. * @return string
  145. */
  146. protected function compileIsset($expression)
  147. {
  148. return "<?php if(isset{$expression}): ?>";
  149. }
  150. /**
  151. * Compile the end-isset statements into valid PHP.
  152. *
  153. * @return string
  154. */
  155. protected function compileEndIsset()
  156. {
  157. return '<?php endif; ?>';
  158. }
  159. /**
  160. * Compile the switch statements into valid PHP.
  161. *
  162. * @param string $expression
  163. * @return string
  164. */
  165. protected function compileSwitch($expression)
  166. {
  167. $this->firstCaseInSwitch = true;
  168. return "<?php switch{$expression}:";
  169. }
  170. /**
  171. * Compile the case statements into valid PHP.
  172. *
  173. * @param string $expression
  174. * @return string
  175. */
  176. protected function compileCase($expression)
  177. {
  178. if ($this->firstCaseInSwitch) {
  179. $this->firstCaseInSwitch = false;
  180. return "case {$expression}: ?>";
  181. }
  182. return "<?php case {$expression}: ?>";
  183. }
  184. /**
  185. * Compile the default statements in switch case into valid PHP.
  186. *
  187. * @return string
  188. */
  189. protected function compileDefault()
  190. {
  191. return '<?php default: ?>';
  192. }
  193. /**
  194. * Compile the end switch statements into valid PHP.
  195. *
  196. * @return string
  197. */
  198. protected function compileEndSwitch()
  199. {
  200. return '<?php endswitch; ?>';
  201. }
  202. }