Skip to content

Commit

Permalink
extract trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Dec 11, 2023
1 parent fb101ff commit 104104f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 79 deletions.
47 changes: 47 additions & 0 deletions src/Exceptions/Concerns/RendersHttpExceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Statamic\Exceptions\Concerns;

use Statamic\Facades\Cascade;
use Statamic\Statamic;
use Statamic\View\View;

trait RendersHttpExceptions
{
public function render()
{
if (Statamic::isCpRoute()) {
return response()->view('statamic::errors.'.$this->getStatusCode(), [], $this->getStatusCode());
}

if (view()->exists('errors.'.$this->getStatusCode())) {
return response($this->contents(), $this->getStatusCode());
}
}

protected function contents()
{
Cascade::hydrated(function ($cascade) {
$cascade->set('response_code', $this->getStatusCode());
});

return app(View::class)
->template('errors.'.$this->getStatusCode())
->layout($this->layout())
->render();
}

protected function layout()
{
$layouts = collect([
'errors.layout',
'layouts.layout',
'layout',
'statamic::blank',
]);

return $layouts->filter()->first(function ($layout) {
return view()->exists($layout);
});
}
}
43 changes: 3 additions & 40 deletions src/Exceptions/ForbiddenHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,15 @@

namespace Statamic\Exceptions;

use Statamic\Facades\Cascade;
use Statamic\Statamic;
use Statamic\View\View;
use Statamic\Exceptions\Concerns\RendersHttpExceptions;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ForbiddenHttpException extends HttpException
{
use RendersHttpExceptions;

public function __construct(string $message = '', \Throwable $previous = null, int $code = 0, array $headers = [])
{
parent::__construct(403, $message, $previous, $headers, $code);
}

public function render()
{
if (Statamic::isCpRoute()) {
return response()->view('statamic::errors.403', [], 403);
}

if (view()->exists('errors.403')) {
return response($this->contents(), 403);
}
}

protected function contents()
{
Cascade::hydrated(function ($cascade) {
$cascade->set('response_code', 403);
});

return app(View::class)
->template('errors.403')
->layout($this->layout())
->render();
}

protected function layout()
{
$layouts = collect([
'errors.layout',
'layouts.layout',
'layout',
'statamic::blank',
]);

return $layouts->filter()->first(function ($layout) {
return view()->exists($layout);
});
}
}
41 changes: 2 additions & 39 deletions src/Exceptions/NotFoundHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,10 @@

namespace Statamic\Exceptions;

use Statamic\Facades\Cascade;
use Statamic\Statamic;
use Statamic\View\View;
use Statamic\Exceptions\Concerns\RendersHttpExceptions;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException as SymfonyException;

class NotFoundHttpException extends SymfonyException
{
public function render()
{
if (Statamic::isCpRoute()) {
return response()->view('statamic::errors.404', [], 404);
}

if (view()->exists('errors.404')) {
return response($this->contents(), 404);
}
}

protected function contents()
{
Cascade::hydrated(function ($cascade) {
$cascade->set('response_code', 404);
});

return app(View::class)
->template('errors.404')
->layout($this->layout())
->render();
}

protected function layout()
{
$layouts = collect([
'errors.layout',
'layouts.layout',
'layout',
'statamic::blank',
]);

return $layouts->filter()->first(function ($layout) {
return view()->exists($layout);
});
}
use RendersHttpExceptions;
}

0 comments on commit 104104f

Please sign in to comment.