Skip to content

Commit 9990b73

Browse files
committed
[dbal]: make dbal connection config usable again
right now user cannot declare connection options. Reason is that always 'connection' => 'url' was set which dbal always prefers over other connection settings. With this fix connection can be defined dbal compliant like this: connection: host: 'localhost' port: '3306' dbname: 'app' user: 'app' password: '' 'dsn only' works like before. PS3-4: - added config tests PS5: - if a fq dsn and connection is set dsn should be prefered
1 parent 7794f6d commit 9990b73

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

Diff for: pkg/dbal/DbalConnectionFactory.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public function __construct($config = 'mysql:')
4545
$config = $this->parseDsn($config);
4646
} elseif (is_array($config)) {
4747
if (array_key_exists('dsn', $config)) {
48-
$config = array_replace_recursive($config, $this->parseDsn($config['dsn']));
49-
48+
$config = array_replace_recursive($config, $this->parseDsn($config['dsn'], $config));
5049
unset($config['dsn']);
5150
}
5251
} else {
@@ -92,7 +91,13 @@ private function establishConnection(): Connection
9291
return $this->connection;
9392
}
9493

95-
private function parseDsn(string $dsn): array
94+
/**
95+
* @param string $dsn
96+
* @param array|null $config
97+
*
98+
* @return array
99+
*/
100+
private function parseDsn(string $dsn, array $config = null): array
96101
{
97102
if (false === strpos($dsn, ':')) {
98103
throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
@@ -135,6 +140,21 @@ private function parseDsn(string $dsn): array
135140

136141
$doctrineScheme = $supported[$scheme];
137142

143+
if ($scheme.':' === $dsn && is_array($config) && key_exists('connection', $config)) {
144+
$default = [
145+
'driver' => $doctrineScheme,
146+
'host' => 'localhost',
147+
'port' => '3306',
148+
'user' => 'root',
149+
'password' => '',
150+
];
151+
152+
return [
153+
'lazy' => true,
154+
'connection' => array_replace_recursive($default, $config['connection']),
155+
];
156+
}
157+
138158
return [
139159
'lazy' => true,
140160
'connection' => [

Diff for: pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php

+66
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,72 @@ public static function provideConfigs()
101101
],
102102
];
103103

104+
yield [
105+
[
106+
'dsn' => 'mysql+pdo:',
107+
'connection' => [
108+
'dbname' => 'dbname',
109+
],
110+
],
111+
[
112+
'connection' => [
113+
'dbname' => 'dbname',
114+
'driver' => 'pdo_mysql',
115+
'host' => 'localhost',
116+
'port' => '3306',
117+
'user' => 'root',
118+
'password' => '',
119+
],
120+
'table_name' => 'enqueue',
121+
'polling_interval' => 1000,
122+
'lazy' => true,
123+
],
124+
];
125+
126+
yield [
127+
[
128+
'dsn' => 'mysql+pdo:',
129+
'connection' => [
130+
'dbname' => 'dbname',
131+
'host' => 'host',
132+
'port' => '10000',
133+
'user' => 'user',
134+
'password' => 'pass',
135+
],
136+
],
137+
[
138+
'connection' => [
139+
'dbname' => 'dbname',
140+
'host' => 'host',
141+
'port' => '10000',
142+
'user' => 'user',
143+
'password' => 'pass',
144+
'driver' => 'pdo_mysql',
145+
],
146+
'table_name' => 'enqueue',
147+
'polling_interval' => 1000,
148+
'lazy' => true,
149+
],
150+
];
151+
152+
yield [
153+
[
154+
'dsn' => 'mysql+pdo://user:pass@host:10000/db',
155+
'connection' => [
156+
'foo' => 'fooValue',
157+
],
158+
],
159+
[
160+
'connection' => [
161+
'foo' => 'fooValue',
162+
'url' => 'pdo_mysql://user:pass@host:10000/db',
163+
],
164+
'table_name' => 'enqueue',
165+
'polling_interval' => 1000,
166+
'lazy' => true,
167+
],
168+
];
169+
104170
yield [
105171
'mysql://user:pass@host:10000/db',
106172
[

0 commit comments

Comments
 (0)