-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathProjectionStateStorageRegistryModule.php
More file actions
104 lines (88 loc) · 4.06 KB
/
ProjectionStateStorageRegistryModule.php
File metadata and controls
104 lines (88 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* licence Apache-2.0
*/
declare(strict_types=1);
namespace Ecotone\Projecting\Config;
use function array_map;
use Ecotone\AnnotationFinder\AnnotationFinder;
use Ecotone\Messaging\Attribute\ModuleAnnotation;
use Ecotone\Messaging\Config\Annotation\AnnotatedDefinitionReference;
use Ecotone\Messaging\Config\Annotation\AnnotationModule;
use Ecotone\Messaging\Config\Annotation\ModuleConfiguration\ExtensionObjectResolver;
use Ecotone\Messaging\Config\Annotation\ModuleConfiguration\NoExternalConfigurationModule;
use Ecotone\Messaging\Config\Configuration;
use Ecotone\Messaging\Config\Container\Definition;
use Ecotone\Messaging\Config\Container\Reference;
use Ecotone\Messaging\Config\ModulePackageList;
use Ecotone\Messaging\Config\ModuleReferenceSearchService;
use Ecotone\Messaging\Handler\InterfaceToCallRegistry;
use Ecotone\Messaging\Support\LicensingException;
use Ecotone\Projecting\Attribute\ProjectionV2;
use Ecotone\Projecting\Attribute\StateStorage as StateStorageAttribute;
use Ecotone\Projecting\InMemory\InMemoryProjectionStateStorage;
use Ecotone\Projecting\ProjectionStateStorageReference;
use Ecotone\Projecting\ProjectionStateStorageRegistry;
#[ModuleAnnotation]
class ProjectionStateStorageRegistryModule extends NoExternalConfigurationModule implements AnnotationModule
{
/**
* @param string[] $allProjectionNames
* @param string[] $userlandStateStorageReferences
*/
public function __construct(
private array $allProjectionNames = [],
private array $userlandStateStorageReferences = [],
) {
}
public static function create(AnnotationFinder $annotationFinder, InterfaceToCallRegistry $interfaceToCallRegistry): static
{
$allProjectionNames = [];
foreach ($annotationFinder->findAnnotatedClasses(ProjectionV2::class) as $projectionClassName) {
$projectionAttribute = $annotationFinder->getAttributeForClass($projectionClassName, ProjectionV2::class);
$allProjectionNames[] = $projectionAttribute->name;
}
$userlandStateStorageReferences = [];
foreach ($annotationFinder->findAnnotatedClasses(StateStorageAttribute::class) as $storageClassName) {
$userlandStateStorageReferences[] = AnnotatedDefinitionReference::getReferenceForClassName($annotationFinder, $storageClassName);
}
return new self($allProjectionNames, $userlandStateStorageReferences);
}
public function prepare(Configuration $messagingConfiguration, array $extensionObjects, ModuleReferenceSearchService $moduleReferenceSearchService, InterfaceToCallRegistry $interfaceToCallRegistry): void
{
if (! empty($this->userlandStateStorageReferences) && ! $messagingConfiguration->isRunningForEnterpriseLicence()) {
throw LicensingException::create('Custom #[StateStorage] implementations require Ecotone Enterprise licence.');
}
$stateStorageReferences = ExtensionObjectResolver::resolve(
ProjectionStateStorageReference::class,
$extensionObjects
);
$userlandStorages = array_map(
fn (string $reference) => new Reference($reference),
$this->userlandStateStorageReferences
);
$builtinStorages = array_map(
fn (ProjectionStateStorageReference $ref) => new Reference($ref->getReferenceName()),
$stateStorageReferences
);
$messagingConfiguration->registerServiceDefinition(
InMemoryProjectionStateStorage::class,
new Definition(InMemoryProjectionStateStorage::class, [null])
);
$messagingConfiguration->registerServiceDefinition(
ProjectionStateStorageRegistry::class,
new Definition(ProjectionStateStorageRegistry::class, [
$userlandStorages,
$builtinStorages,
])
);
}
public function canHandle($extensionObject): bool
{
return $extensionObject instanceof ProjectionStateStorageReference;
}
public function getModulePackageName(): string
{
return ModulePackageList::CORE_PACKAGE;
}
}