1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace TomCizek\SymfonyProoph\EventSourcing; |
4
|
|
|
|
5
|
|
|
use Prooph\EventSourcing\Container\Aggregate\AggregateRepositoryFactory; |
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
use TomCizek\SymfonyProoph\Common\DefaultConfigurator; |
9
|
|
|
|
10
|
|
|
class EventSourcingConfigurator extends DefaultConfigurator |
11
|
|
|
{ |
12
|
|
|
public const KEY = 'event_sourcing'; |
13
|
|
|
public const KEY_AGGREGATE_REPOSITORIES = 'aggregate_repository'; |
14
|
|
|
public const KEY_REPOSITORY_CLASS = 'repository_class'; |
15
|
|
|
|
16
|
18 |
|
public function buildDefaultConfig(): array |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
18 |
|
self::KEY_AGGREGATE_REPOSITORIES => [], |
20
|
|
|
]; |
21
|
|
|
} |
22
|
|
|
|
23
|
18 |
|
public function getConfigKey(): string |
24
|
|
|
{ |
25
|
18 |
|
return self::KEY; |
26
|
|
|
} |
27
|
|
|
|
28
|
16 |
|
public function configureWithConfig(array $config): void |
29
|
|
|
{ |
30
|
16 |
|
$repositoriesConfigs = $config[self::KEY_AGGREGATE_REPOSITORIES]; |
31
|
|
|
|
32
|
16 |
|
$interopContainerServiceId = $this->getInteropContainerServiceId(); |
33
|
|
|
|
34
|
16 |
|
foreach ($repositoriesConfigs as $repositoryConfigName => $repositoryConfig) { |
35
|
5 |
|
$repositoryClass = $repositoryConfig[self::KEY_REPOSITORY_CLASS]; |
36
|
|
|
|
37
|
5 |
|
$this->setRepositoryDefinition($repositoryClass, $repositoryConfigName, $interopContainerServiceId); |
38
|
5 |
|
$this->setBasicAlias($repositoryClass, $repositoryConfigName); |
39
|
5 |
|
$this->setAliasesFromRepositoryImplementations($repositoryClass, $repositoryConfigName); |
40
|
|
|
} |
41
|
16 |
|
} |
42
|
|
|
|
43
|
5 |
|
private function setRepositoryDefinition( |
44
|
|
|
string $repositoryClass, |
45
|
|
|
string $repositoryConfigName, |
46
|
|
|
string $interopContainerServiceId |
47
|
|
|
): void { |
48
|
5 |
|
$repositoryDefinition = new Definition($repositoryClass); |
49
|
|
|
$repositoryDefinition |
50
|
5 |
|
->setClass($repositoryClass) |
51
|
5 |
|
->setFactory(AggregateRepositoryFactory::class . '::' . $repositoryConfigName) |
52
|
5 |
|
->addArgument( |
53
|
5 |
|
new Reference($interopContainerServiceId) |
54
|
|
|
); |
55
|
|
|
|
56
|
5 |
|
$this->containerBuilder->setDefinition( |
57
|
5 |
|
$this->getRepositoryServiceId($repositoryConfigName), |
58
|
5 |
|
$repositoryDefinition |
59
|
|
|
); |
60
|
5 |
|
} |
61
|
|
|
|
62
|
5 |
|
private function setAliasesFromRepositoryImplementations( |
63
|
|
|
string $repositoryClass, |
64
|
|
|
string $repositoryConfigName |
65
|
|
|
): void { |
66
|
5 |
|
$implementations = class_implements($repositoryClass); |
67
|
5 |
|
foreach ($implementations as $implementation) { |
68
|
5 |
|
$this->containerBuilder->setAlias( |
69
|
5 |
|
$implementation, |
70
|
5 |
|
$this->getRepositoryServiceId($repositoryConfigName) |
71
|
|
|
); |
72
|
|
|
} |
73
|
5 |
|
} |
74
|
|
|
|
75
|
5 |
|
private function setBasicAlias(string $repositoryClass, string $repositoryConfigName): void |
76
|
|
|
{ |
77
|
5 |
|
$this->containerBuilder->setAlias( |
78
|
5 |
|
$repositoryClass, |
79
|
5 |
|
$this->getRepositoryServiceId($repositoryConfigName) |
80
|
|
|
); |
81
|
5 |
|
} |
82
|
|
|
|
83
|
5 |
|
private function getRepositoryPrefix(): string |
84
|
|
|
{ |
85
|
5 |
|
return 'prooph.' . self::KEY . '.'; |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
private function getRepositoryServiceId(string $repositoryConfigName): string |
89
|
|
|
{ |
90
|
5 |
|
return $this->getRepositoryPrefix() . $repositoryConfigName; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|