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\Storage\StorageAdapterMap; |
15
|
|
|
use Daikon\Interop\Assertion; |
16
|
|
|
|
17
|
|
|
final class RepositoryMapProvisioner implements ProvisionerInterface |
18
|
|
|
{ |
19
|
|
|
public function provision( |
20
|
|
|
Injector $injector, |
21
|
|
|
ConfigProviderInterface $configProvider, |
22
|
|
|
ServiceDefinitionInterface $serviceDefinition |
23
|
|
|
): void { |
24
|
|
|
$serviceClass = $serviceDefinition->getServiceClass(); |
25
|
|
|
$repositoryConfigs = (array)$configProvider->get('databases.repositories', []); |
26
|
|
|
|
27
|
|
|
$factory = function (StorageAdapterMap $storageAdapterMap) use ( |
28
|
|
|
$injector, |
29
|
|
|
$repositoryConfigs, |
30
|
|
|
$serviceClass |
31
|
|
|
): object { |
32
|
|
|
$repositories = []; |
33
|
|
|
foreach ($repositoryConfigs as $repositoryKey => $repositoryConfig) { |
34
|
|
|
Assertion::keyNotExists( |
35
|
|
|
$repositories, |
36
|
|
|
$repositoryKey, |
37
|
|
|
"Repository '$repositoryKey' is already defined." |
38
|
|
|
); |
39
|
|
|
$dependencies = [':storageAdapter' => $storageAdapterMap->get($repositoryConfig['storage_adapter'])]; |
40
|
|
|
if (isset($repositoryConfig['search_adapter'])) { |
41
|
|
|
$dependencies[':searchAdapter'] = $storageAdapterMap->get($repositoryConfig['search_adapter']); |
42
|
|
|
} |
43
|
|
|
$repositories[$repositoryKey] = $injector->make($repositoryConfig['class'], $dependencies); |
44
|
|
|
} |
45
|
|
|
return new $serviceClass($repositories); |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
$injector |
49
|
|
|
->share($serviceClass) |
50
|
|
|
->delegate($serviceClass, $factory); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|