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.6] View Cleaning #17018

Merged
merged 6 commits into from
Dec 28, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
taylorotwell committed Dec 28, 2016
commit c69ff384ceae50a58a90e3e9e75f7ececa000fbd
39 changes: 11 additions & 28 deletions src/Illuminate/View/Concerns/ManagesLayouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,34 +171,6 @@ public static function parentPlaceholder()
return static::$parentPlaceholder;
}

/**
* Flush all of the section contents.
*
* @return void
*/
public function flushSections()
{
$this->renderCount = 0;

$this->sections = [];
$this->sectionStack = [];

$this->pushes = [];
$this->pushStack = [];
}

/**
* Flush all of the section contents if done rendering.
*
* @return void
*/
public function flushSectionsIfDoneRendering()
{
if ($this->doneRendering()) {
$this->flushSections();
}
}

/**
* Check if section exists.
*
Expand All @@ -219,4 +191,15 @@ public function getSections()
{
return $this->sections;
}

/**
* Flush all of the sections.
*
* @return void
*/
public function flushSections()
{
$this->sections = [];
$this->sectionStack = [];
}
}
17 changes: 14 additions & 3 deletions src/Illuminate/View/Concerns/ManagesStacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,21 @@ protected function extendPush($section, $content)
*/
public function yieldPushContent($section, $default = '')
{
if (! isset($this->pushes[$section])) {
return $default;
if (isset($this->pushes[$section])) {
return implode($this->pushes[$section]);
}

return implode($this->pushes[$section]);
return $default;
}

/**
* Flush all of the stacks.
*
* @return void
*/
public function flushStacks()
{
$this->pushes = [];
$this->pushStack = [];
}
}
25 changes: 25 additions & 0 deletions src/Illuminate/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,31 @@ public function addExtension($extension, $engine, $resolver = null)
$this->extensions = array_merge([$extension => $engine], $this->extensions);
}

/**
* Flush all of the factory state like sections and stacks.
*
* @return void
*/
public function flushState()
{
$this->renderCount = 0;

$this->flushSections();
$this->flushStacks();
}

/**
* Flush all of the section contents if done rendering.
*
* @return void
*/
public function flushStateIfDoneRendering()
{
if ($this->doneRendering()) {
$this->flushState();
}
}

/**
* Get the extension to engine bindings.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ public function render(callable $callback = null)
// Once we have the contents of the view, we will flush the sections if we are
// done rendering all views so that there is nothing left hanging over when
// another view gets rendered in the future by the application developer.
$this->factory->flushSectionsIfDoneRendering();
$this->factory->flushStateIfDoneRendering();

return ! is_null($response) ? $response : $contents;
} catch (Exception $e) {
$this->factory->flushSections();
$this->factory->flushState();

throw $e;
} catch (Throwable $e) {
$this->factory->flushSections();
$this->factory->flushState();

throw $e;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testRenderProperlyRendersView()
$view->getFactory()->shouldReceive('getShared')->once()->andReturn(['shared' => 'foo']);
$view->getEngine()->shouldReceive('get')->once()->with('path', ['foo' => 'bar', 'shared' => 'foo'])->andReturn('contents');
$view->getFactory()->shouldReceive('decrementRender')->once()->ordered();
$view->getFactory()->shouldReceive('flushSectionsIfDoneRendering')->once();
$view->getFactory()->shouldReceive('flushStateIfDoneRendering')->once();

$callback = function (View $rendered, $contents) use ($view) {
$this->assertEquals($view, $rendered);
Expand All @@ -48,7 +48,7 @@ public function testRenderHandlingCallbackReturnValues()
$view->getFactory()->shouldReceive('getShared')->andReturn(['shared' => 'foo']);
$view->getEngine()->shouldReceive('get')->andReturn('contents');
$view->getFactory()->shouldReceive('decrementRender');
$view->getFactory()->shouldReceive('flushSectionsIfDoneRendering');
$view->getFactory()->shouldReceive('flushStateIfDoneRendering');

$this->assertEquals('new contents', $view->render(function () {
return 'new contents';
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testSectionsAreNotFlushedWhenNotDoneRendering()
$view->getFactory()->shouldReceive('getShared')->twice()->andReturn(['shared' => 'foo']);
$view->getEngine()->shouldReceive('get')->twice()->with('path', ['foo' => 'bar', 'shared' => 'foo'])->andReturn('contents');
$view->getFactory()->shouldReceive('decrementRender')->twice();
$view->getFactory()->shouldReceive('flushSectionsIfDoneRendering')->twice();
$view->getFactory()->shouldReceive('flushStateIfDoneRendering')->twice();

$this->assertEquals('contents', $view->render());
$this->assertEquals('contents', (string) $view);
Expand Down Expand Up @@ -177,7 +177,7 @@ public function testViewGatherDataWithRenderable()
$view->getFactory()->shouldReceive('getShared')->once()->andReturn(['shared' => 'foo']);
$view->getEngine()->shouldReceive('get')->once()->andReturn('contents');
$view->getFactory()->shouldReceive('decrementRender')->once()->ordered();
$view->getFactory()->shouldReceive('flushSectionsIfDoneRendering')->once();
$view->getFactory()->shouldReceive('flushStateIfDoneRendering')->once();

$view->renderable = m::mock('Illuminate\Contracts\Support\Renderable');
$view->renderable->shouldReceive('render')->once()->andReturn('text');
Expand All @@ -192,7 +192,7 @@ public function testViewRenderSections()
$view->getFactory()->shouldReceive('getShared')->once()->andReturn(['shared' => 'foo']);
$view->getEngine()->shouldReceive('get')->once()->andReturn('contents');
$view->getFactory()->shouldReceive('decrementRender')->once()->ordered();
$view->getFactory()->shouldReceive('flushSectionsIfDoneRendering')->once();
$view->getFactory()->shouldReceive('flushStateIfDoneRendering')->once();

$view->getFactory()->shouldReceive('getSections')->once()->andReturn(['foo', 'bar']);
$sections = $view->renderSections();
Expand Down