1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection\Factory; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
6
|
|
|
use Bdf\Prime\Connection\SimpleConnection; |
7
|
|
|
use Bdf\Prime\Exception\DBALException; |
8
|
|
|
use Bdf\Prime\Exception\PrimeException; |
9
|
|
|
use Doctrine\Common\EventManager; |
10
|
|
|
use Doctrine\DBAL\Configuration; |
11
|
|
|
use Doctrine\DBAL\DBALException as DoctrineDBALException; |
12
|
|
|
use Doctrine\DBAL\DriverManager; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ConnectionFactory |
16
|
|
|
* |
17
|
|
|
* Create simple connection instance |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
class ConnectionFactory implements ConnectionFactoryInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The drivers map |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
static private $driversMap; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* {@inheritDoc} |
30
|
|
|
*/ |
31
|
149 |
|
public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface |
32
|
|
|
{ |
33
|
149 |
|
$connection = $this->createConnection($parameters, $config); |
34
|
|
|
|
35
|
|
|
// Store connection and return adapter instance |
36
|
149 |
|
if ($connection instanceof ConnectionInterface) { |
|
|
|
|
37
|
149 |
|
$connection->setName($connectionName); |
38
|
|
|
} |
39
|
|
|
|
40
|
149 |
|
return $connection; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
55 |
|
public function support(string $connectionName, array $parameters): bool |
47
|
|
|
{ |
48
|
55 |
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create the instance of the connection |
53
|
|
|
* |
54
|
|
|
* @param array $parameters |
55
|
|
|
* @param Configuration|null $config |
56
|
|
|
* @param EventManager|null $eventManager The event manager, optional. |
57
|
|
|
* |
58
|
|
|
* @return ConnectionInterface |
59
|
|
|
* @throws DBALException |
|
|
|
|
60
|
|
|
*/ |
61
|
149 |
|
private function createConnection(array $parameters, Configuration $config = null, EventManager $eventManager = null): ConnectionInterface |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
// Set the custom driver class + wrapper |
64
|
149 |
|
if (isset($parameters['driver']) && isset(self::$driversMap[$parameters['driver']])) { |
65
|
|
|
list($parameters['driverClass'], $parameters['wrapperClass']) = self::$driversMap[$parameters['driver']]; |
66
|
|
|
unset($parameters['driver']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Replace 'adapter' with 'driver' and add 'pdo_' |
70
|
149 |
|
if (isset($parameters['adapter'])) { |
71
|
119 |
|
$parameters['driver'] = 'pdo_' . $parameters['adapter']; |
72
|
119 |
|
unset($parameters['adapter']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// default charset |
|
|
|
|
76
|
149 |
|
if (!isset($parameters['charset'])) { |
77
|
146 |
|
$parameters['charset'] = 'utf8'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// default wrapper |
|
|
|
|
81
|
149 |
|
if (!isset($parameters['wrapperClass'])) { |
82
|
149 |
|
$parameters['wrapperClass'] = SimpleConnection::class; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
try { |
86
|
149 |
|
return DriverManager::getConnection($parameters, $config, $eventManager); |
|
|
|
|
87
|
|
|
} catch (DoctrineDBALException $e) { |
88
|
|
|
throw new DBALException('Cannot create the connection : '.$e->getMessage(), $e->getCode(), $e); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Register a global driver map |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
* @param string $driver |
97
|
|
|
* @param string|null $wrapper |
98
|
|
|
*/ |
99
|
1 |
|
public static function registerDriverMap($name, $driver, $wrapper = null) |
100
|
|
|
{ |
101
|
1 |
|
self::$driversMap[$name] = [$driver, $wrapper]; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get a global driver map |
106
|
|
|
* |
107
|
|
|
* @param string $name |
108
|
|
|
* |
109
|
|
|
* @return string|null |
110
|
|
|
*/ |
111
|
1 |
|
public static function getDriverMap($name) |
112
|
|
|
{ |
113
|
1 |
|
return isset(self::$driversMap[$name]) |
114
|
1 |
|
? self::$driversMap[$name] |
|
|
|
|
115
|
1 |
|
: null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Unregister a global driver map |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
*/ |
123
|
1 |
|
public static function unregisterDriverMap($name) |
124
|
|
|
{ |
125
|
1 |
|
unset(self::$driversMap[$name]); |
126
|
1 |
|
} |
127
|
|
|
} |
128
|
|
|
|