-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatchbot-cli.php
More file actions
executable file
·69 lines (58 loc) · 2.17 KB
/
Copy pathmatchbot-cli.php
File metadata and controls
executable file
·69 lines (58 loc) · 2.17 KB
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
69
#!/usr/bin/env php
<?php
declare(strict_types=1);
use MatchBot\Application\Commands\Command;
use MatchBot\Application\Commands\LockingCommand;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Lock\LockFactory;
$psr11App = require __DIR__ . '/bootstrap.php';
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleEvent $event) use ($psr11App) {
$logger = $psr11App->get(Logger::class);
$input = $event->getInput();
if ($input->getOption(Command::CLI_OPTION_NOLOG)) {
array_filter(
$logger->getHandlers(),
(static fn ($handler) => $handler instanceof StreamHandler)
)[0]->setLevel(LogLevel::WARNING);
}
});
$cliApp = new Application();
$cliApp->setDispatcher($dispatcher);
$cliApp->getDefinition()->addOption(
new InputOption(
Command::CLI_OPTION_NOLOG,
null,
InputOption::VALUE_NONE,
'Suppresses debug & info log, show only warnings and errors'
)
);
$commands = Command::allCommands($psr11App);
foreach ($commands as $command) {
if ($command instanceof LockingCommand) { // i.e. not Symfony Messenger's built-in consumer.
$command->setLockFactory($psr11App->get(LockFactory::class));
$command->setLogger($psr11App->get(LoggerInterface::class));
}
$cliApp->addCommand($command);
}
try {
// We don't want Symfony to catch any throwable because we want to catch it ourselves and log it
// instead (which should also mean it gets sent to Slack
$cliApp->setCatchExceptions(false);
$cliApp->setCatchErrors(false);
$cliApp->run();
} catch (Throwable $t) {
$logger = $psr11App->get(LoggerInterface::class);
$logger->error("CLI Error: " . $t->__toString());
$cliApp->renderThrowable($t, (new ConsoleOutput())->getErrorOutput());
exit(1);
}