1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheAentMachine\Command; |
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
|
|
|
use TheAentMachine\Aenthill\Aenthill; |
13
|
|
|
use TheAentMachine\Aenthill\CommonEvents; |
14
|
|
|
use TheAentMachine\Exception\LogLevelException; |
15
|
|
|
use TheAentMachine\Helper\AentHelper; |
16
|
|
|
use TheAentMachine\Helper\LogLevelConfigurator; |
17
|
|
|
|
18
|
|
|
abstract class AbstractEventCommand extends Command |
19
|
|
|
{ |
20
|
|
|
/** @var LoggerInterface */ |
21
|
|
|
protected $log; |
22
|
|
|
|
23
|
|
|
/** @var InputInterface */ |
24
|
|
|
protected $input; |
25
|
|
|
|
26
|
|
|
/** @var OutputInterface */ |
27
|
|
|
protected $output; |
28
|
|
|
|
29
|
|
|
/** @var AentHelper */ |
30
|
|
|
private $aentHelper; |
31
|
|
|
|
32
|
|
|
abstract protected function getEventName(): string; |
33
|
|
|
|
34
|
|
|
abstract protected function executeEvent(?string $payload): ?string; |
35
|
|
|
|
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setName($this->getEventName()) |
40
|
|
|
->setDescription('Handle the "' . $this->getEventName() . '" event') |
41
|
|
|
->addArgument('payload', InputArgument::OPTIONAL, 'The event payload'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param InputInterface $input |
46
|
|
|
* @param OutputInterface $output |
47
|
|
|
* @throws LogLevelException |
48
|
|
|
*/ |
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
50
|
|
|
{ |
51
|
|
|
$this->aentHelper = new AentHelper($input, $output, $this->getHelper('question'), $this->getHelper('formatter')); |
52
|
|
|
$logLevelConfigurator = new LogLevelConfigurator($output); |
53
|
|
|
$logLevelConfigurator->configureLogLevel(); |
54
|
|
|
|
55
|
|
|
$this->log = new ConsoleLogger($output); |
56
|
|
|
|
57
|
|
|
$payload = $input->getArgument('payload'); |
58
|
|
|
$this->input = $input; |
59
|
|
|
$this->output = $output; |
60
|
|
|
|
61
|
|
|
$result = $this->executeEvent($payload); |
62
|
|
|
|
63
|
|
|
// Now, let's send a "reply" event |
64
|
|
|
if ($result !== null) { |
65
|
|
|
Aenthill::reply(CommonEvents::REPLY_EVENT, $result); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string[] |
71
|
|
|
*/ |
72
|
|
|
public function getAllEventNames(): array |
73
|
|
|
{ |
74
|
|
|
return array_map(function (AbstractEventCommand $event) { |
75
|
|
|
return $event->getEventName(); |
76
|
|
|
}, \array_filter($this->getApplication()->all(), function (Command $command) { |
77
|
|
|
return $command instanceof AbstractEventCommand && !$command->isHidden(); |
78
|
|
|
})); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getAentHelper(): AentHelper |
82
|
|
|
{ |
83
|
|
|
if ($this->aentHelper === null) { |
84
|
|
|
throw new \BadMethodCallException('Function getAentHelper can only be called inside "execute(xxx)" functions.'); |
85
|
|
|
} |
86
|
|
|
return $this->aentHelper; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|