1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* The software is based on the Axon Framework project which is |
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework |
18
|
|
|
* see <http://www.axonframework.org/>. |
19
|
|
|
* |
20
|
|
|
* This software consists of voluntary contributions made by many individuals |
21
|
|
|
* and is licensed under the MIT license. For more information, see |
22
|
|
|
* <http://www.governor-framework.org/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Governor\Bundle\GovernorBundle\DependencyInjection; |
26
|
|
|
|
27
|
|
|
use Symfony\Component\Finder\Finder; |
28
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
29
|
|
|
use Symfony\Component\Config\FileLocator; |
30
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
31
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
32
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
33
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
34
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
35
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Class GovernorFrameworkExtension. |
39
|
|
|
* |
40
|
|
|
* @author "David Kalosi" <[email protected]> |
41
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
42
|
|
|
*/ |
43
|
|
|
class GovernorFrameworkExtension extends Extension |
44
|
|
|
{ |
45
|
|
|
|
46
|
11 |
|
public function load(array $configs, ContainerBuilder $container) |
47
|
|
|
{ |
48
|
11 |
|
$config = $this->processConfiguration(new Configuration, $configs); |
49
|
|
|
|
50
|
11 |
|
$container->setAlias( |
51
|
11 |
|
'governor.lock_manager', |
52
|
11 |
|
new Alias( |
53
|
11 |
|
sprintf( |
54
|
11 |
|
'governor.lock_manager.%s', |
55
|
11 |
|
$config['lock_manager'] |
56
|
11 |
|
) |
57
|
11 |
|
) |
58
|
11 |
|
); |
59
|
|
|
|
60
|
11 |
|
$container->setAlias( |
61
|
11 |
|
'governor.command_target_resolver', |
62
|
11 |
|
new Alias( |
63
|
11 |
|
sprintf( |
64
|
11 |
|
'governor.command_target_resolver.%s', |
65
|
11 |
|
$config['command_target_resolver'] |
66
|
11 |
|
) |
67
|
11 |
|
) |
68
|
11 |
|
); |
69
|
|
|
|
70
|
11 |
|
$container->setAlias( |
71
|
11 |
|
'governor.order_resolver', |
72
|
11 |
|
new Alias( |
73
|
11 |
|
sprintf( |
74
|
11 |
|
'governor.order_resolver.%s', |
75
|
11 |
|
$config['order_resolver'] |
76
|
11 |
|
) |
77
|
11 |
|
) |
78
|
11 |
|
); |
79
|
|
|
|
80
|
11 |
|
$container->setAlias( |
81
|
11 |
|
'governor.serializer', |
82
|
11 |
|
new Alias( |
83
|
11 |
|
sprintf( |
84
|
11 |
|
'governor.serializer.%s', |
85
|
11 |
|
$config['serializer'] |
86
|
11 |
|
) |
87
|
11 |
|
) |
88
|
11 |
|
); |
89
|
|
|
|
90
|
11 |
|
$container->setAlias('governor.uow_factory', new Alias($config['uow_factory'])); |
91
|
|
|
|
92
|
11 |
|
$container->setParameter('governor.aggregates', $config['aggregates']); |
93
|
11 |
|
$container->setParameter('governor.node_name', $config['node_name']); |
94
|
|
|
|
95
|
11 |
|
$loader = new XmlFileLoader( |
96
|
11 |
|
$container, |
97
|
11 |
|
new FileLocator(__DIR__.'/../Resources/config') |
98
|
11 |
|
); |
99
|
11 |
|
$loader->load('services.xml'); |
100
|
|
|
|
101
|
|
|
// configure annotation reader |
102
|
11 |
|
$this->loadAnnotationReader($config, $container); |
103
|
|
|
// configure routing strategies |
104
|
11 |
|
$this->loadRoutingStrategies($config, $container); |
105
|
|
|
//configure terminals |
106
|
11 |
|
$this->loadTerminals($config, $container); |
107
|
|
|
//configure connectors |
108
|
11 |
|
$this->loadConnectors($config, $container); |
109
|
|
|
// configure command buses |
110
|
11 |
|
$this->loadCommandBuses($config, $container); |
111
|
|
|
// configure event buses |
112
|
11 |
|
$this->loadEventBuses($config, $container); |
113
|
|
|
// configure command gateways |
114
|
11 |
|
$this->loadCommandGateways($config, $container); |
115
|
|
|
// configure repositories |
116
|
11 |
|
$this->loadRepositories($config, $container); |
117
|
|
|
// configure event store |
118
|
11 |
|
$this->loadEventStore($config, $container); |
119
|
|
|
// configure saga repository |
120
|
11 |
|
$this->loadSagaRepository($config, $container); |
121
|
|
|
// configure saga manager |
122
|
11 |
|
$this->loadSagaManager($config, $container); |
123
|
11 |
|
} |
124
|
|
|
|
125
|
11 |
|
private function loadRoutingStrategies($config, ContainerBuilder $container) |
126
|
|
|
{ |
127
|
11 |
|
if (!isset($config['routing_strategies'])) { |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
11 |
|
foreach ($config['routing_strategies'] as $name => $strategy) { |
132
|
11 |
|
switch ($strategy['type']) { |
133
|
11 |
|
case 'metadata': |
134
|
11 |
|
$definition = new Definition( |
135
|
11 |
|
$container->getParameter(sprintf('governor.routing_strategy.%s.class', $strategy['type'])) |
136
|
11 |
|
); |
137
|
|
|
|
138
|
11 |
|
$definition->addArgument($strategy['parameters']['key_name']); |
139
|
11 |
|
$container->setDefinition(sprintf('governor.routing_strategy.%s', $name), $definition); |
140
|
11 |
|
break; |
141
|
11 |
|
case 'annotation': |
142
|
11 |
|
$definition = new Definition( |
143
|
11 |
|
$container->getParameter(sprintf('governor.routing_strategy.%s.class', $strategy['type'])) |
144
|
11 |
|
); |
145
|
|
|
|
146
|
11 |
|
$definition->addArgument(new Reference('governor.annotation_reader_factory')); |
147
|
11 |
|
$container->setDefinition(sprintf('governor.routing_strategy.%s', $name), $definition); |
148
|
11 |
|
break; |
149
|
11 |
|
} |
150
|
11 |
|
} |
151
|
11 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $config |
155
|
|
|
* @param ContainerBuilder $container |
156
|
|
|
*/ |
157
|
11 |
|
private function loadMongoTemplates($location, $config, ContainerBuilder $container) |
158
|
|
|
{ |
159
|
11 |
|
if (!isset($config['mongo_templates'])) { |
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
11 |
|
foreach ($config['mongo_templates'] as $name => $params) { |
164
|
11 |
|
$definition = new Definition( |
165
|
11 |
|
$container->getParameter(sprintf('governor.%s.mongo_template.default.class', $location)) |
166
|
11 |
|
); |
167
|
|
|
|
168
|
11 |
|
$definition->addArgument($params['server']); |
169
|
11 |
|
$definition->addArgument($params['database']); |
170
|
|
|
|
171
|
11 |
|
if (isset($params['auth_database'])) { |
172
|
|
|
$definition->addArgument($params['auth_database']); |
173
|
|
|
} |
174
|
|
|
|
175
|
11 |
|
if (isset($params['event_collection'])) { |
176
|
|
|
$definition->addArgument($params['event_collection']); |
177
|
|
|
} |
178
|
|
|
|
179
|
11 |
|
if (isset($params['snapshot_collection'])) { |
180
|
|
|
$definition->addArgument($params['snapshot_collection']); |
181
|
|
|
} |
182
|
|
|
|
183
|
11 |
|
$container->setDefinition(sprintf('governor.%s.mongo_template.%s', $location, $name), $definition); |
184
|
11 |
|
} |
185
|
|
|
|
186
|
11 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $config |
190
|
|
|
* @param ContainerBuilder $container |
191
|
|
|
*/ |
192
|
11 |
|
private function loadAnnotationReader($config, ContainerBuilder $container) |
193
|
|
|
{ |
194
|
11 |
|
$definition = new Definition( |
195
|
11 |
|
$container->getParameter( |
196
|
11 |
|
sprintf('governor.annotation_reader_factory.%s.class', $config['annotation_reader']['type']) |
197
|
11 |
|
) |
198
|
11 |
|
); |
199
|
|
|
|
200
|
11 |
|
switch ($config['annotation_reader']['type']) { |
201
|
11 |
|
case 'simple': |
202
|
|
|
break; |
203
|
11 |
|
case 'file_cache': |
204
|
11 |
|
$definition->addArgument($config['annotation_reader']['parameters']['path']); |
205
|
11 |
|
$definition->addArgument($config['annotation_reader']['parameters']['debug']); |
206
|
11 |
|
break; |
207
|
11 |
|
} |
208
|
|
|
|
209
|
11 |
|
$container->setDefinition('governor.annotation_reader_factory', $definition); |
210
|
11 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param $config |
214
|
|
|
* @param ContainerBuilder $container |
215
|
|
|
*/ |
216
|
11 |
|
private function configureAmqpTerminals($config, ContainerBuilder $container) |
217
|
|
|
{ |
218
|
11 |
|
foreach ($config as $name => $terminal) { |
219
|
11 |
|
$connectionDefinition = new Definition( |
220
|
11 |
|
$container->getParameter('governor.amqp.connection.class'), |
221
|
|
|
[ |
222
|
11 |
|
$terminal['host'], |
223
|
11 |
|
$terminal['port'], |
224
|
11 |
|
$terminal['user'], |
225
|
11 |
|
$terminal['password'], |
226
|
11 |
|
$terminal['vhost'] |
227
|
11 |
|
] |
228
|
11 |
|
); |
229
|
|
|
|
230
|
11 |
|
$connectionDefinition->setLazy(true); |
231
|
|
|
|
232
|
11 |
|
$container->setDefinition( |
233
|
11 |
|
sprintf( |
234
|
11 |
|
"governor.amqp.connection.%s", |
235
|
|
|
$name |
236
|
11 |
|
), |
237
|
|
|
$connectionDefinition |
238
|
11 |
|
); |
239
|
|
|
|
240
|
11 |
|
$definition = new Definition($container->getParameter('governor.terminal.amqp.class')); |
241
|
11 |
|
$definition->addArgument(new Reference('governor.serializer')); |
242
|
11 |
|
$definition->addMethodCall( |
243
|
11 |
|
'setConnection', |
244
|
|
|
[ |
245
|
11 |
|
new Reference( |
246
|
11 |
|
sprintf( |
247
|
11 |
|
"governor.amqp.connection.%s", |
248
|
|
|
$name |
249
|
11 |
|
) |
250
|
11 |
|
) |
251
|
11 |
|
] |
252
|
11 |
|
); |
253
|
|
|
|
254
|
11 |
|
$definition->addMethodCall( |
255
|
11 |
|
'setLogger', |
256
|
11 |
|
array(new Reference('logger')) |
257
|
11 |
|
); |
258
|
|
|
|
259
|
11 |
|
if (isset($terminal['routing_key_resolver'])) { |
260
|
|
|
$definition->addMethodCall( |
261
|
|
|
'setRoutingKeyResolver', |
262
|
|
|
array(new Reference('routing_key_resolver')) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
11 |
|
$container->setDefinition( |
267
|
11 |
|
sprintf("governor.terminal.amqp.%s", $name), |
268
|
|
|
$definition |
269
|
11 |
|
); |
270
|
11 |
|
} |
271
|
11 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param array $config |
275
|
|
|
* @param ContainerBuilder $container |
276
|
|
|
*/ |
277
|
11 |
|
private function loadTerminals($config, ContainerBuilder $container) |
278
|
|
|
{ |
279
|
11 |
|
if (empty($config['terminals'])) { |
280
|
|
|
return; |
281
|
|
|
} |
282
|
|
|
|
283
|
11 |
|
foreach ($config['terminals'] as $type => $data) { |
284
|
|
|
switch ($type) { |
285
|
11 |
|
case 'amqp': |
286
|
11 |
|
$this->configureAmqpTerminals($data, $container); |
287
|
11 |
|
break; |
288
|
|
|
} |
289
|
11 |
|
} |
290
|
11 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param $config |
294
|
|
|
* @param ContainerBuilder $container |
295
|
|
|
*/ |
296
|
11 |
|
private function loadConnectors($config, ContainerBuilder $container) |
297
|
|
|
{ |
298
|
11 |
|
if (empty($config['connectors'])) { |
299
|
|
|
return; |
300
|
|
|
} |
301
|
|
|
|
302
|
11 |
|
foreach ($config['connectors'] as $type => $data) { |
303
|
|
|
switch ($type) { |
304
|
11 |
|
case 'redis': |
305
|
11 |
|
$this->configureRedisConnectors($data, $container); |
306
|
11 |
|
break; |
307
|
|
|
} |
308
|
11 |
|
} |
309
|
11 |
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param $config |
313
|
|
|
* @param ContainerBuilder $container |
314
|
|
|
*/ |
315
|
11 |
|
private function configureRedisConnectors($config, ContainerBuilder $container) |
316
|
|
|
{ |
317
|
11 |
|
foreach ($config as $name => $connector) { |
318
|
11 |
|
$templateId = sprintf('governor.connector.%s.template', $name); |
319
|
|
|
|
320
|
11 |
|
$templateDefinition = new Definition($container->getParameter('governor.connector_template.redis.class')); |
321
|
11 |
|
$templateDefinition->addArgument($connector['url']); |
322
|
11 |
|
$templateDefinition->addArgument($container->getParameter('governor.node_name')); |
323
|
11 |
|
$templateDefinition->addArgument([]); |
324
|
|
|
|
325
|
11 |
|
$container->setDefinition($templateId, $templateDefinition); |
326
|
|
|
|
327
|
11 |
|
$definition = new Definition($container->getParameter('governor.connector.redis.class')); |
328
|
11 |
|
$definition->addArgument(new Reference($templateId)); |
329
|
11 |
|
$definition->addArgument(new Reference(sprintf('governor.command_bus.%s', $connector['local_segment']))); |
330
|
11 |
|
$definition->addArgument(new Reference('governor.serializer')); |
331
|
|
|
|
332
|
11 |
|
$container->setDefinition(sprintf('governor.connector.%s', $name), $definition); |
333
|
|
|
|
334
|
|
|
// cache warmer & clarer |
335
|
11 |
|
$warmerDefinition = new Definition($container->getParameter('governor.connector_cache_warmer.redis.class')); |
336
|
11 |
|
$warmerDefinition->addArgument(new Reference(sprintf('governor.connector.%s', $name))); |
337
|
11 |
|
$warmerDefinition->addMethodCall('setLogger', [new Reference('logger')]); |
338
|
11 |
|
$warmerDefinition->addTag('kernel.cache_warmer'); |
339
|
|
|
|
340
|
11 |
|
$container->setDefinition(sprintf('governor.connector_cache_warmer.%s', $name), $warmerDefinition); |
341
|
|
|
|
342
|
11 |
|
$clearerDefinition = new Definition($container->getParameter('governor.connector_cache_clearer.redis.class')); |
343
|
11 |
|
$clearerDefinition->addArgument(new Reference(sprintf('governor.connector.%s', $name))); |
344
|
11 |
|
$clearerDefinition->addMethodCall('setLogger', [new Reference('logger')]); |
345
|
11 |
|
$clearerDefinition->addTag('kernel.cache_clearer'); |
346
|
|
|
|
347
|
11 |
|
$container->setDefinition(sprintf('governor.connector_cache_clearer.%s', $name), $clearerDefinition); |
348
|
|
|
|
349
|
|
|
// connector receiver |
350
|
11 |
|
$receiverDefinition = new Definition($container->getParameter('governor.connector_receiver.redis.class')); |
351
|
11 |
|
$receiverDefinition->addArgument(new Reference($templateId)); |
352
|
11 |
|
$receiverDefinition->addArgument(new Reference(sprintf('governor.command_bus.%s', $connector['local_segment']))); |
353
|
11 |
|
$receiverDefinition->addArgument(new Reference('governor.serializer')); |
354
|
11 |
|
$receiverDefinition->addMethodCall('setLogger', [new Reference('logger')]); |
355
|
|
|
|
356
|
11 |
|
$container->setDefinition(sprintf('governor.connector_receiver.%s', $name), $receiverDefinition); |
357
|
11 |
|
} |
358
|
11 |
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param array $config |
362
|
|
|
* @param ContainerBuilder $container |
363
|
|
|
*/ |
364
|
11 |
|
private function loadCommandBuses($config, ContainerBuilder $container) |
365
|
|
|
{ |
366
|
11 |
|
foreach ($config['command_buses'] as $name => $bus) { |
367
|
|
|
|
368
|
11 |
|
$definition = new Definition( |
369
|
11 |
|
$container->getParameter(sprintf('governor.command_bus.%s.class', $bus['type'])) |
370
|
11 |
|
); |
371
|
|
|
|
372
|
11 |
|
$definition->addMethodCall('setLogger', [new Reference('logger')]); |
373
|
11 |
|
$definition->addMethodCall( |
374
|
11 |
|
'setDispatchInterceptors', |
375
|
|
|
[ |
376
|
11 |
|
array_map( |
377
|
|
|
function ($interceptor) { |
378
|
|
|
return new Reference($interceptor); |
379
|
11 |
|
}, |
380
|
11 |
|
$bus['dispatch_interceptors'] |
381
|
11 |
|
) |
382
|
11 |
|
] |
383
|
11 |
|
); |
384
|
|
|
|
385
|
11 |
|
switch ($bus['type']) { |
386
|
11 |
|
case 'simple': |
387
|
11 |
|
$this->configureSimpleCommandBus($definition, $bus); |
388
|
11 |
|
break; |
389
|
11 |
|
case 'distributed': |
390
|
11 |
|
$this->configureDistributedCommandBus($definition, $bus); |
391
|
11 |
|
break; |
392
|
|
|
default: |
393
|
|
|
throw new \RuntimeException(sprintf('Unknown command bus type %s', $bus['type'])); |
394
|
11 |
|
} |
395
|
|
|
|
396
|
11 |
|
$container->setDefinition( |
397
|
11 |
|
sprintf("governor.command_bus.%s", $name), |
398
|
|
|
$definition |
399
|
11 |
|
); |
400
|
11 |
|
} |
401
|
|
|
|
402
|
11 |
|
if (!$container->hasDefinition('governor.command_bus.default')) { |
403
|
|
|
throw new \RuntimeException( |
404
|
|
|
"Missing default command bus configuration, a command bus with the name \"default\" has to be configured." |
405
|
|
|
); |
406
|
|
|
} |
407
|
11 |
|
} |
408
|
|
|
|
409
|
11 |
|
private function configureSimpleCommandBus(Definition $definition, $bus) |
410
|
|
|
{ |
411
|
11 |
|
$definition->addArgument(new Reference('governor.uow_factory')); |
412
|
11 |
|
$definition->addMethodCall( |
413
|
11 |
|
'setHandlerInterceptors', |
414
|
|
|
[ |
415
|
11 |
|
array_map( |
416
|
11 |
|
function ($interceptor) { |
417
|
|
|
return new Reference($interceptor); |
418
|
11 |
|
}, |
419
|
11 |
|
$bus['handler_interceptors'] |
420
|
11 |
|
) |
421
|
11 |
|
] |
422
|
11 |
|
); |
423
|
11 |
|
} |
424
|
|
|
|
425
|
11 |
|
private function configureDistributedCommandBus(Definition $definition, $bus) |
426
|
|
|
{ |
427
|
11 |
|
$definition->addArgument(new Reference(sprintf('governor.connector.%s', $bus['connector']))); |
428
|
11 |
|
$definition->addArgument(new Reference(sprintf('governor.routing_strategy.%s', $bus['routing_strategy']))); |
429
|
11 |
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param array $config |
433
|
|
|
* @param ContainerBuilder $container |
434
|
|
|
*/ |
435
|
11 |
|
private function loadEventBuses($config, ContainerBuilder $container) |
436
|
|
|
{ |
437
|
11 |
|
foreach ($config['event_buses'] as $name => $bus) { |
438
|
11 |
|
$terminals = []; |
439
|
|
|
|
440
|
11 |
|
$template = $container->findDefinition($bus['registry']); |
441
|
11 |
|
$registryDefinition = new Definition($template->getClass()); |
442
|
11 |
|
$registryDefinition->setArguments($template->getArguments()); |
443
|
|
|
|
444
|
11 |
|
$container->setDefinition(sprintf('governor.event_bus.registry.%s', $name), $registryDefinition); |
445
|
|
|
|
446
|
11 |
|
$definition = new Definition($bus['class']); |
447
|
11 |
|
$definition->addArgument(new Reference(sprintf('governor.event_bus.registry.%s', $name))); |
448
|
11 |
|
$definition->addMethodCall( |
449
|
11 |
|
'setLogger', |
450
|
11 |
|
[new Reference('logger')] |
451
|
11 |
|
); |
452
|
|
|
|
453
|
11 |
|
foreach ($bus['terminals'] as $terminal) { |
454
|
11 |
|
$terminals[] = new Reference($terminal); |
455
|
11 |
|
} |
456
|
|
|
|
457
|
11 |
|
$definition->addMethodCall('setTerminals', [$terminals]); |
458
|
|
|
|
459
|
11 |
|
$container->setDefinition( |
460
|
11 |
|
sprintf("governor.event_bus.%s", $name), |
461
|
|
|
$definition |
462
|
11 |
|
); |
463
|
11 |
|
} |
464
|
|
|
|
465
|
11 |
|
if (!$container->hasDefinition('governor.event_bus.default')) { |
466
|
|
|
throw new \RuntimeException( |
467
|
|
|
"Missing default event bus configuration, an event bus with the name \"default\" has to be configured." |
468
|
|
|
); |
469
|
|
|
} |
470
|
11 |
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* @param array $config |
474
|
|
|
* @param ContainerBuilder $container |
475
|
|
|
*/ |
476
|
11 |
|
private function loadCommandGateways($config, ContainerBuilder $container) |
477
|
|
|
{ |
478
|
11 |
|
foreach ($config['command_gateways'] as $name => $gateway) { |
479
|
11 |
|
$definition = new Definition($gateway['class']); |
480
|
11 |
|
$definition->addArgument( |
481
|
11 |
|
new Reference( |
482
|
11 |
|
sprintf( |
483
|
11 |
|
"governor.command_bus.%s", |
484
|
11 |
|
$gateway['command_bus'] |
485
|
11 |
|
) |
486
|
11 |
|
) |
487
|
11 |
|
); |
488
|
|
|
|
489
|
11 |
|
$container->setDefinition( |
490
|
11 |
|
sprintf( |
491
|
11 |
|
"governor.command_gateway.%s", |
492
|
|
|
$name |
493
|
11 |
|
), |
494
|
|
|
$definition |
495
|
11 |
|
); |
496
|
11 |
|
} |
497
|
11 |
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* @param array $config |
501
|
|
|
* @param ContainerBuilder $container |
502
|
|
|
*/ |
503
|
11 |
|
private function loadSagaRepository($config, ContainerBuilder $container) |
504
|
|
|
{ |
505
|
11 |
|
if (!isset($config['saga_repository'])) { |
506
|
|
|
return; |
507
|
|
|
} |
508
|
|
|
|
509
|
11 |
|
$this->loadMongoTemplates('saga_repository', $config['saga_repository'], $container); |
510
|
|
|
|
511
|
11 |
|
$definition = new Definition( |
512
|
11 |
|
$container->getParameter( |
513
|
11 |
|
sprintf( |
514
|
11 |
|
"governor.saga_repository.%s.class", |
515
|
11 |
|
$config['saga_repository']['type'] |
516
|
11 |
|
) |
517
|
11 |
|
) |
518
|
11 |
|
); |
519
|
|
|
|
520
|
11 |
|
$serviceId = sprintf( |
521
|
11 |
|
"governor.saga_repository.%s", |
522
|
11 |
|
$config['saga_repository']['type'] |
523
|
11 |
|
); |
524
|
|
|
|
525
|
11 |
|
switch ($config['saga_repository']['type']) { |
526
|
11 |
View Code Duplication |
case 'orm': |
|
|
|
|
527
|
|
|
$definition->addArgument( |
528
|
|
|
new Reference( |
529
|
|
|
sprintf( |
530
|
|
|
'doctrine.orm.%s', |
531
|
|
|
$config['saga_repository']['parameters']['entity_manager'] |
532
|
|
|
) |
533
|
|
|
) |
534
|
|
|
); |
535
|
|
|
$definition->addArgument(new Reference('governor.resource_injector')); |
536
|
|
|
$definition->addArgument(new Reference('governor.serializer')); |
537
|
|
|
break; |
538
|
11 |
View Code Duplication |
case 'mongo': |
|
|
|
|
539
|
11 |
|
$definition->addArgument( |
540
|
11 |
|
new Reference($config['saga_repository']['parameters']['mongo_template']) |
541
|
11 |
|
); |
542
|
11 |
|
$definition->addArgument(new Reference('governor.resource_injector')); |
543
|
11 |
|
$definition->addArgument(new Reference('governor.serializer')); |
544
|
11 |
|
break; |
545
|
11 |
|
} |
546
|
|
|
|
547
|
11 |
|
$definition->addMethodCall('setLogger', array(new Reference('logger'))); |
548
|
|
|
|
549
|
11 |
|
$container->setDefinition($serviceId, $definition); |
550
|
11 |
|
$container->setAlias('governor.saga_repository', $serviceId); |
551
|
11 |
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @param array $config |
555
|
|
|
* @param ContainerBuilder $container |
556
|
|
|
*/ |
557
|
11 |
|
private function loadSagaManager($config, ContainerBuilder $container) |
558
|
|
|
{ |
559
|
11 |
|
if (!isset($config['saga_manager'])) { |
560
|
|
|
return; |
561
|
|
|
} |
562
|
|
|
|
563
|
11 |
|
$finder = new Finder(); |
564
|
11 |
|
$finder->files()->in($config['saga_manager']['saga_locations']); |
565
|
11 |
|
$classes = array(); |
566
|
|
|
|
567
|
|
|
// !!! TODO this is temporary and very poor |
568
|
11 |
|
foreach ($finder as $file) { |
569
|
11 |
|
if (preg_match("/^.*\/src\/(.*)\.php$/", $file, $matches)) { |
570
|
|
|
$classes[] = str_replace('/', '\\', $matches[1]); |
571
|
|
|
} |
572
|
11 |
|
} |
573
|
|
|
|
574
|
11 |
|
$container->setParameter('governor.sagas', $classes); |
575
|
|
|
|
576
|
11 |
|
$registry = $container->findDefinition( |
577
|
11 |
|
sprintf( |
578
|
11 |
|
"governor.event_bus.registry.%s", |
579
|
11 |
|
$config['saga_manager']['event_bus'] |
580
|
11 |
|
) |
581
|
11 |
|
); |
582
|
11 |
|
$registry->addMethodCall( |
583
|
11 |
|
'subscribe', |
584
|
11 |
|
[new Reference('governor.saga_manager')] |
585
|
11 |
|
); |
586
|
|
|
|
587
|
11 |
|
$definition = new Definition($container->getParameter('governor.saga_manager.annotation.class')); |
588
|
11 |
|
$definition->addArgument(new Reference('governor.saga_repository')); |
589
|
11 |
|
$definition->addArgument(new Reference('governor.saga_factory')); |
590
|
11 |
|
$definition->addArgument($container->getParameter('governor.sagas')); |
591
|
11 |
|
$definition->addMethodCall('setLogger', array(new Reference('logger'))); |
592
|
|
|
|
593
|
11 |
|
$container->setDefinition('governor.saga_manager', $definition); |
594
|
11 |
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* @param array $config |
598
|
|
|
* @param ContainerBuilder $container |
599
|
|
|
*/ |
600
|
11 |
|
private function loadEventStore($config, ContainerBuilder $container) |
601
|
|
|
{ |
602
|
11 |
|
if (!array_key_exists('event_store', $config)) { |
603
|
|
|
return; |
604
|
|
|
} |
605
|
|
|
|
606
|
11 |
|
$this->loadMongoTemplates('event_store', $config['event_store'], $container); |
607
|
|
|
|
608
|
11 |
|
$definition = new Definition( |
609
|
11 |
|
$container->getParameter( |
610
|
11 |
|
sprintf( |
611
|
11 |
|
"governor.event_store.%s.class", |
612
|
11 |
|
$config['event_store']['type'] |
613
|
11 |
|
) |
614
|
11 |
|
) |
615
|
11 |
|
); |
616
|
11 |
|
$serviceId = sprintf( |
617
|
11 |
|
'governor.event_store.%s', |
618
|
11 |
|
$config['event_store']['type'] |
619
|
11 |
|
); |
620
|
|
|
|
621
|
11 |
|
switch ($config['event_store']['type']) { |
622
|
11 |
|
case 'filesystem': |
623
|
|
|
break; |
624
|
11 |
|
case 'orm': |
625
|
|
|
$definition->addArgument( |
626
|
|
|
new Reference( |
627
|
|
|
sprintf( |
628
|
|
|
'doctrine.orm.%s', |
629
|
|
|
$config['event_store']['parameters']['entity_manager'] |
630
|
|
|
) |
631
|
|
|
) |
632
|
|
|
); |
633
|
|
|
$definition->addArgument(new Reference('governor.serializer')); |
634
|
|
|
|
635
|
|
|
if (isset($config['event_store']['parameters']['entry_store'])) { |
636
|
|
|
$definition->addArgument(new Reference($config['event_store']['parameters']['entry_store'])); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
break; |
640
|
11 |
|
case 'mongo': |
641
|
11 |
|
$definition->addArgument(new Reference($config['event_store']['parameters']['mongo_template'])); |
642
|
11 |
|
$definition->addArgument(new Reference('governor.serializer')); |
643
|
11 |
|
$definition->addArgument(new Reference($config['event_store']['parameters']['storage_strategy'])); |
644
|
|
|
|
645
|
11 |
|
break; |
646
|
11 |
|
} |
647
|
|
|
|
648
|
11 |
|
$definition->addMethodCall('setLogger', array(new Reference('logger'))); |
649
|
|
|
|
650
|
11 |
|
$container->setDefinition($serviceId, $definition); |
651
|
11 |
|
$container->setAlias('governor.event_store', $serviceId); |
652
|
11 |
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* @param array $config |
656
|
|
|
* @param ContainerBuilder $container |
657
|
|
|
*/ |
658
|
11 |
|
private function loadRepositories($config, ContainerBuilder $container) |
659
|
|
|
{ |
660
|
11 |
|
foreach ($config['aggregates'] as $name => $parameters) { |
661
|
11 |
|
$repository = new DefinitionDecorator( |
662
|
11 |
|
sprintf( |
663
|
11 |
|
'governor.repository.%s', |
664
|
11 |
|
$parameters['repository'] |
665
|
11 |
|
) |
666
|
11 |
|
); |
667
|
|
|
|
668
|
11 |
|
$repository->replaceArgument(0, $parameters['class']) |
669
|
11 |
|
->setPublic(true); |
670
|
11 |
|
$repository->replaceArgument( |
671
|
11 |
|
1, |
672
|
11 |
|
new Reference( |
673
|
11 |
|
sprintf( |
674
|
11 |
|
"governor.event_bus.%s", |
675
|
11 |
|
$parameters['event_bus'] |
676
|
11 |
|
) |
677
|
11 |
|
) |
678
|
11 |
|
); |
679
|
|
|
|
680
|
11 |
|
if ($parameters['repository'] === 'event_sourcing' && |
681
|
11 |
|
isset($parameters['factory']) |
682
|
11 |
|
) { |
683
|
|
|
$repository->replaceArgument( |
684
|
|
|
4, |
685
|
|
|
new Reference($parameters['factory']) |
686
|
|
|
); |
687
|
|
|
} |
688
|
|
|
|
689
|
11 |
|
$container->setDefinition( |
690
|
11 |
|
sprintf('%s.repository', $name), |
691
|
|
|
$repository |
692
|
11 |
|
); |
693
|
11 |
|
} |
694
|
11 |
|
} |
695
|
|
|
|
696
|
|
|
} |
697
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.