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