1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventSourcing\Console; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Spatie\EventSourcing\Projectionist; |
9
|
|
|
use Spatie\EventSourcing\StoredEventRepository; |
10
|
|
|
|
11
|
|
|
class ReplayCommand extends Command |
12
|
|
|
{ |
13
|
|
|
protected $signature = 'event-sourcing:replay {projector?*} |
14
|
|
|
{--from=0 : Replay events starting from this event number} |
15
|
|
|
{--stored-event-model= : Replay events from this store}'; |
16
|
|
|
|
17
|
|
|
protected $description = 'Replay stored events'; |
18
|
|
|
|
19
|
|
|
protected ?Projectionist $projectionist; |
|
|
|
|
20
|
|
|
|
21
|
|
|
public function handle(Projectionist $projectionist): void |
22
|
|
|
{ |
23
|
|
|
$this->projectionist = $projectionist; |
24
|
|
|
|
25
|
|
|
$projectors = $this->selectProjectors($this->argument('projector')); |
26
|
|
|
|
27
|
|
|
if (is_null($projectors)) { |
28
|
|
|
$this->warn('No events replayed!'); |
29
|
|
|
|
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->replay($projectors, $this->option('from')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function selectProjectors(array $projectorClassNames): ?Collection |
37
|
|
|
{ |
38
|
|
|
if (count($projectorClassNames ?? []) === 0) { |
39
|
|
|
if (! $confirmed = $this->confirm('Are you sure you want to replay events to all projectors?', true)) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->projectionist->getProjectors(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return collect($projectorClassNames) |
47
|
|
|
->map(fn (string $projectorName) => ltrim($projectorName, '\\')) |
48
|
|
|
->map(function (string $projectorName) { |
49
|
|
|
if (! $projector = $this->projectionist->getProjector($projectorName)) { |
50
|
|
|
throw new Exception("Projector {$projectorName} not found. Did you register it?"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $projector; |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function replay(Collection $projectors, int $startingFrom): void |
58
|
|
|
{ |
59
|
|
|
$repository = app(StoredEventRepository::class); |
60
|
|
|
$replayCount = $repository->countAllStartingFrom($startingFrom); |
61
|
|
|
|
62
|
|
|
if ($replayCount === 0) { |
63
|
|
|
$this->warn('There are no events to replay'); |
64
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->comment("Replaying {$replayCount} events..."); |
69
|
|
|
|
70
|
|
|
$bar = $this->output->createProgressBar($replayCount); |
71
|
|
|
$onEventReplayed = function () use ($bar) { |
72
|
|
|
$bar->advance(); |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
$this->projectionist->replay($projectors, $startingFrom, $onEventReplayed); |
76
|
|
|
|
77
|
|
|
$bar->finish(); |
78
|
|
|
|
79
|
|
|
$this->emptyLine(2); |
80
|
|
|
$this->comment('All done!'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function emptyLine(int $amount = 1): void |
84
|
|
|
{ |
85
|
|
|
foreach (range(1, $amount) as $i) { |
86
|
|
|
$this->line(''); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|