This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
ConnectionFactory.php
116 lines (101 loc) · 3.47 KB
/
ConnectionFactory.php
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
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* container-interop-doctrine
*
* @link http://github.com/DASPRiD/container-interop-doctrine For the canonical source repository
* @copyright 2016 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace ContainerInteropDoctrine;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOMySql\Driver as PdoMysqlDriver;
use Doctrine\DBAL\DriverManager;
use Psr\Container\ContainerInterface;
use Doctrine\DBAL\Types\Type;
/**
* @method Connection __invoke(ContainerInterface $container)
*/
class ConnectionFactory extends AbstractFactory
{
/**
* @var bool
*/
private static $areTypesRegistered = false;
/**
* {@inheritdoc}
*/
protected function createWithConfig(ContainerInterface $container, $configKey)
{
$this->registerTypes($container);
$config = $this->retrieveConfig($container, $configKey, 'connection');
$params = $config['params'] + [
'driverClass' => $config['driver_class'],
'wrapperClass' => $config['wrapper_class'],
'pdo' => is_string($config['pdo']) ? $container->get($config['pdo']) : $config['pdo'],
];
if (isset($params['platform'])) {
$params['platform'] = $container->get($params['platform']);
}
$connection = DriverManager::getConnection(
$params,
$this->retrieveDependency(
$container,
$config['configuration'],
'configuration',
ConfigurationFactory::class
),
$this->retrieveDependency(
$container,
$config['event_manager'],
'event_manager',
EventManagerFactory::class
)
);
$platform = $connection->getDatabasePlatform();
foreach ($config['doctrine_mapping_types'] as $dbType => $doctrineType) {
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
}
foreach ($config['doctrine_commented_types'] as $doctrineType) {
$platform->markDoctrineTypeCommented($doctrineType);
}
return $connection;
}
/**
* {@inheritdoc}
*/
protected function getDefaultConfig($configKey)
{
return [
'driver_class' => PdoMysqlDriver::class,
'wrapper_class' => null,
'pdo' => null,
'configuration' => $configKey,
'event_manager' => $configKey,
'params' => [],
'doctrine_mapping_types' => [],
'doctrine_commented_types' => [],
];
}
/**
* Registers all declared typed, if not already done.
*
* @param ContainerInterface $container
*/
private function registerTypes(ContainerInterface $container)
{
if (self::$areTypesRegistered) {
return;
}
$applicationConfig = $container->has('config') ? $container->get('config') : [];
$doctrineConfig = array_key_exists('doctrine', $applicationConfig) ? $applicationConfig['doctrine'] : [];
$typesConfig = array_key_exists('types', $doctrineConfig) ? $doctrineConfig['types'] : [];
self::$areTypesRegistered = true;
foreach ($typesConfig as $name => $className) {
if (Type::hasType($name)) {
Type::overrideType($name, $className);
continue;
}
Type::addType($name, $className);
}
}
}