1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine Bundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project, Benjamin Eberlei <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Doctrine\Bundle\DoctrineBundle; |
16
|
|
|
|
17
|
|
|
use Doctrine\Common\EventManager; |
18
|
|
|
use Doctrine\DBAL\Configuration; |
19
|
|
|
use Doctrine\DBAL\Connection; |
20
|
|
|
use Doctrine\DBAL\DriverManager; |
21
|
|
|
use Doctrine\DBAL\Exception\DriverException; |
22
|
|
|
use Doctrine\DBAL\Types\Type; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Connection |
26
|
|
|
*/ |
27
|
|
|
class ConnectionFactory |
28
|
|
|
{ |
29
|
|
|
private $typesConfig = array(); |
30
|
|
|
private $commentedTypes = array(); |
31
|
|
|
private $initialized = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct. |
35
|
|
|
* |
36
|
|
|
* @param array $typesConfig |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $typesConfig) |
39
|
|
|
{ |
40
|
|
|
$this->typesConfig = $typesConfig; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a connection by name. |
45
|
|
|
* |
46
|
|
|
* @param array $params |
47
|
|
|
* @param Configuration $config |
|
|
|
|
48
|
|
|
* @param EventManager $eventManager |
|
|
|
|
49
|
|
|
* @param array $mappingTypes |
50
|
|
|
* |
51
|
|
|
* @return \Doctrine\DBAL\Connection |
52
|
|
|
*/ |
53
|
|
|
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array()) |
54
|
|
|
{ |
55
|
|
|
if (!$this->initialized) { |
56
|
|
|
$this->initializeTypes(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$connection = DriverManager::getConnection($params, $config, $eventManager); |
60
|
|
|
|
61
|
|
|
if (!empty($mappingTypes)) { |
62
|
|
|
$platform = $this->getDatabasePlatform($connection); |
63
|
|
|
foreach ($mappingTypes as $dbType => $doctrineType) { |
64
|
|
|
$platform->registerDoctrineTypeMapping($dbType, $doctrineType); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
if (!empty($this->commentedTypes)) { |
68
|
|
|
$platform = $this->getDatabasePlatform($connection); |
69
|
|
|
foreach ($this->commentedTypes as $type) { |
70
|
|
|
$platform->markDoctrineTypeCommented(Type::getType($type)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $connection; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Try to get the database platform. |
79
|
|
|
* |
80
|
|
|
* This could fail if types should be registered to an predefined/unused connection |
81
|
|
|
* and the platform version is unknown. |
82
|
|
|
* For details have a look at DoctrineBundle issue #673. |
83
|
|
|
* |
84
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
85
|
|
|
* |
86
|
|
|
* @return \Doctrine\DBAL\Platforms\AbstractPlatform |
87
|
|
|
* @throws \Doctrine\DBAL\Exception\DriverException |
88
|
|
|
*/ |
89
|
|
|
private function getDatabasePlatform(Connection $connection) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
return $connection->getDatabasePlatform(); |
93
|
|
|
} catch (DriverException $driverException) { |
94
|
|
|
throw new DriverException( |
95
|
|
|
"An exception occured while establishing a connection to figure out your platform version." . PHP_EOL . |
96
|
|
|
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL . |
97
|
|
|
"For further information have a look at:" . PHP_EOL . |
98
|
|
|
"https://github.com/doctrine/DoctrineBundle/issues/673", |
99
|
|
|
0, |
|
|
|
|
100
|
|
|
$driverException |
|
|
|
|
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* initialize the types |
107
|
|
|
*/ |
108
|
|
|
private function initializeTypes() |
109
|
|
|
{ |
110
|
|
|
foreach ($this->typesConfig as $type => $typeConfig) { |
111
|
|
|
if (Type::hasType($type)) { |
112
|
|
|
Type::overrideType($type, $typeConfig['class']); |
113
|
|
|
} else { |
114
|
|
|
Type::addType($type, $typeConfig['class']); |
115
|
|
|
} |
116
|
|
|
if ($typeConfig['commented']) { |
117
|
|
|
$this->commentedTypes[] = $type; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
$this->initialized = true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.