|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Doctrine\Factory; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
7
|
|
|
use Doctrine\DBAL\DriverManager; |
|
8
|
|
|
use Doctrine\ORM\Configuration; |
|
9
|
|
|
use Doctrine\ORM\EntityManager; |
|
10
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
11
|
|
|
use Interop\Container\ContainerInterface; |
|
12
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Exception\InvalidArgumentDoctrineConfigException; |
|
13
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Exception\MissingDoctrineConfigException; |
|
14
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\SimpleRegistry; |
|
15
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class ManagerRegistryFactory implements FactoryInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
22
|
|
|
*/ |
|
23
|
58 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ManagerRegistry |
|
24
|
|
|
{ |
|
25
|
58 |
|
$config = $container->has('config') ? $container->get('config') : []; |
|
26
|
|
|
|
|
27
|
58 |
|
$connections = $this->getConnections($config); |
|
28
|
|
|
|
|
29
|
58 |
|
if (!isset($config['doctrine'])) { |
|
30
|
|
|
throw new MissingDoctrineConfigException( |
|
31
|
|
|
'Doctrine configuration is missing' |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
58 |
|
$doctrineAutoloadConfig = $config['doctrine']; |
|
36
|
58 |
|
$emMapping = $doctrineAutoloadConfig['entity_manager_mapping'] ?? []; |
|
37
|
|
|
|
|
38
|
58 |
|
if (!isset($doctrineAutoloadConfig['entity_managers'])) { |
|
39
|
|
|
throw new MissingDoctrineConfigException( |
|
40
|
|
|
'Doctrine configuration is missing entity managers' |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
58 |
|
$entityManagersConfig = $doctrineAutoloadConfig['entity_managers']; |
|
45
|
|
|
|
|
46
|
58 |
|
$managers = []; |
|
47
|
58 |
|
foreach ($entityManagersConfig as $key => $emConfig) { |
|
48
|
58 |
|
$paths = $emConfig['paths'] ?? []; |
|
49
|
58 |
|
$devMode = $emConfig['dev_mode'] ?? false; |
|
50
|
|
|
|
|
51
|
|
|
// get "base" config |
|
52
|
58 |
|
$doctrineConfiguration = Setup::createAnnotationMetadataConfiguration( |
|
53
|
58 |
|
$paths, |
|
54
|
58 |
|
$devMode, |
|
55
|
58 |
|
null, |
|
56
|
58 |
|
null, |
|
57
|
58 |
|
false |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
58 |
|
$this->populateConfig($emConfig, $doctrineConfiguration, $container); |
|
61
|
|
|
|
|
62
|
58 |
|
$connectionName = $emConfig['connection'] ?? 'default'; |
|
63
|
|
|
|
|
64
|
58 |
|
if (!isset($connections[$connectionName])) { |
|
65
|
|
|
throw new InvalidArgumentDoctrineConfigException( |
|
66
|
|
|
sprintf('Can not find DB connection named %s', $connectionName) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// obtaining the entity manager |
|
71
|
58 |
|
$managers[$key] = EntityManager::create($connections[$connectionName], $doctrineConfiguration); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
58 |
|
return new SimpleRegistry($connections, $managers, $emMapping); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
58 |
|
private function populateConfig( |
|
78
|
|
|
array $configuration, |
|
79
|
|
|
Configuration $configurationObject, |
|
80
|
|
|
ContainerInterface $container |
|
81
|
|
|
): void { |
|
82
|
58 |
|
if (isset($configuration['naming_strategy'])) { |
|
83
|
58 |
|
$configurationObject->setNamingStrategy($container->get($configuration['naming_strategy'])); |
|
84
|
|
|
} |
|
85
|
58 |
|
if (isset($configuration['proxy_dir'])) { |
|
86
|
58 |
|
$configurationObject->setProxyDir($configuration['proxy_dir']); |
|
87
|
|
|
} |
|
88
|
58 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $config |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
58 |
|
private function getConnections(array $config): array |
|
95
|
|
|
{ |
|
96
|
58 |
|
if (!isset($config['connections'])) { |
|
97
|
|
|
throw new MissingDoctrineConfigException( |
|
98
|
|
|
'DB Connections configuration is missing' |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return array_map(function (array $connection) { |
|
103
|
58 |
|
return DriverManager::getConnection($connection); |
|
104
|
58 |
|
}, $config['connections']); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|