Skip to content

Commit 550f11b

Browse files
author
Thierry Parent
committed
Restore view factory state on caught exception
1 parent 5336b97 commit 550f11b

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

Diff for: src/Flynsarmy/DbBladeCompiler/DbView.php

+28-15
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,30 @@ public function field($content_field)
6770
*/
6871
public function render(callable $callback = null)
6972
{
70-
$contents = $this->renderContents();
71-
72-
$response = isset($callback) ? $callback($this, $contents) : null;
73-
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();
73+
try {
74+
$contents = $this->renderContents();
75+
76+
$response = isset($callback) ? $callback($this, $contents) : null;
77+
78+
// Once we have the contents of the view, we will flush the sections if we are
79+
// done rendering all views so that there is nothing left hanging over when
80+
// anothoer view is rendered in the future by the application developers.
81+
// Before flushing, check Laravel version for correct method use
82+
if (version_compare(app()->version(), '5.4.0') >= 0)
83+
View::flushStateIfDoneRendering();
84+
else
85+
View::flushSectionsIfDoneRendering();
86+
87+
return $response ?: $contents;
88+
} catch (Throwable $exception) {
89+
if (version_compare(app()->version(), '5.4.0') >= 0) {
90+
View::flushState();
91+
} else {
92+
View::flushSections();
93+
}
8294

83-
return $response ?: $contents;
95+
throw $exception;
96+
}
8497
}
8598

8699
/**

0 commit comments

Comments
 (0)