|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine\Aent\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Safe\Exceptions\StringsException; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use TheAentMachine\Aenthill\Aenthill; |
|
11
|
|
|
use TheAentMachine\Prompt\Prompt; |
|
12
|
|
|
use function Safe\sprintf; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractEvent extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var InputInterface */ |
|
17
|
|
|
protected $input; |
|
18
|
|
|
|
|
19
|
|
|
/** @var OutputInterface */ |
|
20
|
|
|
protected $output; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Prompt */ |
|
23
|
|
|
protected $prompt; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract protected function getEventName(): string; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract protected function shouldRegisterEvents(): bool; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* return @void |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract protected function beforeExecute(): void; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param null|string $payload |
|
42
|
|
|
* @return null|string |
|
43
|
|
|
*/ |
|
44
|
|
|
abstract protected function executeEvent(?string $payload): ?string; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* return @void |
|
48
|
|
|
*/ |
|
49
|
|
|
abstract protected function afterExecute(): void; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return void |
|
53
|
|
|
* @throws StringsException |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function configure() |
|
56
|
|
|
{ |
|
57
|
|
|
$this |
|
58
|
|
|
->setName($this->getEventName()) |
|
59
|
|
|
->setDescription(sprintf('Handles the <info>%s</info> event', $this->getEventName())) |
|
60
|
|
|
->addArgument('payload', InputArgument::OPTIONAL, 'The event payload'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param InputInterface $input |
|
65
|
|
|
* @param OutputInterface $output |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->shouldRegisterEvents()) { |
|
71
|
|
|
$this->registerEvents(); |
|
72
|
|
|
} |
|
73
|
|
|
$this->input = $input; |
|
74
|
|
|
$this->output = $output; |
|
75
|
|
|
$this->prompt = new Prompt($this->input, $this->output, $this->getHelper('question'), $this->getHelper('formatter')); |
|
76
|
|
|
$this->beforeExecute(); |
|
77
|
|
|
$result = $this->executeEvent($input->getArgument('payload')); |
|
|
|
|
|
|
78
|
|
|
$this->afterExecute(); |
|
79
|
|
|
if ($result !== null) { |
|
80
|
|
|
Aenthill::reply('REPLY', $result); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function registerEvents(): void |
|
88
|
|
|
{ |
|
89
|
|
|
$events = \array_map(function (AbstractEvent $event) { |
|
90
|
|
|
return $event->getEventName(); |
|
91
|
|
|
}, \array_filter($this->getApplication()->all(), function (Command $command) { |
|
92
|
|
|
return $command instanceof AbstractEvent && !$command->isHidden(); |
|
93
|
|
|
})); |
|
94
|
|
|
Aenthill::update(null, $events); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getAentName(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->getApplication()->getName(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|