Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c977887

Browse files
committedFeb 18, 2021
add ability to override url
1 parent 91f0139 commit c977887

14 files changed

+118
-11
lines changed
 

‎ConnectionFactory.php

+42-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ public function createConnection(array $params, Configuration $config = null, Ev
4545
$this->initializeTypes();
4646
}
4747

48-
if (! isset($params['pdo']) && ! isset($params['charset'])) {
49-
$wrapperClass = null;
48+
$allowOverrideUrl = $params['override_url'] ?? false;
49+
unset($params['override_url']);
50+
51+
if (! $allowOverrideUrl) {
52+
$this->triggerUrlOverrideDeprecation();
53+
}
54+
55+
if (! isset($params['pdo']) && (! isset($params['charset']) || $allowOverrideUrl)) {
56+
$originalParams = $params;
57+
$wrapperClass = null;
58+
5059
if (isset($params['wrapperClass'])) {
5160
if (! is_subclass_of($params['wrapperClass'], Connection::class)) {
5261
if (class_exists(DBALException::class)) {
@@ -64,6 +73,24 @@ public function createConnection(array $params, Configuration $config = null, Ev
6473
$params = $connection->getParams();
6574
$driver = $connection->getDriver();
6675

76+
if ($allowOverrideUrl) {
77+
$supportedOverrideParams = [
78+
'dbname',
79+
'host',
80+
'port',
81+
'user',
82+
'password',
83+
];
84+
85+
foreach ($supportedOverrideParams as $paramKey) {
86+
if (! isset($originalParams[$paramKey])) {
87+
continue;
88+
}
89+
90+
$params[$paramKey] = $originalParams[$paramKey];
91+
}
92+
}
93+
6794
if ($driver instanceof AbstractMySQLDriver) {
6895
$params['charset'] = 'utf8mb4';
6996

@@ -138,4 +165,17 @@ private function initializeTypes(): void
138165

139166
$this->initialized = true;
140167
}
168+
169+
private function triggerUrlOverrideDeprecation(): void
170+
{
171+
$deprecationMessage = 'Setting doctrine.dbal.override_url to true allows you to override the dsn parameters set in doctrine.dbal.url. This will be the default behavior in future versions.';
172+
173+
if (function_exists('trigger_deprecation')) {
174+
trigger_deprecation('doctrine/DoctrineBundle', '2.3.0', $deprecationMessage);
175+
176+
return;
177+
}
178+
179+
@trigger_error($deprecationMessage, E_USER_DEPRECATED);
180+
}
141181
}

‎DependencyInjection/Configuration.php

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ private function configureDbalDriverNode(ArrayNodeDefinition $node): void
221221
->scalarNode('port')->defaultNull()->end()
222222
->scalarNode('user')->defaultValue('root')->end()
223223
->scalarNode('password')->defaultNull()->end()
224+
->booleanNode('override_url')->end()
224225
->scalarNode('application_name')->end()
225226
->scalarNode('charset')->end()
226227
->scalarNode('path')->end()

‎Resources/config/schema/doctrine-1.0.xsd

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<xsd:attribute name="port" type="xsd:string" />
5252
<xsd:attribute name="user" type="xsd:string" />
5353
<xsd:attribute name="password" type="xsd:string" />
54+
<xsd:attribute name="override-url" type="xsd:boolean" />
5455
<xsd:attribute name="application-name" type="xsd:string" />
5556
<xsd:attribute name="path" type="xsd:string" />
5657
<xsd:attribute name="unix-socket" type="xsd:string" />

‎Tests/ConnectionFactoryTest.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public function testContainer(): void
1818
{
1919
$typesConfig = [];
2020
$factory = new ConnectionFactory($typesConfig);
21-
$params = ['driverClass' => FakeDriver::class];
21+
$params = [
22+
'driverClass' => FakeDriver::class,
23+
'override_url' => true,
24+
];
2225
$config = null;
2326
$eventManager = null;
2427
$mappingTypes = [0];
@@ -52,6 +55,7 @@ public function testDefaultCharset(): void
5255
$params = [
5356
'driverClass' => FakeDriver::class,
5457
'wrapperClass' => FakeConnection::class,
58+
'override_url' => true,
5559
];
5660

5761
$creationCount = FakeConnection::$creationCount;
@@ -65,12 +69,39 @@ public function testDefaultCharset(): void
6569
public function testDefaultCharsetMySql(): void
6670
{
6771
$factory = new ConnectionFactory([]);
68-
$params = ['driver' => 'pdo_mysql'];
72+
$params = [
73+
'driver' => 'pdo_mysql',
74+
'override_url' => true,
75+
];
6976

7077
$connection = $factory->createConnection($params);
7178

7279
$this->assertSame('utf8mb4', $connection->getParams()['charset']);
7380
}
81+
82+
public function testUrlOverride(): void
83+
{
84+
$factory = new ConnectionFactory([]);
85+
$url = 'mysql://root:password@database:3306/main?serverVersion=mariadb-10.5.8';
86+
87+
$params = [
88+
'dbname' => 'main_test',
89+
'host' => 'db_test',
90+
'port' => 5432,
91+
'user' => 'tester',
92+
'password' => 'wordpass',
93+
];
94+
95+
$connection = $factory->createConnection(
96+
array_merge(['url' => $url, 'override_url' => true], $params)
97+
);
98+
99+
$result = $connection->getParams();
100+
101+
foreach ($params as $paramKey => $expectedValue) {
102+
self::assertSame($expectedValue, $result[$paramKey]);
103+
}
104+
}
74105
}
75106

76107
/**

‎Tests/DependencyInjection/AbstractDoctrineExtensionTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ public function testDbalLoadFromXmlSingleConnections(): void
113113
$this->assertEquals('5.6.20', $config['serverVersion']);
114114
}
115115

116+
public function testDbalLoadUrlOverride(): void
117+
{
118+
$container = $this->loadContainer('dbal_allow_url_override');
119+
120+
// doctrine.dbal.mysql_connection
121+
$config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
122+
123+
$this->assertTrue($config['override_url']);
124+
}
125+
116126
public function testDbalLoadSingleMasterSlaveConnection(): void
117127
{
118128
$container = $this->loadContainer('dbal_service_single_master_slave_connection');

‎Tests/DependencyInjection/Fixtures/TestKernel.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
3333
$container->loadFromExtension('framework', ['secret' => 'F00']);
3434

3535
$container->loadFromExtension('doctrine', [
36-
'dbal' => ['driver' => 'pdo_sqlite'],
36+
'dbal' => [
37+
'driver' => 'pdo_sqlite',
38+
'override_url' => true,
39+
],
3740
'orm' => [
3841
'auto_generate_proxy_classes' => true,
3942
'mappings' => [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
3+
<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:srv="http://symfony.com/schema/dic/services"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
8+
9+
<config>
10+
<dbal override-url="true" />
11+
</config>
12+
</srv:container>

‎Tests/DependencyInjection/Fixtures/config/xml/dbal_schema_filter.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
<config>
1010
<dbal default-connection="connection1">
11-
<connection name="connection1" schema-filter="~^(?!t_)~" />
12-
<connection name="connection2" />
13-
<connection name="connection3" />
11+
<connection name="connection1" schema-filter="~^(?!t_)~" override-url="true" />
12+
<connection name="connection2" override-url="true" />
13+
<connection name="connection3" override-url="true" />
1414
</dbal>
1515
</config>
1616
</srv:container>

‎Tests/DependencyInjection/Fixtures/config/xml/orm_filters.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<config>
1010
<dbal default-connection="default">
11-
<connection name="default" dbname="db" />
11+
<connection name="default" dbname="db" override-url="true" />
1212
</dbal>
1313

1414
<orm>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
doctrine:
2+
dbal:
3+
override_url: true

‎Tests/DependencyInjection/Fixtures/config/yml/dbal_schema_filter.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ doctrine:
33
default_connection: connection1
44
connections:
55
connection1:
6+
override_url: true
67
schema_filter: ~^(?!t_)~
7-
connection2: []
8-
connection3: []
8+
connection2:
9+
override_url: true
10+
connection3:
11+
override_url: true

‎Tests/DependencyInjection/Fixtures/config/yml/orm_filters.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ doctrine:
33
default_connection: default
44
connections:
55
default:
6+
override_url: true
67
dbname: db
78

89
orm:

‎Tests/ServiceRepositoryTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function testRepositoryServiceWiring(): void
6666
'dbal' => [
6767
'driver' => 'pdo_sqlite',
6868
'charset' => 'UTF8',
69+
'override_url' => true,
6970
],
7071
'orm' => [
7172
'mappings' => [

‎Tests/TestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function createXmlBundleTestContainer(): ContainerBuilder
3838
'driver' => 'pdo_mysql',
3939
'charset' => 'UTF8',
4040
'platform-service' => 'my.platform',
41+
'override_url' => true,
4142
],
4243
],
4344
'default_connection' => 'default',

0 commit comments

Comments
 (0)
Please sign in to comment.