1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Event Sourcing implementation module |
5
|
|
|
* |
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace ServiceBus\EventSourcingModule; |
14
|
|
|
|
15
|
|
|
use ServiceBus\Common\Module\ServiceBusModule; |
16
|
|
|
use ServiceBus\EventSourcing\EventStream\EventStreamRepository; |
17
|
|
|
use ServiceBus\EventSourcing\EventStream\Store\EventStreamStore; |
18
|
|
|
use ServiceBus\EventSourcing\EventStream\Store\SqlEventStreamStore; |
19
|
|
|
use ServiceBus\EventSourcing\Indexes\Store\IndexStore; |
20
|
|
|
use ServiceBus\EventSourcing\Indexes\Store\SqlIndexStore; |
21
|
|
|
use ServiceBus\EventSourcing\Snapshots\Snapshotter; |
22
|
|
|
use ServiceBus\EventSourcing\Snapshots\Store\SnapshotStore; |
23
|
|
|
use ServiceBus\EventSourcing\Snapshots\Store\SqlSnapshotStore; |
24
|
|
|
use ServiceBus\EventSourcing\Snapshots\Triggers\SnapshotTrigger; |
25
|
|
|
use ServiceBus\EventSourcing\Snapshots\Triggers\SnapshotVersionTrigger; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
27
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
28
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @todo: custom store initialization |
32
|
|
|
*/ |
33
|
|
|
final class EventSourcingModule implements ServiceBusModule |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $eventStoreServiceId; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $snapshotStoreServiceId; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $indexerStore; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string|null |
52
|
|
|
*/ |
53
|
|
|
private $databaseAdapterServiceId; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
private $customEventSerializerServiceId; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string|null |
62
|
|
|
*/ |
63
|
|
|
private $customSnapshotStrategyServiceId; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $databaseAdapterServiceId |
67
|
|
|
* |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
1 |
|
public static function withSqlStorage(string $databaseAdapterServiceId): self |
71
|
|
|
{ |
72
|
1 |
|
$self = new self( |
73
|
1 |
|
EventStreamStore::class, |
74
|
1 |
|
SnapshotStore::class, |
75
|
1 |
|
IndexStore::class |
76
|
|
|
); |
77
|
|
|
|
78
|
1 |
|
$self->databaseAdapterServiceId = $databaseAdapterServiceId; |
79
|
|
|
|
80
|
1 |
|
return $self; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $eventSerializerServiceId |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function withCustomEventSerializer(string $eventSerializerServiceId): self |
89
|
|
|
{ |
90
|
|
|
$this->customEventSerializerServiceId = $eventSerializerServiceId; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $snapshotStrategyServiceId |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function withCustomSnapshotStrategy(string $snapshotStrategyServiceId): self |
101
|
|
|
{ |
102
|
|
|
$this->customSnapshotStrategyServiceId = $snapshotStrategyServiceId; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
1 |
|
public function boot(ContainerBuilder $containerBuilder): void |
111
|
|
|
{ |
112
|
|
|
/** Default configuration used */ |
113
|
1 |
|
if(null !== $this->databaseAdapterServiceId) |
114
|
|
|
{ |
115
|
1 |
|
$storeArguments = [new Reference($this->databaseAdapterServiceId)]; |
116
|
|
|
|
117
|
1 |
|
$containerBuilder->addDefinitions([ |
118
|
1 |
|
$this->eventStoreServiceId => (new Definition(SqlEventStreamStore::class))->setArguments($storeArguments), |
119
|
1 |
|
$this->snapshotStoreServiceId => (new Definition(SqlSnapshotStore::class))->setArguments($storeArguments), |
120
|
1 |
|
$this->indexerStore => (new Definition(SqlIndexStore::class))->setArguments($storeArguments) |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
1 |
|
$this->registerSnapshotter($containerBuilder); |
126
|
1 |
|
$this->registerEventSourcingProvider($containerBuilder); |
127
|
1 |
|
$this->registerIndexer($containerBuilder); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param ContainerBuilder $containerBuilder |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
1 |
|
private function registerIndexer(ContainerBuilder $containerBuilder): void |
136
|
|
|
{ |
137
|
1 |
|
$containerBuilder->addDefinitions([ |
138
|
1 |
|
$this->indexerStore => (new Definition(SqlIndexStore::class))->setArguments([new Reference($this->databaseAdapterServiceId)]), |
|
|
|
|
139
|
1 |
|
IndexProvider::class => (new Definition(IndexProvider::class))->setArguments([new Reference($this->indexerStore)]) |
140
|
|
|
]); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param ContainerBuilder $containerBuilder |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
1 |
|
private function registerEventSourcingProvider(ContainerBuilder $containerBuilder): void |
149
|
|
|
{ |
150
|
|
|
$arguments = [ |
151
|
1 |
|
new Reference($this->eventStoreServiceId), |
152
|
1 |
|
new Reference(Snapshotter::class), |
153
|
1 |
|
null !== $this->customEventSerializerServiceId |
154
|
|
|
? new Reference($this->customEventSerializerServiceId) |
155
|
|
|
: null, |
156
|
1 |
|
new Reference('service_bus.logger') |
157
|
|
|
]; |
158
|
|
|
|
159
|
1 |
|
$containerBuilder->addDefinitions([ |
160
|
1 |
|
EventStreamRepository::class => (new Definition(EventStreamRepository::class))->setArguments($arguments), |
161
|
1 |
|
EventSourcingProvider::class => (new Definition(EventSourcingProvider::class))->setArguments([new Reference(EventStreamRepository::class)]) |
162
|
|
|
]); |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param ContainerBuilder $containerBuilder |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
1 |
|
private function registerSnapshotter(ContainerBuilder $containerBuilder): void |
171
|
|
|
{ |
172
|
1 |
|
if(null === $this->customSnapshotStrategyServiceId) |
173
|
|
|
{ |
174
|
1 |
|
$containerBuilder->addDefinitions([ |
175
|
1 |
|
SnapshotTrigger::class => new Definition(SnapshotVersionTrigger::class) |
176
|
|
|
]); |
177
|
|
|
|
178
|
1 |
|
$this->customSnapshotStrategyServiceId = SnapshotTrigger::class; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$arguments = [ |
182
|
1 |
|
new Reference($this->snapshotStoreServiceId), |
183
|
1 |
|
new Reference($this->customSnapshotStrategyServiceId), |
184
|
1 |
|
new Reference('service_bus.logger') |
185
|
|
|
]; |
186
|
|
|
|
187
|
1 |
|
$containerBuilder->addDefinitions([ |
188
|
1 |
|
Snapshotter::class => (new Definition(Snapshotter::class))->setArguments($arguments) |
189
|
|
|
]); |
190
|
1 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $eventStoreServiceId |
194
|
|
|
* @param string $snapshotStoreServiceId |
195
|
|
|
* @param string $indexerStore |
196
|
|
|
*/ |
197
|
1 |
|
private function __construct(string $eventStoreServiceId, string $snapshotStoreServiceId, string $indexerStore) |
198
|
|
|
{ |
199
|
1 |
|
$this->eventStoreServiceId = $eventStoreServiceId; |
200
|
1 |
|
$this->snapshotStoreServiceId = $snapshotStoreServiceId; |
201
|
1 |
|
$this->indexerStore = $indexerStore; |
202
|
1 |
|
} |
203
|
|
|
} |
204
|
|
|
|