From 8174e9dc868041536d56a76a16df0100372b8537 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Thu, 16 Dec 2021 13:49:47 -0500 Subject: [PATCH 1/2] Consistently negate conditions in @includeUnless() Blade directive Before this, `@includeUnless($undefined ?? true, 'template')` would result in a PHP Warning and an unexpected condition result, because `$undefined ?? true` would be compiled as `! $undefined ?? true`, which due to `!` having precedence over `??` is different than `! ($undefined ?? true)` (which is how `@unless` would compile it). This necessitated the creation of `renderUnless()`, because `@includeUnless()` accepts multiple arguments, and parsing out the first argument to a Blade directive is fraught. So `renderUnless()` takes the parameters as-is, and negates the condition inside the method. --- .../Compilers/Concerns/CompilesIncludes.php | 2 +- src/Illuminate/View/Factory.php | 18 ++++++++++++++++++ tests/View/Blade/BladeIncludesTest.php | 6 ++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php b/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php index b80a5b5d21a7..aa3d4a6f5b86 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php @@ -64,7 +64,7 @@ protected function compileIncludeUnless($expression) { $expression = $this->stripParentheses($expression); - return "renderWhen(! $expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>"; + return "renderUnless($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>"; } /** diff --git a/src/Illuminate/View/Factory.php b/src/Illuminate/View/Factory.php index eda479db8468..f0b366675303 100755 --- a/src/Illuminate/View/Factory.php +++ b/src/Illuminate/View/Factory.php @@ -189,6 +189,24 @@ public function renderWhen($condition, $view, $data = [], $mergeData = []) return $this->make($view, $this->parseData($data), $mergeData)->render(); } + /** + * Get the rendered content of the view based on the negation of a given condition. + * + * @param bool $condition + * @param string $view + * @param \Illuminate\Contracts\Support\Arrayable|array $data + * @param array $mergeData + * @return string + */ + public function renderUnless($condition, $view, $data = [], $mergeData = []) + { + if ($condition) { + return ''; + } + + return $this->make($view, $this->parseData($data), $mergeData)->render(); + } + /** * Get the rendered contents of a partial from a loop. * diff --git a/tests/View/Blade/BladeIncludesTest.php b/tests/View/Blade/BladeIncludesTest.php index 1273ded2c138..0cf5e3a1d931 100644 --- a/tests/View/Blade/BladeIncludesTest.php +++ b/tests/View/Blade/BladeIncludesTest.php @@ -28,6 +28,12 @@ public function testIncludeWhensAreCompiled() $this->assertSame('renderWhen(true, \'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeWhen(true, \'foo\')')); } + public function testIncludeUnlessesAreCompiled() + { + $this->assertSame('renderUnless(true, \'foo\', ["foo" => "bar"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeUnless(true, \'foo\', ["foo" => "bar"])')); + $this->assertSame('renderUnless($undefined ?? true, \'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\'])); ?>', $this->compiler->compileString('@includeUnless($undefined ?? true, \'foo\')')); + } + public function testIncludeFirstsAreCompiled() { $this->assertSame('first(["one", "two"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"])')); From 10dfd0b517572804d2e2762205f4d1364c5bab8d Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Thu, 16 Dec 2021 14:16:44 -0500 Subject: [PATCH 2/2] DRY renderUnless() by having it use renderWhen() --- src/Illuminate/View/Factory.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Illuminate/View/Factory.php b/src/Illuminate/View/Factory.php index f0b366675303..de431f77e41f 100755 --- a/src/Illuminate/View/Factory.php +++ b/src/Illuminate/View/Factory.php @@ -200,11 +200,7 @@ public function renderWhen($condition, $view, $data = [], $mergeData = []) */ public function renderUnless($condition, $view, $data = [], $mergeData = []) { - if ($condition) { - return ''; - } - - return $this->make($view, $this->parseData($data), $mergeData)->render(); + return $this->renderWhen(! $condition, $view, $data, $mergeData); } /**