-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDbalConnectionFactory.php
More file actions
168 lines (147 loc) · 5.48 KB
/
DbalConnectionFactory.php
File metadata and controls
168 lines (147 loc) · 5.48 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
namespace Enqueue\Dbal;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
use Enqueue\Dsn\Dsn;
use Interop\Queue\ConnectionFactory;
use Interop\Queue\Context;
class DbalConnectionFactory implements ConnectionFactory
{
/**
* @var array
*/
private $config;
/**
* @var Connection
*/
private $connection;
/**
* The config could be an array, string DSN or null. In case of null it will attempt to connect to mysql localhost with default credentials.
*
* $config = [
* 'connection' => [] - dbal connection options. see http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
* 'table_name' => 'enqueue', - database table name.
* 'polling_interval' => '1000', - How often query for new messages (milliseconds)
* 'lazy' => true, - Use lazy database connection (boolean)
* ]
*
* or
*
* mysql://user:pass@localhost:3606/db?charset=UTF-8
*
* @param array|string|null $config
*/
public function __construct($config = 'mysql:')
{
if (empty($config)) {
$config = $this->parseDsn('mysql:');
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
if (array_key_exists('dsn', $config)) {
$config = array_replace_recursive($config, $this->parseDsn($config['dsn'], $config));
unset($config['dsn']);
}
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$this->config = array_replace_recursive([
'connection' => [],
'table_name' => 'enqueue',
'polling_interval' => 1000,
'lazy' => true,
], $config);
}
/**
* @return DbalContext
*/
public function createContext(): Context
{
if ($this->config['lazy']) {
return new DbalContext(function () {
return $this->establishConnection();
}, $this->config);
}
return new DbalContext($this->establishConnection(), $this->config);
}
public function close(): void
{
if ($this->connection) {
$this->connection->close();
}
}
private function establishConnection(): Connection
{
if (false == $this->connection) {
$this->connection = DriverManager::getConnection($this->config['connection']);
if (
method_exists($this->connection, 'connect')
&& (new \ReflectionMethod($this->connection, 'connect'))->isPublic()
) {
// TODO: remove check when dropping support for DBAL < 4
// DBAL < 4
$this->connection->connect();
} else {
// DBAL >= 4
$this->connection->getServerVersion(); // calls connect() internally
}
}
return $this->connection;
}
private function parseDsn(string $dsn, ?array $config = null): array
{
$parsedDsn = Dsn::parseFirst($dsn);
// enqueue scheme => dbal scheme
// out of the box dbal 4 schemes: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, oci8, ibm_db2, pdo_sqlsrv, mysqli, pgsql, sqlsrv, sqlite3
// in case of multiple drivers like mysqli vs pdo_mysql the pdo variant is preferred
$supported = [
'db2' => 'ibm_db2',
'ibm-db2' => 'ibm_db2',
'mssql' => 'pdo_sqlsrv',
'sqlsrv+pdo' => 'pdo_sqlsrv',
'mysql' => 'pdo_mysql',
'mysql2' => 'mysqli',
'mysql+pdo' => 'pdo_mysql',
'pgsql' => 'pgsql',
'postgres' => 'pdo_pgsql',
'pgsql+pdo' => 'pdo_pgsql',
'sqlite' => 'sqlite3',
'sqlite3' => 'sqlite3',
'sqlite+pdo' => 'pdo_sqlite',
];
if ($parsedDsn && false == isset($supported[$parsedDsn->getScheme()])) {
throw new \LogicException(sprintf('The given DSN schema "%s" is not supported. There are supported schemes: "%s".', $parsedDsn->getScheme(), implode('", "', array_keys($supported))));
}
$doctrineScheme = $supported[$parsedDsn->getScheme()];
$dsnHasProtocolOnly = $parsedDsn->getScheme().':' === $dsn;
if ($dsnHasProtocolOnly && is_array($config) && array_key_exists('connection', $config)) {
$default = [
'driver' => $doctrineScheme,
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => '',
];
return [
'lazy' => true,
'connection' => array_replace_recursive($default, $config['connection']),
];
}
if ($dsnHasProtocolOnly) {
$dsn = $parsedDsn->getScheme().'://root@localhost';
}
$dsnParser = new DsnParser($supported);
// replace scheme with the matching one from the supported list, but the prefixes there are "pdo-" not "pdo_"
$dsn = preg_replace(
'/^'.preg_quote($parsedDsn->getScheme(), '/').'(:|:\/\/)/',
str_replace('_', '-', $supported[$parsedDsn->getScheme()]).'$1',
$dsn,
);
return [
'lazy' => true,
'connection' => $dsnParser->parse($dsn),
];
}
}