From 21f54c5562ad566247b60cdc967175ed563e2711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Wed, 2 Oct 2019 14:36:01 +0200 Subject: [PATCH] added ability to re-use existing error handling stack --- src/Console/Event/Configuring.php | 8 +------- src/Console/Server.php | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Console/Event/Configuring.php b/src/Console/Event/Configuring.php index 28cce8411..eadd04fff 100644 --- a/src/Console/Event/Configuring.php +++ b/src/Console/Event/Configuring.php @@ -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; } /** diff --git a/src/Console/Server.php b/src/Console/Server.php index 2d5000501..fd0b5a1f4 100644 --- a/src/Console/Server.php +++ b/src/Console/Server.php @@ -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); }