Skip to content

Commit

Permalink
Extend ViewFormatter instead of using a middleware. Support for beta 13
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkwinkelmann committed May 6, 2020
1 parent 7c05a43 commit 1615b90
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 93 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"source": "https://github.com/FriendsOfFlarum/html-errors"
},
"require": {
"flarum/core": ">=0.1.0-beta.11 <0.1.0-beta.13"
"flarum/core": ">=0.1.0-beta.12 <0.1.0-beta.14"
},
"replace": {
"flagrow/html-errors": "*"
Expand Down
8 changes: 5 additions & 3 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace FoF\HtmlErrors;

use Flarum\Extend;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Foundation\Application;

return [
(new Extend\Frontend('admin'))
->js(__DIR__ . '/js/dist/admin.js'),

new Extend\Locales(__DIR__ . '/locale'),
function (Dispatcher $events) {
$events->subscribe(Listeners\Middlewares::class);

function (Application $app) {
$app->register(Providers\ErrorServiceProvider::class);
},
];
25 changes: 25 additions & 0 deletions src/ErrorHandling/CustomViewFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace FoF\HtmlErrors\ErrorHandling;

use Flarum\Foundation\ErrorHandling\HandledError;
use Flarum\Foundation\ErrorHandling\ViewFormatter;
use Laminas\Diactoros\Response\HtmlResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class CustomViewFormatter extends ViewFormatter
{
public function format(HandledError $error, ServerRequestInterface $request): ResponseInterface
{
// Get the custom html for that error if it exists
// This supports more codes than what is exposed in the extension settings
$html = $this->settings->get('flagrow-html-errors.custom' . $error->getStatusCode() . 'ErrorHtml');

if ($html) {
return new HtmlResponse($html, $error->getStatusCode());
}

return parent::format($error, $request);
}
}
28 changes: 0 additions & 28 deletions src/Listeners/Middlewares.php

This file was deleted.

61 changes: 0 additions & 61 deletions src/Middlewares/HandleErrors.php

This file was deleted.

16 changes: 16 additions & 0 deletions src/Providers/ErrorServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace FoF\HtmlErrors\Providers;

use Flarum\Foundation\ErrorHandling\ViewFormatter;
use FoF\HtmlErrors\ErrorHandling\CustomViewFormatter;
use Illuminate\Support\ServiceProvider;

class ErrorServiceProvider extends ServiceProvider
{
public function register()
{
// Replace Flarum's error view formatter with our own
$this->app->bind(ViewFormatter::class, CustomViewFormatter::class);
}
}

0 comments on commit 1615b90

Please sign in to comment.