1
|
|
|
<?php declare (strict_types = 1); |
2
|
|
|
namespace SmoothPhp\LaravelAdapter\Console; |
3
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use SmoothPhp\Contracts\Domain\DomainMessage; |
9
|
|
|
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher; |
10
|
|
|
use SmoothPhp\Contracts\EventDispatcher\Subscriber; |
11
|
|
|
use SmoothPhp\Contracts\EventStore\EventStore; |
12
|
|
|
use SmoothPhp\Contracts\Projections\ProjectionServiceProvider; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class RunProjectionCommand |
16
|
|
|
* @package SmoothPhp\LaravelAdapter\Console |
17
|
|
|
* @author Simon Bennett <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class RunProjectionCommand extends Command |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The name and signature of the console command. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $signature = 'smoothphp:project {projections}'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Run Projections'; |
34
|
|
|
|
35
|
|
|
/** @var Repository */ |
36
|
|
|
private $config; |
37
|
|
|
|
38
|
|
|
/** @var EventDispatcher */ |
39
|
|
|
private $eventDispatcher; |
40
|
|
|
|
41
|
|
|
/** @var EventStore */ |
42
|
|
|
private $eventStore; |
43
|
|
|
/** @var Application */ |
44
|
|
|
private $application; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* RunProjectionCommand constructor. |
48
|
|
|
* @param Repository $config |
49
|
|
|
* @param EventDispatcher $eventDispatcher |
50
|
|
|
* @param EventStore $eventStore |
51
|
|
|
* @param Application $application |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
Repository $config, |
55
|
|
|
EventDispatcher $eventDispatcher, |
56
|
|
|
EventStore $eventStore, |
57
|
|
|
Application $application |
58
|
|
|
) { |
59
|
|
|
parent::__construct(); |
60
|
|
|
$this->config = $config; |
61
|
|
|
$this->eventDispatcher = $eventDispatcher; |
62
|
|
|
$this->eventStore = $eventStore; |
63
|
|
|
$this->application = $application; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Execute the console command. |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function handle() |
72
|
|
|
{ |
73
|
|
|
$projectionRequest = collect(explode(',', $this->argument('projections'))); |
74
|
|
|
|
75
|
|
|
$projectionsServiceProviders = $projectionRequest->each( |
76
|
|
|
function ($projectionName) { |
77
|
|
|
if (!isset($this->config->get('cqrses.projections_service_providers')[$projectionName])) { |
78
|
|
|
$this->error("{$projectionName} Does not exist, check cqrses config"); |
79
|
|
|
|
80
|
|
|
exit(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
)->map( |
84
|
|
|
function ($projectionName) { |
85
|
|
|
return $this->application->make( |
86
|
|
|
$this->config->get('cqrses.projections_service_providers')[$projectionName] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
)->each( |
90
|
|
|
function (ProjectionServiceProvider $projectionClass) { |
91
|
|
|
$this->downMigration($projectionClass); |
92
|
|
|
} |
93
|
|
|
)->each( |
94
|
|
|
function (ProjectionServiceProvider $projectionClass) { |
95
|
|
|
$this->upMigration($projectionClass); |
96
|
|
|
} |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
/** @var Collection|Subscriber[] $projections */ |
100
|
|
|
$projections = $projectionsServiceProviders->map( |
101
|
|
|
function (ProjectionServiceProvider $projectServiceProvider) { |
102
|
|
|
return collect($projectServiceProvider->getProjections())->map( |
103
|
|
|
function ($projection) { |
104
|
|
|
return $this->application->make($projection); |
105
|
|
|
} |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
)->collapse(); |
109
|
|
|
|
110
|
|
|
$events = $projections->map( |
111
|
|
|
function (Subscriber $subscriber) { |
112
|
|
|
return array_keys($subscriber->getSubscribedEvents()); |
113
|
|
|
} |
114
|
|
|
)->collapse()->map( |
115
|
|
|
function ($eventClassName) { |
116
|
|
|
return str_replace('\\', '.', $eventClassName); |
117
|
|
|
} |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->replayEvents($projections, $events); |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ProjectionServiceProvider $projectionServiceProvider |
126
|
|
|
*/ |
127
|
|
|
public function downMigration(ProjectionServiceProvider $projectionServiceProvider) |
128
|
|
|
{ |
129
|
|
|
$this->line($projectionServiceProvider->down()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param ProjectionServiceProvider $projectionServiceProvider |
134
|
|
|
*/ |
135
|
|
|
public function upMigration(ProjectionServiceProvider $projectionServiceProvider) |
136
|
|
|
{ |
137
|
|
|
$this->line($projectionServiceProvider->up()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param Collection|Subscriber[] |
142
|
|
|
* @param string[] $events |
143
|
|
|
*/ |
144
|
|
|
protected function replayEvents($projections, $events) |
145
|
|
|
{ |
146
|
|
|
$eventCount = $this->eventStore->getEventCountByTypes($events); |
147
|
|
|
$start = 0; |
148
|
|
|
$take = 1000; |
149
|
|
|
|
150
|
|
|
$this->output->progressStart($eventCount); |
151
|
|
|
$dispatcher = $this->buildAndRegisterDispatcher($projections); |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
while ($start < $eventCount) { |
155
|
|
|
foreach ($this->eventStore->getEventsByType($events, $start, $take) as $eventRow) { |
156
|
|
|
$this->dispatchEvent($dispatcher, $eventRow); |
157
|
|
|
} |
158
|
|
|
$start += $take; |
159
|
|
|
$this->output->progressAdvance($take > $eventCount ? $eventCount : $take); |
160
|
|
|
} |
161
|
|
|
$this->output->progressFinish(); |
162
|
|
|
$this->line((memory_get_peak_usage(true) / 1024 / 1024) . "mb Peak Usage", false); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param EventDispatcher $eventDispatcher |
167
|
|
|
* @param $eventRow |
168
|
|
|
*/ |
169
|
|
|
protected function dispatchEvent(EventDispatcher $eventDispatcher, DomainMessage $eventRow) |
170
|
|
|
{ |
171
|
|
|
$eventDispatcher->dispatch( |
172
|
|
|
$eventRow->getType(), |
173
|
|
|
[ |
174
|
|
|
$eventRow->getPayload() |
175
|
|
|
], |
176
|
|
|
true |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param Collection $projections |
182
|
|
|
* @return EventDispatcher |
183
|
|
|
*/ |
184
|
|
|
protected function buildAndRegisterDispatcher($projections) |
185
|
|
|
{ |
186
|
|
|
/** @var EventDispatcher $dispatcher */ |
187
|
|
|
$dispatcher = $this->application->make($this->config->get('cqrses.event_dispatcher')); |
188
|
|
|
|
189
|
|
|
$projections->each( |
190
|
|
|
function ($projection) use ($dispatcher) { |
191
|
|
|
$dispatcher->addSubscriber($projection); |
192
|
|
|
} |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
return $dispatcher; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|