CompilesEchos.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Illuminate\View\Compilers\Concerns;
  3. trait CompilesEchos
  4. {
  5. /**
  6. * Compile Blade echos into valid PHP.
  7. *
  8. * @param string $value
  9. * @return string
  10. */
  11. protected function compileEchos($value)
  12. {
  13. foreach ($this->getEchoMethods() as $method) {
  14. $value = $this->$method($value);
  15. }
  16. return $value;
  17. }
  18. /**
  19. * Get the echo methods in the proper order for compilation.
  20. *
  21. * @return array
  22. */
  23. protected function getEchoMethods()
  24. {
  25. return [
  26. 'compileRawEchos',
  27. 'compileEscapedEchos',
  28. 'compileRegularEchos',
  29. ];
  30. }
  31. /**
  32. * Compile the "raw" echo statements.
  33. *
  34. * @param string $value
  35. * @return string
  36. */
  37. protected function compileRawEchos($value)
  38. {
  39. $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]);
  40. $callback = function ($matches) {
  41. $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
  42. return $matches[1] ? substr($matches[0], 1) : "<?php echo {$this->compileEchoDefaults($matches[2])}; ?>{$whitespace}";
  43. };
  44. return preg_replace_callback($pattern, $callback, $value);
  45. }
  46. /**
  47. * Compile the "regular" echo statements.
  48. *
  49. * @param string $value
  50. * @return string
  51. */
  52. protected function compileRegularEchos($value)
  53. {
  54. $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);
  55. $callback = function ($matches) {
  56. $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
  57. $wrapped = sprintf($this->echoFormat, $this->compileEchoDefaults($matches[2]));
  58. return $matches[1] ? substr($matches[0], 1) : "<?php echo {$wrapped}; ?>{$whitespace}";
  59. };
  60. return preg_replace_callback($pattern, $callback, $value);
  61. }
  62. /**
  63. * Compile the escaped echo statements.
  64. *
  65. * @param string $value
  66. * @return string
  67. */
  68. protected function compileEscapedEchos($value)
  69. {
  70. $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->escapedTags[0], $this->escapedTags[1]);
  71. $callback = function ($matches) {
  72. $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
  73. return $matches[1] ? $matches[0] : "<?php echo e({$this->compileEchoDefaults($matches[2])}); ?>{$whitespace}";
  74. };
  75. return preg_replace_callback($pattern, $callback, $value);
  76. }
  77. /**
  78. * Compile the default values for the echo statement.
  79. *
  80. * @param string $value
  81. * @return string
  82. */
  83. public function compileEchoDefaults($value)
  84. {
  85. return preg_replace('/^(?=\$)(.+?)(?:\s+or\s+)(.+?)$/si', 'isset($1) ? $1 : $2', $value);
  86. }
  87. }