Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ protected function compileHasSection($expression)
return "<?php if (! empty(trim(\$__env->yieldContent{$expression}))): ?>";
}

/**
* Compile the has-stack statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileHasStack($expression)
{
return "<?php if (! \$__env->isStackEmpty{$expression}): ?>";
}

/**
* Compile the section-missing statements into valid PHP.
*
Expand Down
10 changes: 9 additions & 1 deletion src/Illuminate/View/Concerns/ManagesStacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function extendPrepend($section, $content)
*/
public function yieldPushContent($section, $default = '')
{
if (! isset($this->pushes[$section]) && ! isset($this->prepends[$section])) {
if ($this->isStackEmpty($section)) {
return $default;
}

Expand All @@ -165,6 +165,14 @@ public function yieldPushContent($section, $default = '')
return $output;
}

/**
* Determine if the stack has any content in it.
*/
public function isStackEmpty(string $section): bool
{
return ! isset($this->pushes[$section]) && ! isset($this->prepends[$section]);
}

/**
* Flush all of the stacks.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/View/Blade/BladeHasStackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeHasStackTest extends AbstractBladeTestCase
{
public function testHasStackStatementsAreCompiled()
{
$string = '@hasStack("stack")
breeze
@endif';
$expected = '<?php if (! $__env->isStackEmpty("stack")): ?>
breeze
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}
37 changes: 37 additions & 0 deletions tests/View/Concerns/ManagesStacksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Tests\View\Concerns;

use Illuminate\View\Concerns\ManagesStacks;
use PHPUnit\Framework\TestCase;

class ManagesStacksTest extends TestCase
{
public function testStackIsEmpty()
{
$this->assertTrue((new FakeViewFactory)->isStackEmpty('my-stack'));
}

public function testStackIsNotEmptyWithPushedContent()
{
$object = new FakeViewFactory;
$object->startPush('my-stack', 'some pushed content');

$this->assertFalse($object->isStackEmpty('my-stack'));
}

public function testStackIsNotEmptyWithPrependedContent()
{
$object = new FakeViewFactory;
$object->startPrepend('my-stack', 'some prepended content');

$this->assertFalse($object->isStackEmpty('my-stack'));
}
}

class FakeViewFactory
{
use ManagesStacks;

protected int $renderCount = 0;
}
Loading