|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheAentMachine; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
abstract class EventCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var LoggerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $log; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var InputInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $input; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var OutputInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $output; |
|
27
|
|
|
|
|
28
|
|
|
abstract protected function getEventName(): string; |
|
29
|
|
|
abstract protected function executeEvent(?string $payload): void; |
|
30
|
|
|
|
|
31
|
|
|
protected function configure() |
|
32
|
|
|
{ |
|
33
|
|
|
$this |
|
34
|
|
|
->setName($this->getEventName()) |
|
35
|
|
|
->setDescription('Handle the "' . $this->getEventName() . '" event') |
|
36
|
|
|
->addArgument('payload', InputArgument::OPTIONAL, 'The event payload'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
40
|
|
|
{ |
|
41
|
|
|
// Let's send the list of caught events to Hercule |
|
42
|
|
|
Hercule::setHandledEvents($this->getAllEventNames()); |
|
43
|
|
|
|
|
44
|
|
|
$logLevelConfigurator = new LogLevelConfigurator($output); |
|
45
|
|
|
$logLevelConfigurator->configureLogLevel(); |
|
46
|
|
|
|
|
47
|
|
|
$this->log = new ConsoleLogger($output); |
|
48
|
|
|
|
|
49
|
|
|
$payload = $input->getArgument('payload'); |
|
50
|
|
|
$this->input = $input; |
|
51
|
|
|
$this->output = $output; |
|
52
|
|
|
|
|
53
|
|
|
$this->executeEvent($payload); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string[] |
|
58
|
|
|
*/ |
|
59
|
|
|
private function getAllEventNames(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return array_map(function (EventCommand $event) { |
|
62
|
|
|
return $event->getEventName(); |
|
63
|
|
|
}, \array_filter($this->getApplication()->all(), function (Command $command) { |
|
64
|
|
|
return $command instanceof EventCommand; |
|
65
|
|
|
})); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|