forked from doctrine/DoctrineBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionFactory.php
173 lines (149 loc) · 6.09 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace Doctrine\Bundle\DoctrineBundle;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use const E_USER_DEPRECATED;
use function get_class;
use function trigger_error;
class ConnectionFactory
{
/** @var mixed[][] */
private $typesConfig = [];
/** @var bool */
private $initialized = false;
/**
* @param mixed[][] $typesConfig
*/
public function __construct(array $typesConfig)
{
$this->typesConfig = $typesConfig;
}
/**
* Create a connection by name.
*
* @param mixed[] $params
* @param string[]|Type[] $mappingTypes
*
* @return Connection
*/
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = [])
{
if (! $this->initialized) {
$this->initializeTypes();
}
$connection = DriverManager::getConnection($params, $config, $eventManager);
if (! empty($mappingTypes)) {
$platform = $this->getDatabasePlatform($connection);
foreach ($mappingTypes as $dbType => $doctrineType) {
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
}
}
if (! empty($this->typesConfig)) {
$this->markTypesCommented($this->getDatabasePlatform($connection));
}
return $connection;
}
/**
* Try to get the database platform.
*
* This could fail if types should be registered to an predefined/unused connection
* and the platform version is unknown.
* For details have a look at DoctrineBundle issue #673.
*
* @return AbstractPlatform
*
* @throws DBALException
*/
private function getDatabasePlatform(Connection $connection)
{
try {
return $connection->getDatabasePlatform();
} catch (DriverException $driverException) {
throw new DBALException(
'An exception occured while establishing a connection to figure out your platform version.' . PHP_EOL .
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
'For further information have a look at:' . PHP_EOL .
'https://github.com/doctrine/DoctrineBundle/issues/673',
0,
$driverException
);
}
}
/**
* initialize the types
*/
private function initializeTypes()
{
foreach ($this->typesConfig as $typeName => $typeConfig) {
if (Type::hasType($typeName)) {
Type::overrideType($typeName, $typeConfig['class']);
} else {
Type::addType($typeName, $typeConfig['class']);
}
}
$this->initialized = true;
}
private function markTypesCommented(AbstractPlatform $platform) : void
{
foreach ($this->typesConfig as $typeName => $typeConfig) {
$type = Type::getType($typeName);
$requiresSQLCommentHint = $type->requiresSQLCommentHint($platform);
// Attribute is missing, make sure a type that doesn't require a comment is marked as commented
// This is deprecated behaviour that will be dropped in 2.0.
if ($typeConfig['commented'] === null) {
if (! $requiresSQLCommentHint) {
@trigger_error(
sprintf(
'The type "%s" was implicitly marked as commented due to the configuration. This is deprecated and will be removed in DoctrineBundle 2.0. Either set the "commented" attribute in the configuration to "false" or mark the type as commented in "%s::requiresSQLCommentHint()."',
$typeName,
get_class($type)
),
E_USER_DEPRECATED
);
$platform->markDoctrineTypeCommented($type);
}
continue;
}
// The following logic generates appropriate deprecation notices telling the user how to update their type configuration.
if ($typeConfig['commented']) {
if (! $requiresSQLCommentHint) {
@trigger_error(
sprintf(
'The type "%s" was marked as commented in its configuration but not in the type itself. This is deprecated and will be removed in DoctrineBundle 2.0. Please update the return value of "%s::requiresSQLCommentHint()."',
$typeName,
get_class($type)
),
E_USER_DEPRECATED
);
$platform->markDoctrineTypeCommented($type);
continue;
}
@trigger_error(
sprintf(
'The type "%s" was explicitly marked as commented in its configuration. This is no longer necessary and will be removed in DoctrineBundle 2.0. Please remove the "commented" attribute from the type configuration.',
$typeName
),
E_USER_DEPRECATED
);
continue;
}
if (! $requiresSQLCommentHint) {
continue;
}
@trigger_error(
sprintf(
'The type "%s" was marked as uncommented in its configuration but commented in the type itself. This is deprecated and will be removed in DoctrineBundle 2.0. Please update the return value of "%s::requiresSQLCommentHint()" or remove the "commented" attribute from the type configuration.',
$typeName,
get_class($type)
),
E_USER_DEPRECATED
);
}
}
}