123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
-
- namespace Illuminate\View\Compilers\Concerns;
-
- trait CompilesEchos
- {
- /**
- * Compile Blade echos into valid PHP.
- *
- * @param string $value
- * @return string
- */
- protected function compileEchos($value)
- {
- foreach ($this->getEchoMethods() as $method) {
- $value = $this->$method($value);
- }
-
- return $value;
- }
-
- /**
- * Get the echo methods in the proper order for compilation.
- *
- * @return array
- */
- protected function getEchoMethods()
- {
- return [
- 'compileRawEchos',
- 'compileEscapedEchos',
- 'compileRegularEchos',
- ];
- }
-
- /**
- * Compile the "raw" echo statements.
- *
- * @param string $value
- * @return string
- */
- protected function compileRawEchos($value)
- {
- $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]);
-
- $callback = function ($matches) {
- $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
-
- return $matches[1] ? substr($matches[0], 1) : "<?php echo {$this->compileEchoDefaults($matches[2])}; ?>{$whitespace}";
- };
-
- return preg_replace_callback($pattern, $callback, $value);
- }
-
- /**
- * Compile the "regular" echo statements.
- *
- * @param string $value
- * @return string
- */
- protected function compileRegularEchos($value)
- {
- $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);
-
- $callback = function ($matches) {
- $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
-
- $wrapped = sprintf($this->echoFormat, $this->compileEchoDefaults($matches[2]));
-
- return $matches[1] ? substr($matches[0], 1) : "<?php echo {$wrapped}; ?>{$whitespace}";
- };
-
- return preg_replace_callback($pattern, $callback, $value);
- }
-
- /**
- * Compile the escaped echo statements.
- *
- * @param string $value
- * @return string
- */
- protected function compileEscapedEchos($value)
- {
- $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->escapedTags[0], $this->escapedTags[1]);
-
- $callback = function ($matches) {
- $whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];
-
- return $matches[1] ? $matches[0] : "<?php echo e({$this->compileEchoDefaults($matches[2])}); ?>{$whitespace}";
- };
-
- return preg_replace_callback($pattern, $callback, $value);
- }
-
- /**
- * Compile the default values for the echo statement.
- *
- * @param string $value
- * @return string
- */
- public function compileEchoDefaults($value)
- {
- return preg_replace('/^(?=\$)(.+?)(?:\s+or\s+)(.+?)$/si', 'isset($1) ? $1 : $2', $value);
- }
- }
|