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