Skip to content

Commit

Permalink
Merge pull request #61 from thierry2015/fix/reset-factory-state-on-ex…
Browse files Browse the repository at this point in the history
…ception

Restore view factory state on caught exception
  • Loading branch information
Flynsarmy authored Feb 5, 2024
2 parents 5336b97 + 623ff42 commit b60df20
Showing 1 changed file with 29 additions and 13 deletions.
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

0 comments on commit b60df20

Please sign in to comment.