Skip to content

Commit 6250dff

Browse files
committed
feat: allow DsnParser to parse driverClass via $schemeMapping
1 parent e0870f1 commit 6250dff

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

docs/en/reference/configuration.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ name.
134134
135135
$conn = DriverManager::getConnection($connectionParams);
136136
137+
You can also use the mapping table to map a DSN's scheme to a custom driver
138+
class:
139+
140+
.. code-block:: php
141+
142+
<?php
143+
use Doctrine\DBAL\Tools\DsnParser;
144+
use App\DBAL\CustomDriver; // implements Doctrine\DBAL\Driver
145+
146+
//..
147+
$dsnParser = new DsnParser(['custom' => CustomDriver::class]);
148+
$connectionParams = $dsnParser
149+
->parse('custom://user:secret@localhost/mydb');
150+
137151
Driver
138152
~~~~~~
139153

src/Tools/DsnParser.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Doctrine\DBAL\Tools;
44

5+
use Doctrine\DBAL\Driver;
56
use Doctrine\DBAL\DriverManager;
67
use Doctrine\DBAL\Exception\MalformedDsnException;
78
use SensitiveParameter;
89

910
use function array_merge;
1011
use function assert;
12+
use function is_a;
1113
use function is_string;
1214
use function parse_str;
1315
use function parse_url;
@@ -20,10 +22,10 @@
2022
/** @psalm-import-type Params from DriverManager */
2123
final class DsnParser
2224
{
23-
/** @var array<string, string> */
25+
/** @var array<string, string|class-string<Driver>> */
2426
private array $schemeMapping;
2527

26-
/** @param array<string, string> $schemeMapping An array used to map DSN schemes to DBAL drivers */
28+
/** @param array<string, string|class-string<Driver>> $schemeMapping An array used to map DSN schemes to DBAL drivers */
2729
public function __construct(array $schemeMapping = [])
2830
{
2931
$this->schemeMapping = $schemeMapping;
@@ -78,6 +80,11 @@ public function parse(
7880
$params['password'] = $url['pass'];
7981
}
8082

83+
if (isset($params['driver']) && is_a($params['driver'], Driver::class, true)) {
84+
$params['driverClass'] = $params['driver'];
85+
unset($params['driver']);
86+
}
87+
8188
$params = $this->parseDatabaseUrlPath($url, $params);
8289
$params = $this->parseDatabaseUrlQuery($url, $params);
8390

tests/Tools/DsnParserTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Doctrine\DBAL\Tests\Tools;
44

5+
use Doctrine\DBAL\Driver;
56
use Doctrine\DBAL\DriverManager;
67
use Doctrine\DBAL\Tools\DsnParser;
78
use PHPUnit\Framework\TestCase;
89

10+
use function get_class;
911
use function ksort;
1012

1113
/** @psalm-import-type Params from DriverManager */
@@ -172,4 +174,22 @@ public function databaseUrls(): iterable
172174
],
173175
];
174176
}
177+
178+
public function testDriverClassScheme(): void
179+
{
180+
$driverClass = get_class($this->createMock(Driver::class));
181+
$parser = new DsnParser(['custom' => $driverClass]);
182+
$actual = $parser->parse('custom://foo:bar@localhost/baz');
183+
184+
self::assertSame(
185+
[
186+
'host' => 'localhost',
187+
'user' => 'foo',
188+
'password' => 'bar',
189+
'driverClass' => $driverClass,
190+
'dbname' => 'baz',
191+
],
192+
$actual,
193+
);
194+
}
175195
}

0 commit comments

Comments
 (0)