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