|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSB\Core\Persistence; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use PSB\Core\Exception\RuntimeException; |
|
6
|
|
|
use PSB\Core\Exception\UnexpectedValueException; |
|
7
|
|
|
use PSB\Core\KnownSettingsEnum; |
|
8
|
|
|
use PSB\Core\Util\Settings; |
|
9
|
|
|
|
|
10
|
|
|
class PersistenceDefinitionApplier |
|
11
|
|
|
{ |
|
12
|
3 |
|
public function apply(Settings $settings) |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var EnabledPersistence[]|null $enabledPersistences */ |
|
15
|
3 |
|
$enabledPersistences = $settings->tryGet(KnownSettingsEnum::ENABLED_PERSISTENCES); |
|
16
|
|
|
|
|
17
|
3 |
|
if (!$enabledPersistences) { |
|
18
|
|
|
// TODO tweak the error message (vezi interfetele de care vb in NServicebus) |
|
19
|
|
|
// TODO vezi si de SendOnly endpoint cand lamuresti cu sending messages din afara unui handler (fara tranzactie explicita) |
|
20
|
1 |
|
throw new UnexpectedValueException( |
|
21
|
1 |
|
"No persistence has been selected, please select your persistence by calling endpointConfigurator.usePersistence." |
|
22
|
|
|
); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
$enabledPersistences = array_reverse($enabledPersistences); |
|
26
|
2 |
|
$availableStorageTypeValues = array_flip(StorageType::getConstants()); |
|
27
|
|
|
|
|
28
|
2 |
|
$supportedStorageTypeValues = []; |
|
29
|
|
|
|
|
30
|
2 |
|
foreach ($enabledPersistences as $enabledPersistence) { |
|
31
|
2 |
|
$currentDefinition = $enabledPersistence->getDefinition(); |
|
32
|
2 |
|
$currentDefinition->formalize(); |
|
33
|
2 |
|
$selectedStorageType = $enabledPersistence->getSelectedStorageType(); |
|
34
|
|
|
|
|
35
|
2 |
|
if ($selectedStorageType && !$currentDefinition->hasSupportFor($selectedStorageType)) { |
|
36
|
1 |
|
$definitionFqcn = get_class($currentDefinition); |
|
37
|
1 |
|
throw new RuntimeException( |
|
38
|
1 |
|
"Definition '$definitionFqcn' does not support storage type {$selectedStorageType->getValue()}." |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$currentSupportedStorageTypeValues = $currentDefinition->getSupportedStorages($selectedStorageType); |
|
43
|
1 |
|
foreach ($currentSupportedStorageTypeValues as $supportedStorageValue) { |
|
44
|
1 |
|
if (isset($availableStorageTypeValues[$supportedStorageValue])) { |
|
45
|
1 |
|
unset($availableStorageTypeValues[$supportedStorageValue]); |
|
46
|
1 |
|
$supportedStorageTypeValues[$supportedStorageValue] = $supportedStorageValue; |
|
47
|
1 |
|
$currentDefinition->applyFor(new StorageType($supportedStorageValue), $settings); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$settings->set(KnownSettingsEnum::SUPPORTED_STORAGE_TYPE_VALUES, $supportedStorageTypeValues); |
|
53
|
1 |
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|