|
1
|
|
|
<?php |
|
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\Database\DatabaseManager; |
|
8
|
|
|
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher; |
|
9
|
|
|
use SmoothPhp\Contracts\Serialization\Serializer; |
|
10
|
|
|
use SmoothPhp\Serialization\Exception\SerializedClassDoesNotExist; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class RebuildProjectionsCommand |
|
14
|
|
|
* @package SmoothPhp\LaravelAdapter\Console |
|
15
|
|
|
* @author Simon Bennett <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
final class RebuildProjectionsCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The name and signature of the console command. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $signature = 'smoothphp:rebuild'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The console command description. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $description = 'Rebuild all projection'; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Repository */ |
|
34
|
|
|
private $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var EventDispatcher |
|
38
|
|
|
*/ |
|
39
|
|
|
private $dispatcher; |
|
40
|
|
|
|
|
41
|
|
|
/** @var DatabaseManager */ |
|
42
|
|
|
private $databaseManager; |
|
43
|
|
|
|
|
44
|
|
|
/** @var Serializer */ |
|
45
|
|
|
private $serializer; |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* BuildLaravelEventStore constructor. |
|
50
|
|
|
* @param Repository $config |
|
51
|
|
|
* @param Application $application |
|
52
|
|
|
* @param DatabaseManager $databaseManager |
|
53
|
|
|
* @param Serializer $serializer |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
Repository $config, |
|
57
|
|
|
Application $application, |
|
58
|
|
|
DatabaseManager $databaseManager, |
|
59
|
|
|
Serializer $serializer |
|
60
|
|
|
) { |
|
61
|
|
|
parent::__construct(); |
|
62
|
|
|
$this->config = $config; |
|
63
|
|
|
|
|
64
|
|
|
$this->dispatcher = $application->make(EventDispatcher::class); |
|
65
|
|
|
$this->databaseManager = $databaseManager; |
|
66
|
|
|
$this->serializer = $serializer; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Execute the console command. |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function handle() |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($this->config->get('cqrses.pre_rebuild_commands') as $preRebuildCommand) { |
|
77
|
|
|
if (is_array($preRebuildCommand)) { |
|
78
|
|
|
$this->call(key($preRebuildCommand), current($preRebuildCommand)); |
|
79
|
|
|
} else { |
|
80
|
|
|
$this->call($preRebuildCommand); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->replayEvents(); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($this->config->get('cqrses.post_rebuild_commands') as $postRebuildCommand) { |
|
87
|
|
|
if (is_array($postRebuildCommand)) { |
|
88
|
|
|
$this->call(key($postRebuildCommand), current($postRebuildCommand)); |
|
89
|
|
|
} else { |
|
90
|
|
|
$this->call($postRebuildCommand); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function replayEvents() |
|
99
|
|
|
{ |
|
100
|
|
|
$allEvents = $this->getAllEvents(); |
|
101
|
|
|
$this->output->progressStart(count($allEvents)); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($allEvents as $eventRow) { |
|
|
|
|
|
|
104
|
|
|
$payload = json_decode($eventRow->payload, true)['payload']; |
|
105
|
|
|
$this->dispatcher->dispatch($eventRow->type, |
|
106
|
|
|
[ |
|
107
|
|
|
(call_user_func( |
|
108
|
|
|
[ |
|
109
|
|
|
str_replace('.', '\\', $eventRow->type), |
|
110
|
|
|
'deserialize' |
|
111
|
|
|
], |
|
112
|
|
|
$payload |
|
113
|
|
|
)) |
|
114
|
|
|
], |
|
115
|
|
|
true); |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
$this->output->progressFinish(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return \stdClass |
|
126
|
|
|
*/ |
|
127
|
|
|
private function getAllEvents() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->databaseManager |
|
130
|
|
|
->connection($this->config->get('cqrses.eventstore_connection')) |
|
131
|
|
|
->table($this->config->get('cqrses.eventstore_table')) |
|
132
|
|
|
->get(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|