1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/boot project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Boot\Service\Provisioner; |
10
|
|
|
|
11
|
|
|
use Auryn\Injector; |
12
|
|
|
use Daikon\Boot\Service\ServiceDefinitionInterface; |
13
|
|
|
use Daikon\Config\ConfigProviderInterface; |
14
|
|
|
use Daikon\Dbal\Connector\ConnectorMap; |
15
|
|
|
use Daikon\Dbal\Migration\MigrationAdapterMap; |
16
|
|
|
use Daikon\Dbal\Migration\MigrationLoaderMap; |
17
|
|
|
use Daikon\Dbal\Migration\MigrationTarget; |
18
|
|
|
use Daikon\Dbal\Migration\MigrationTargetMap; |
19
|
|
|
use Daikon\Interop\Assertion; |
20
|
|
|
|
21
|
|
|
final class MigrationTargetMapProvisioner implements ProvisionerInterface |
22
|
|
|
{ |
23
|
|
|
public function provision( |
24
|
|
|
Injector $injector, |
25
|
|
|
ConfigProviderInterface $configProvider, |
26
|
|
|
ServiceDefinitionInterface $serviceDefinition |
27
|
|
|
): void { |
28
|
|
|
$loaderConfigs = (array)$configProvider->get('migrations.migration_loaders', []); |
29
|
|
|
$adapterConfigs = (array)$configProvider->get('migrations.migration_adapters', []); |
30
|
|
|
$targetConfigs = (array)$configProvider->get('migrations.migration_targets', []); |
31
|
|
|
|
32
|
|
|
$this->delegateLoaderMap($injector, $loaderConfigs); |
33
|
|
|
$this->delegateAdapterMap($injector, $adapterConfigs); |
34
|
|
|
$this->delegateTargetMap($injector, $targetConfigs); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function delegateLoaderMap(Injector $injector, array $loaderConfigs): void |
38
|
|
|
{ |
39
|
|
|
$factory = function (ConnectorMap $connectorMap) use ( |
40
|
|
|
$injector, |
41
|
|
|
$loaderConfigs |
42
|
|
|
): MigrationLoaderMap { |
43
|
|
|
$loaders = []; |
44
|
|
|
foreach ($loaderConfigs as $loaderKey => $loaderConfig) { |
45
|
|
|
Assertion::keyNotExists($loaders, $loaderKey, "Migration loader '$loaderKey' is already defined."); |
46
|
|
|
$loaders[$loaderKey] = $injector->make( |
47
|
|
|
$loaderConfig['class'], |
48
|
|
|
[ |
49
|
|
|
':connector' => $connectorMap->get($loaderConfig['connector']), |
50
|
|
|
':settings' => $loaderConfig['settings'] ?? [] |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
return new MigrationLoaderMap($loaders); |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
$injector->delegate(MigrationLoaderMap::class, $factory)->share(MigrationLoaderMap::class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function delegateAdapterMap(Injector $injector, array $adapterConfigs): void |
61
|
|
|
{ |
62
|
|
|
$factory = function (ConnectorMap $connectorMap) use ($injector, $adapterConfigs): MigrationAdapterMap { |
63
|
|
|
$adapters = []; |
64
|
|
|
foreach ($adapterConfigs as $adapterKey => $adapterConfig) { |
65
|
|
|
Assertion::keyNotExists($adapters, $adapterKey, "Migration adapter '$adapterKey' is already defined."); |
66
|
|
|
$adapters[$adapterKey] = $injector->make( |
67
|
|
|
$adapterConfig['class'], |
68
|
|
|
[ |
69
|
|
|
':connector' => $connectorMap->get($adapterConfig['connector']), |
70
|
|
|
':settings' => $adapterConfig['settings'] ?? [] |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
return new MigrationAdapterMap($adapters); |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
$injector->delegate(MigrationAdapterMap::class, $factory)->share(MigrationAdapterMap::class); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function delegateTargetMap(Injector $injector, array $targetConfigs): void |
81
|
|
|
{ |
82
|
|
|
$factory = function ( |
83
|
|
|
MigrationAdapterMap $adapterMap, |
84
|
|
|
MigrationLoaderMap $loaderMap |
85
|
|
|
) use ( |
86
|
|
|
$injector, |
87
|
|
|
$targetConfigs |
88
|
|
|
): MigrationTargetMap { |
89
|
|
|
$targets = []; |
90
|
|
|
foreach ($targetConfigs as $targetKey => $targetConfig) { |
91
|
|
|
Assertion::keyNotExists($targets, $targetKey, "Migration target '$targetKey' is already defined."); |
92
|
|
|
$targets[$targetKey] = $injector->make( |
93
|
|
|
MigrationTarget::class, |
94
|
|
|
[ |
95
|
|
|
':key' => $targetKey, |
96
|
|
|
':enabled' => $targetConfig['enabled'], |
97
|
|
|
':migrationAdapter' => $adapterMap->get($targetConfig['migration_adapter']), |
98
|
|
|
':migrationLoader' => $loaderMap->get($targetConfig['migration_loader']) |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
return new MigrationTargetMap($targets); |
103
|
|
|
}; |
104
|
|
|
|
105
|
|
|
$injector->delegate(MigrationTargetMap::class, $factory)->share(MigrationTargetMap::class); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|