From 0ac6218edc80e912830e966b16d515e8f4bda7d2 Mon Sep 17 00:00:00 2001 From: Lars Greiss Date: Thu, 14 Feb 2019 09:53:15 +0100 Subject: [PATCH] [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 PS6: - better naming for dbname in tests --- pkg/dbal/DbalConnectionFactory.php | 26 +++++++- .../Tests/DbalConnectionFactoryConfigTest.php | 66 +++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/pkg/dbal/DbalConnectionFactory.php b/pkg/dbal/DbalConnectionFactory.php index 7e0f65c45..077bd0e3c 100644 --- a/pkg/dbal/DbalConnectionFactory.php +++ b/pkg/dbal/DbalConnectionFactory.php @@ -45,8 +45,7 @@ public function __construct($config = 'mysql:') $config = $this->parseDsn($config); } elseif (is_array($config)) { if (array_key_exists('dsn', $config)) { - $config = array_replace_recursive($config, $this->parseDsn($config['dsn'])); - + $config = array_replace_recursive($config, $this->parseDsn($config['dsn'], $config)); unset($config['dsn']); } } else { @@ -92,7 +91,13 @@ private function establishConnection(): Connection return $this->connection; } - private function parseDsn(string $dsn): array + /** + * @param string $dsn + * @param array|null $config + * + * @return array + */ + private function parseDsn(string $dsn, array $config = null): array { if (false === strpos($dsn, ':')) { throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".')); @@ -135,6 +140,21 @@ private function parseDsn(string $dsn): array $doctrineScheme = $supported[$scheme]; + if ($scheme.':' === $dsn && 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']), + ]; + } + return [ 'lazy' => true, 'connection' => [ diff --git a/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php b/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php index 1b9d8634c..fb5cae5d5 100644 --- a/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php +++ b/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php @@ -101,6 +101,72 @@ public static function provideConfigs() ], ]; + yield [ + [ + 'dsn' => 'mysql+pdo:', + 'connection' => [ + 'dbname' => 'customDbName', + ], + ], + [ + 'connection' => [ + 'dbname' => 'customDbName', + 'driver' => 'pdo_mysql', + 'host' => 'localhost', + 'port' => '3306', + 'user' => 'root', + 'password' => '', + ], + 'table_name' => 'enqueue', + 'polling_interval' => 1000, + 'lazy' => true, + ], + ]; + + yield [ + [ + 'dsn' => 'mysql+pdo:', + 'connection' => [ + 'dbname' => 'customDbName', + 'host' => 'host', + 'port' => '10000', + 'user' => 'user', + 'password' => 'pass', + ], + ], + [ + 'connection' => [ + 'dbname' => 'customDbName', + 'host' => 'host', + 'port' => '10000', + 'user' => 'user', + 'password' => 'pass', + 'driver' => 'pdo_mysql', + ], + 'table_name' => 'enqueue', + 'polling_interval' => 1000, + 'lazy' => true, + ], + ]; + + yield [ + [ + 'dsn' => 'mysql+pdo://user:pass@host:10000/db', + 'connection' => [ + 'foo' => 'fooValue', + ], + ], + [ + 'connection' => [ + 'foo' => 'fooValue', + 'url' => 'pdo_mysql://user:pass@host:10000/db', + ], + 'table_name' => 'enqueue', + 'polling_interval' => 1000, + 'lazy' => true, + ], + ]; + yield [ 'mysql://user:pass@host:10000/db', [