/
Server.php
68 lines (52 loc) · 1.79 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Console;
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Foundation\SiteInterface;
use Illuminate\Container\Container;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
class Server
{
private $site;
public function __construct(SiteInterface $site)
{
$this->site = $site;
}
public function listen()
{
$app = $this->site->bootApp();
$console = new Application('Flarum', \Flarum\Foundation\Application::VERSION);
foreach ($app->getConsoleCommands() as $command) {
$console->add($command);
}
$this->handleErrors($console);
exit($console->run());
}
private function handleErrors(Application $console)
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
$container = Container::getInstance();
/** @var Registry $registry */
$registry = $container->make(Registry::class);
$error = $registry->handle($event->getError());
/** @var Reporter[] $reporters */
$reporters = $container->tagged(Reporter::class);
if ($error->shouldBeReported()) {
foreach ($reporters as $reporter) {
$reporter->report($error->getException());
}
}
});
$console->setDispatcher($dispatcher);
}
}