Skip to content

Commit b60df20

Browse files
authored
Merge pull request #61 from thierry2015/fix/reset-factory-state-on-exception
Restore view factory state on caught exception
2 parents 5336b97 + 623ff42 commit b60df20

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/Flynsarmy/DbBladeCompiler/DbView.php

+29-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<?php namespace Flynsarmy\DbBladeCompiler;
22

3-
use View, Closure, ArrayAccess;
3+
use ArrayAccess;
44
use Illuminate\Contracts\Support\Arrayable;
55
use Illuminate\Contracts\Support\Renderable;
66
use Illuminate\Config\Repository;
77
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\View\View as BaseView;
9+
use Illuminate\Support\Facades\View;
10+
use Throwable;
811

9-
class DbView extends \Illuminate\View\View implements ArrayAccess, Renderable
12+
class DbView extends BaseView implements ArrayAccess, Renderable
1013
{
1114

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

72-
$response = isset($callback) ? $callback($this, $contents) : null;
75+
try {
76+
$contents = $this->renderContents();
7377

74-
// Once we have the contents of the view, we will flush the sections if we are
75-
// done rendering all views so that there is nothing left hanging over when
76-
// anothoer view is rendered in the future by the application developers.
77-
// Before flushing, check Laravel version for correct method use
78-
if (version_compare(app()->version(), '5.4.0') >= 0)
79-
View::flushStateIfDoneRendering();
80-
else
81-
View::flushSectionsIfDoneRendering();
78+
$response = isset($callback) ? $callback($this, $contents) : null;
8279

83-
return $response ?: $contents;
80+
// Once we have the contents of the view, we will flush the sections if we are
81+
// done rendering all views so that there is nothing left hanging over when
82+
// anothoer view is rendered in the future by the application developers.
83+
// Before flushing, check Laravel version for correct method use
84+
if ($usesState) {
85+
View::flushStateIfDoneRendering();
86+
} else {
87+
View::flushSectionsIfDoneRendering();
88+
}
89+
90+
return $response ?: $contents;
91+
} catch (Throwable $exception) {
92+
if ($usesState) {
93+
View::flushState();
94+
} else {
95+
View::flushSections();
96+
}
97+
98+
throw $exception;
99+
}
84100
}
85101

86102
/**

0 commit comments

Comments
 (0)