From 7e7d681ad25e06b60f3dd2f76d09e2c1edb97ec2 Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Fri, 14 Jul 2017 13:36:13 +0100 Subject: [PATCH] Prevent Blade from parsing Blade codeinside the @php and @endphp blocks --- .../View/Compilers/BladeCompiler.php | 47 +++++++++++++------ .../Compilers/Concerns/CompilesRawPhp.php | 14 ++---- .../View/Blade/BladeEndphpStatementsTest.php | 13 ----- tests/View/Blade/BladePhpStatementsTest.php | 30 +++++++++++- 4 files changed, 65 insertions(+), 39 deletions(-) delete mode 100644 tests/View/Blade/BladeEndphpStatementsTest.php diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 56925c38f79d..a64608889ebd 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -96,18 +96,18 @@ class BladeCompiler extends Compiler implements CompilerInterface protected $footer = []; /** - * Placeholder to temporary mark the position of verbatim blocks. + * Placeholder to temporary mark the position of raw blocks. * * @var string */ - protected $verbatimPlaceholder = '@__verbatim__@'; + protected $rawPlaceholder = '@__raw-block__@'; /** - * Array to temporary store the verbatim blocks found in the template. + * Array to temporary store the raw blocks found in the template. * * @var array */ - protected $verbatimBlocks = []; + protected $rawBlocks = []; /** * Compile the view at the given path. @@ -157,14 +157,18 @@ public function setPath($path) */ public function compileString($value) { - $result = ''; - if (strpos($value, '@verbatim') !== false) { $value = $this->storeVerbatimBlocks($value); } $this->footer = []; + if (strpos($value, '@php') !== false) { + $value = $this->storePhpBlocks($value); + } + + $result = ''; + // Here we will loop through all of the tokens returned by the Zend lexer and // parse each one into the corresponding valid PHP. We will then have this // template as the correctly rendered PHP that can be rendered natively. @@ -172,8 +176,8 @@ public function compileString($value) $result .= is_array($token) ? $this->parseToken($token) : $token; } - if (! empty($this->verbatimBlocks)) { - $result = $this->restoreVerbatimBlocks($result); + if (! empty($this->rawBlocks)) { + $result = $this->restoreRawContent($result); } // If there are any footer lines that need to get added to a template we will @@ -195,9 +199,24 @@ public function compileString($value) protected function storeVerbatimBlocks($value) { return preg_replace_callback('/(?verbatimBlocks[] = $matches[1]; + $this->rawBlocks[] = $matches[1]; + + return $this->rawPlaceholder; + }, $value); + } + + /** + * Store the PHP blocks and replace them with a temporary placeholder. + * + * @param string $value + * @return string + */ + protected function storePhpBlocks($value) + { + return preg_replace_callback('/(?rawBlocks[] = ""; - return $this->verbatimPlaceholder; + return $this->rawPlaceholder; }, $value); } @@ -207,13 +226,13 @@ protected function storeVerbatimBlocks($value) * @param string $result * @return string */ - protected function restoreVerbatimBlocks($result) + protected function restoreRawContent($result) { - $result = preg_replace_callback('/'.preg_quote($this->verbatimPlaceholder).'/', function () { - return array_shift($this->verbatimBlocks); + $result = preg_replace_callback('/'.preg_quote($this->rawPlaceholder).'/', function () { + return array_shift($this->rawBlocks); }, $result); - $this->verbatimBlocks = []; + $this->rawBlocks = []; return $result; } diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php b/src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php index b49a386e3010..41c7edfd50bc 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesRawPhp.php @@ -12,17 +12,11 @@ trait CompilesRawPhp */ protected function compilePhp($expression) { - return $expression ? "" : '"; + } - /** - * Compile end-php statements into valid PHP. - * - * @return string - */ - protected function compileEndphp() - { - return ' ?>'; + return '@php'; } /** diff --git a/tests/View/Blade/BladeEndphpStatementsTest.php b/tests/View/Blade/BladeEndphpStatementsTest.php deleted file mode 100644 index 7be6073ab834..000000000000 --- a/tests/View/Blade/BladeEndphpStatementsTest.php +++ /dev/null @@ -1,13 +0,0 @@ -'; - $this->assertEquals($expected, $this->compiler->compileString($string)); - } -} diff --git a/tests/View/Blade/BladePhpStatementsTest.php b/tests/View/Blade/BladePhpStatementsTest.php index 4b3ee79e084e..410412f9336d 100644 --- a/tests/View/Blade/BladePhpStatementsTest.php +++ b/tests/View/Blade/BladePhpStatementsTest.php @@ -11,10 +11,36 @@ public function testPhpStatementsWithExpressionAreCompiled() $this->assertEquals($expected, $this->compiler->compileString($string)); } - public function testPhpStatementsWithoutExpressionAreCompiled() + public function testPhpStatementsWithoutExpressionAreIgnored() { $string = '@php'; - $expected = 'assertEquals($expected, $this->compiler->compileString($string)); + + $string = '{{ "Ignore: @php" }}'; + $expected = ''; + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testPhpStatementsDontParseBladeCode() + { + $string = '@php echo "{{ This is a blade tag }}" @endphp'; + $expected = ''; + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testVerbatimAndPhpStatementsDontGetMixedUp() + { + $string = "@verbatim {{ Hello, I'm not blade! }}" + ."\n@php echo 'And I'm not PHP!' @endphp" + ."\n@endverbatim {{ 'I am Blade' }}" + ."\n@php echo 'I am PHP {{ not Blade }}' @endphp"; + + $expected = " {{ Hello, I'm not blade! }}" + ."\n@php echo 'And I'm not PHP!' @endphp" + ."\n " + ."\n\n"; + $this->assertEquals($expected, $this->compiler->compileString($string)); } }