|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Spatie\EventProjector\Projectionist; |
|
8
|
|
|
use Spatie\EventProjector\EventHandlers\EventHandler; |
|
9
|
|
|
|
|
10
|
|
|
final class ListCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
protected $signature = 'event-projector:list'; |
|
13
|
|
|
|
|
14
|
|
|
protected $description = 'Lists all event handlers'; |
|
15
|
|
|
|
|
16
|
|
|
public function handle(Projectionist $projectionist) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->info(''); |
|
19
|
|
|
$projectors = $projectionist->getProjectors(); |
|
20
|
|
|
$rows = $this->convertEventHandlersToTableRows($projectors); |
|
21
|
|
|
count($rows) |
|
22
|
|
|
? $this->table(['Event', 'Handled by projectors'], $rows) |
|
23
|
|
|
: $this->warn('No projectors registered'); |
|
24
|
|
|
|
|
25
|
|
|
$this->info(''); |
|
26
|
|
|
$projectors = $projectionist->getReactors(); |
|
27
|
|
|
$rows = $this->convertEventHandlersToTableRows($projectors); |
|
28
|
|
|
count($rows) |
|
29
|
|
|
? $this->table(['Event', 'Handled by reactors'], $rows) |
|
30
|
|
|
: $this->warn('No reactors registered'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function convertEventHandlersToTableRows(Collection $eventHandlers): array |
|
34
|
|
|
{ |
|
35
|
|
|
$events = $eventHandlers |
|
36
|
|
|
->reduce(function ($events, EventHandler $eventHandler) { |
|
37
|
|
|
$eventHandler->getEventHandlingMethods()->each(function (string $method, string $eventClass) use (&$events, $eventHandler) { |
|
38
|
|
|
$events[$eventClass][] = get_class($eventHandler); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
return $events; |
|
42
|
|
|
}, []); |
|
43
|
|
|
|
|
44
|
|
|
return collect($events)->map(function (array $eventHandlers, string $eventClass) { |
|
45
|
|
|
return [$eventClass, implode(PHP_EOL, collect($eventHandlers)->sort()->toArray())]; |
|
46
|
|
|
}) |
|
47
|
|
|
->sort() |
|
48
|
|
|
->values() |
|
49
|
|
|
->toArray(); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|