Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Blade push with layouts weird ordering #12808

Merged
merged 3 commits into from
Mar 28, 2016
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
6 changes: 3 additions & 3 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ protected function compileInclude($expression)
*/
protected function compileStack($expression)
{
return "<?php echo \$__env->yieldContent{$expression}; ?>";
return "<?php echo \$__env->yieldPushContent{$expression}; ?>";
}

/**
Expand All @@ -844,7 +844,7 @@ protected function compileStack($expression)
*/
protected function compilePush($expression)
{
return "<?php \$__env->startSection{$expression}; ?>";
return "<?php \$__env->startPush{$expression}; ?>";
}

/**
Expand All @@ -855,7 +855,7 @@ protected function compilePush($expression)
*/
protected function compileEndpush($expression)
{
return '<?php $__env->appendSection(); ?>';
return '<?php $__env->stopPush(); ?>';
}

/**
Expand Down
86 changes: 86 additions & 0 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,27 @@ class Factory implements FactoryContract
*/
protected $sections = [];

/**
* All of the finished, captured push sections.
*
* @var array
*/
protected $pushContentsStack = [];

/**
* The stack of in-progress sections.
*
* @var array
*/
protected $sectionStack = [];

/**
* The stack of in-progress push sections.
*
* @var array
*/
protected $pushStack = [];

/**
* The number of active rendering operations.
*
Expand Down Expand Up @@ -636,6 +650,78 @@ public function yieldContent($section, $default = '')
);
}

/**
* Start injecting content into a push section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startPush($section, $content = '')
{
if ($content === '') {
if (ob_start()) {
$this->pushStack[] = $section;
}
} else {
$this->extendPush($section, $content);
}
}

/**
* Stop injecting content into a push section.
*
* @return string
* @throws \InvalidArgumentException
*/
public function stopPush()
{
if (empty($this->pushStack)) {
throw new InvalidArgumentException('Cannot end a section without first starting one.');
}

$last = array_pop($this->pushStack);

$this->extendPush($last, ob_get_clean());

return $last;
}

/**
* Append content to a given push section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendPush($section, $content)
{
if (! isset($this->pushContentsStack[$section])) {
$this->pushContentsStack[$section] = [];
}
if (! isset($this->pushContentsStack[$section][$this->renderCount])) {
$this->pushContentsStack[$section][$this->renderCount] = $content;
} else {
$this->pushContentsStack[$section][$this->renderCount] .= $content;
}
}

/**
* Get the string contents of a push section.
*
* @param string $section
* @param string $default
* @return string
*/
public function yieldPushContent($section, $default = '')
{
if (! isset($this->pushContentsStack[$section])) {
return $default;
}

return implode(array_reverse($this->pushContentsStack[$section]));
}

/**
* Flush all of the section contents.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@ public function testPushIsCompiled()
$string = '@push(\'foo\')
test
@endpush';
$expected = '<?php $__env->startSection(\'foo\'); ?>
$expected = '<?php $__env->startPush(\'foo\'); ?>
test
<?php $__env->appendSection(); ?>';
<?php $__env->stopPush(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testStackIsCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@stack(\'foo\')';
$expected = '<?php echo $__env->yieldContent(\'foo\'); ?>';
$expected = '<?php echo $__env->yieldPushContent(\'foo\'); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

Expand Down
28 changes: 20 additions & 8 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,34 @@ public function testSectionMultipleExtending()
public function testSingleStackPush()
{
$factory = $this->getFactory();
$factory->startSection('foo');
$factory->startPush('foo');
echo 'hi';
$factory->appendSection();
$this->assertEquals('hi', $factory->yieldContent('foo'));
$factory->stopPush();
$this->assertEquals('hi', $factory->yieldPushContent('foo'));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since @push uses startPush, so this tests whether the startPush, stopPush, yieldPushContent works


public function testMultipleStackPush()
{
$factory = $this->getFactory();
$factory->startSection('foo');
$factory->startPush('foo');
echo 'hi';
$factory->appendSection();
$factory->startSection('foo');
$factory->stopPush();
$factory->startPush('foo');
echo ', Hello!';
$factory->appendSection();
$this->assertEquals('hi, Hello!', $factory->yieldContent('foo'));
$factory->stopPush();
$this->assertEquals('hi, Hello!', $factory->yieldPushContent('foo'));

// mimic a parent view is rendering
$factory->incrementRender();
$factory->startPush('foo');
echo 'Dear ';
$factory->stopPush();
$factory->startPush('foo');
echo 'friend';
$factory->stopPush();
$factory->decrementRender();

$this->assertEquals('Dear friendhi, Hello!', $factory->yieldPushContent('foo'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests multiple pushes in child and parent view

}

public function testSessionAppending()
Expand Down