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', [