forked from doctrine/DoctrineBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionFactory.php
89 lines (79 loc) · 2.38 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
<?php
/*
* This file is part of the Doctrine Bundle
*
* The code was originally distributed inside the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
* (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Doctrine\Bundle\DoctrineBundle;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Type;
/**
* Connection
*/
class ConnectionFactory
{
private $typesConfig = array();
private $commentedTypes = array();
private $initialized = false;
/**
* Construct.
*
* @param array $typesConfig
*/
public function __construct(array $typesConfig)
{
$this->typesConfig = $typesConfig;
}
/**
* Create a connection by name.
*
* @param array $params
* @param Configuration $config
* @param EventManager $eventManager
* @param array $mappingTypes
*
* @return \Doctrine\DBAL\Connection
*/
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
{
if (!$this->initialized) {
$this->initializeTypes();
$this->initialized = true;
}
$connection = DriverManager::getConnection($params, $config, $eventManager);
if (!empty($mappingTypes)) {
$platform = $connection->getDatabasePlatform();
foreach ($mappingTypes as $dbType => $doctrineType) {
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
}
foreach ($this->commentedTypes as $type) {
$platform->markDoctrineTypeCommented(Type::getType($type));
}
}
return $connection;
}
/**
* initialize the types
*/
private function initializeTypes()
{
foreach ($this->typesConfig as $type => $typeConfig) {
if (Type::hasType($type)) {
Type::overrideType($type, $typeConfig['class']);
} else {
Type::addType($type, $typeConfig['class']);
}
if ($typeConfig['commented']) {
$this->commentedTypes[] = $type;
}
}
}
}