|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony framework. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* This source file is subject to the MIT license that is bundled |
|
|
9 |
* with this source code in the file LICENSE. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\DoctrineBundle; |
|
|
13 |
|
|
|
14 |
use Doctrine\Common\EventManager; |
|
|
15 |
use Doctrine\DBAL\Configuration; |
|
|
16 |
use Doctrine\DBAL\DriverManager; |
|
|
17 |
use Doctrine\DBAL\Types\Type; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* Connection |
|
|
21 |
*/ |
|
|
22 |
class ConnectionFactory |
|
|
23 |
{ |
|
|
24 |
private $typesConfig = array(); |
|
|
25 |
private $initialized = false; |
|
|
26 |
|
|
|
27 |
/** |
|
|
28 |
* Construct. |
|
|
29 |
* |
|
|
30 |
* @param array $typesConfig |
|
|
31 |
*/ |
|
|
32 |
public function __construct(array $typesConfig) |
|
|
33 |
{ |
|
|
34 |
$this->typesConfig = $typesConfig; |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
/** |
|
|
38 |
* Create a connection by name. |
|
|
39 |
* |
|
|
40 |
* @param array $params |
|
|
41 |
* @param Configuration $config |
|
|
42 |
* @param EventManager $eventManager |
|
|
43 |
* |
|
|
44 |
* @return Doctrine\DBAL\Connection |
|
|
45 |
*/ |
|
|
46 |
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array()) |
|
|
47 |
{ |
|
|
48 |
if (!$this->initialized) { |
|
|
49 |
$this->initializeTypes(); |
|
|
50 |
$this->initialized = true; |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
$connection = DriverManager::getConnection($params, $config, $eventManager); |
|
|
54 |
|
|
|
55 |
if (!empty($mappingTypes)) { |
|
|
56 |
$platform = $connection->getDatabasePlatform(); |
|
|
57 |
foreach ($mappingTypes as $dbType => $doctrineType) { |
|
|
58 |
$platform->registerDoctrineTypeMapping($dbType, $doctrineType); |
|
|
59 |
} |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
return $connection; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
private function initializeTypes() |
|
|
66 |
{ |
|
|
67 |
foreach ($this->typesConfig as $type => $className) { |
|
|
68 |
if (Type::hasType($type)) { |
|
|
69 |
Type::overrideType($type, $className); |
|
|
70 |
} else { |
|
|
71 |
Type::addType($type, $className); |
|
|
72 |
} |
|
|
73 |
} |
|
|
74 |
} |
|
|
75 |
} |