Skip to content

Restore view factory state on caught exception #61

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

Merged
Merged
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
42 changes: 29 additions & 13 deletions src/Flynsarmy/DbBladeCompiler/DbView.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php namespace Flynsarmy\DbBladeCompiler;

use View, Closure, ArrayAccess;
use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Config\Repository;
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\View as BaseView;
use Illuminate\Support\Facades\View;
use Throwable;

class DbView extends \Illuminate\View\View implements ArrayAccess, Renderable
class DbView extends BaseView implements ArrayAccess, Renderable
{

protected $content_field = null;
Expand Down Expand Up @@ -67,20 +70,33 @@ public function field($content_field)
*/
public function render(callable $callback = null)
{
$contents = $this->renderContents();
$usesState = version_compare(app()->version(), '5.4.0') >= 0;

$response = isset($callback) ? $callback($this, $contents) : null;
try {
$contents = $this->renderContents();

// 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
// anothoer view is rendered in the future by the application developers.
// Before flushing, check Laravel version for correct method use
if (version_compare(app()->version(), '5.4.0') >= 0)
View::flushStateIfDoneRendering();
else
View::flushSectionsIfDoneRendering();
$response = isset($callback) ? $callback($this, $contents) : null;

return $response ?: $contents;
// 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
// anothoer view is rendered in the future by the application developers.
// Before flushing, check Laravel version for correct method use
if ($usesState) {
View::flushStateIfDoneRendering();
} else {
View::flushSectionsIfDoneRendering();
}

return $response ?: $contents;
} catch (Throwable $exception) {
if ($usesState) {
View::flushState();
} else {
View::flushSections();
}

throw $exception;
}
}

/**
Expand Down