Skip to content

Commit e9a0e25

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.
1 parent 7794f6d commit e9a0e25

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

Diff for: pkg/dbal/DbalConnectionFactory.php

+21-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,12 @@ 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+
* @return array
98+
*/
99+
private function parseDsn(string $dsn, array $config = null): array
96100
{
97101
if (false === strpos($dsn, ':')) {
98102
throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
@@ -135,6 +139,20 @@ private function parseDsn(string $dsn): array
135139

136140
$doctrineScheme = $supported[$scheme];
137141

142+
if (is_array($config) && key_exists('connection', $config)) {
143+
$default = [
144+
'driver' => $doctrineScheme,
145+
'host' => 'localhost',
146+
'port' => '3306',
147+
'user' => 'root',
148+
'password' => '',
149+
];
150+
return [
151+
'lazy' => true,
152+
'connection' => array_replace_recursive($default, $config['connection']),
153+
];
154+
}
155+
138156
return [
139157
'lazy' => true,
140158
'connection' => [

0 commit comments

Comments
 (0)