1
0
mirror of https://github.com/flarum/core.git synced 2025-05-06 15:35:38 +02:00

added ability to re-use existing error handling stack

This commit is contained in:
Daniël Klabbers 2019-10-02 14:36:01 +02:00 committed by Daniël Klabbers
parent a0ace316e8
commit 21f54c5562
2 changed files with 29 additions and 8 deletions

View File

@ -32,21 +32,15 @@ class Configuring
* @var ConsoleApplication
*/
public $console;
/**
* @var EventDispatcher
*/
public $eventDispatcher;
/**
* @param Application $app
* @param ConsoleApplication $console
* @param EventDispatcher $eventDispatcher
*/
public function __construct(Application $app, ConsoleApplication $console, EventDispatcher $eventDispatcher)
public function __construct(Application $app, ConsoleApplication $console)
{
$this->app = $app;
$this->console = $console;
$this->eventDispatcher = $eventDispatcher;
}
/**

View File

@ -13,9 +13,13 @@ namespace Flarum\Console;
use Flarum\Console\Event\Configuring;
use Flarum\Foundation\Application;
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Foundation\SiteInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
class Server
@ -46,8 +50,31 @@ class Server
{
$app = Application::getInstance();
$this->handleErrors($app, $console);
$events = $app->make(Dispatcher::class);
$events->fire(new Configuring($app, $console, $dispatcher = new EventDispatcher()));
$events->fire(new Configuring($app, $console));
}
private function handleErrors(Application $app, ConsoleApplication $console)
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) use ($app) {
/** @var Registry $registry */
$registry = $app->make(Registry::class);
$error = $registry->handle($event->getError());
$reporters = $app->tagged(Reporter::class);
if ($error->shouldBeReported()) {
foreach ($reporters as $reporter) {
$reporter->report($error->getException());
}
}
});
$console->setDispatcher($dispatcher);
}