Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 410d776

Browse files
committed
Doctrine DBAL DSNs support
1 parent 57d9c6e commit 410d776

File tree

10 files changed

+108
-33
lines changed

10 files changed

+108
-33
lines changed

pkg/dbal/DbalConnectionFactory.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,6 @@ private function parseDsn($dsn)
115115
'sqlite' => true,
116116
'sqlite3' => true,
117117
'pdo_sqlite' => true,
118-
'db2+doctrine' => true,
119-
'ibm_db2+doctrine' => true,
120-
'mssql+doctrine' => true,
121-
'pdo_sqlsrv+doctrine' => true,
122-
'mysql+doctrine' => true,
123-
'mysql2+doctrine' => true,
124-
'pdo_mysql+doctrine' => true,
125-
'pgsql+doctrine' => true,
126-
'postgres+doctrine' => true,
127-
'postgresql+doctrine' => true,
128-
'pdo_pgsql+doctrine' => true,
129-
'sqlite+doctrine' => true,
130-
'sqlite3+doctrine' => true,
131-
'pdo_sqlite+doctrine' => true,
132118
];
133119

134120
if (false == isset($supported[$schema])) {
@@ -139,14 +125,11 @@ private function parseDsn($dsn)
139125
));
140126
}
141127

142-
$doctrineSchema = str_replace('+doctrine', '', $schema);
143-
$doctrineUrl = empty($rest) ?
144-
$doctrineSchema.'://root@localhost' :
145-
str_replace($schema, $doctrineSchema, $dsn)
146-
;
147-
148-
return ['connection' => [
149-
'url' => $doctrineUrl,
150-
]];
128+
return [
129+
'lazy' => true,
130+
'connection' => [
131+
'url' => empty($rest) ? $schema.'://root@localhost' : $dsn,
132+
],
133+
];
151134
}
152135
}

pkg/dbal/Symfony/DbalTransportFactory.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ public function __construct($name = 'dbal')
3333
public function addConfiguration(ArrayNodeDefinition $builder)
3434
{
3535
$builder
36+
->beforeNormalization()
37+
->ifString()
38+
->then(function ($v) {
39+
return ['dsn' => $v];
40+
})
41+
->end()
3642
->children()
43+
->scalarNode('dsn')
44+
->info('The Doctrine DBAL DSN. Other parameters are ignored if set')
45+
->end()
3746
->variableNode('connection')
3847
->treatNullLike([])
3948
->info('Doctrine DBAL connection options. See http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html')
@@ -66,6 +75,9 @@ public function createConnectionFactory(ContainerBuilder $container, array $conf
6675
if (false == empty($config['dbal_connection_name'])) {
6776
$factory = new Definition(ManagerRegistryConnectionFactory::class);
6877
$factory->setArguments([new Reference('doctrine'), $config]);
78+
} elseif (false == empty($config['dsn'])) {
79+
$factory = new Definition(DbalConnectionFactory::class);
80+
$factory->setArguments([$config['dsn']]);
6981
} elseif (false == empty($config['connection'])) {
7082
$factory = new Definition(DbalConnectionFactory::class);
7183
$factory->setArguments([$config]);

pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static function provideConfigs()
5555
yield [
5656
null,
5757
[
58+
'lazy' => true,
5859
'connection' => [
5960
'url' => 'mysql://root@localhost',
6061
],
@@ -64,15 +65,7 @@ public static function provideConfigs()
6465
yield [
6566
'mysql://',
6667
[
67-
'connection' => [
68-
'url' => 'mysql://root@localhost',
69-
],
70-
],
71-
];
72-
73-
yield [
74-
'mysql+doctrine://',
75-
[
68+
'lazy' => true,
7669
'connection' => [
7770
'url' => 'mysql://root@localhost',
7871
],
@@ -82,6 +75,7 @@ public static function provideConfigs()
8275
yield [
8376
'pgsql://',
8477
[
78+
'lazy' => true,
8579
'connection' => [
8680
'url' => 'pgsql://root@localhost',
8781
],
@@ -91,6 +85,7 @@ public static function provideConfigs()
9185
yield [
9286
'mysql://user:pass@host:10000/db',
9387
[
88+
'lazy' => true,
9489
'connection' => [
9590
'url' => 'mysql://user:pass@host:10000/db',
9691
],
@@ -100,6 +95,7 @@ public static function provideConfigs()
10095
yield [
10196
[],
10297
[
98+
'lazy' => true,
10399
'connection' => [
104100
'url' => 'mysql://root@localhost',
105101
],

pkg/dbal/Tests/DbalConnectionFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testCouldBeConstructedWithEmptyConfiguration()
2323

2424
$this->assertAttributeEquals([
2525
'lazy' => true,
26-
'connection' => [],
26+
'connection' => ['url' => 'mysql://root@localhost'],
2727
], 'config', $factory);
2828
}
2929

pkg/dbal/Tests/Symfony/DbalTransportFactoryTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ public function testShouldAllowAddConfiguration()
6161
], $config);
6262
}
6363

64+
public function testShouldAllowAddConfigurationAsString()
65+
{
66+
$transport = new DbalTransportFactory();
67+
$tb = new TreeBuilder();
68+
$rootNode = $tb->root('foo');
69+
70+
$transport->addConfiguration($rootNode);
71+
$processor = new Processor();
72+
$config = $processor->process($tb->buildTree(), ['mysqlDSN']);
73+
74+
$this->assertEquals([
75+
'dsn' => 'mysqlDSN',
76+
'dbal_connection_name' => null,
77+
'table_name' => 'enqueue',
78+
'polling_interval' => 1000,
79+
'lazy' => true,
80+
], $config);
81+
}
82+
6483
public function testShouldCreateDbalConnectionFactory()
6584
{
6685
$container = new ContainerBuilder();
@@ -89,6 +108,26 @@ public function testShouldCreateDbalConnectionFactory()
89108
], $factory->getArgument(0));
90109
}
91110

111+
public function testShouldCreateConnectionFactoryFromDsnString()
112+
{
113+
$container = new ContainerBuilder();
114+
115+
$transport = new DbalTransportFactory();
116+
117+
$serviceId = $transport->createConnectionFactory($container, [
118+
'dsn' => 'theDSN',
119+
'connection' => [],
120+
'lazy' => true,
121+
'table_name' => 'enqueue',
122+
'polling_interval' => 1000,
123+
]);
124+
125+
$this->assertTrue($container->hasDefinition($serviceId));
126+
$factory = $container->getDefinition($serviceId);
127+
$this->assertEquals(DbalConnectionFactory::class, $factory->getClass());
128+
$this->assertSame('theDSN', $factory->getArgument(0));
129+
}
130+
92131
public function testShouldCreateManagerRegistryConnectionFactory()
93132
{
94133
$container = new ContainerBuilder();

pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public function provideEnqueueConfigs()
4949
],
5050
]];
5151

52+
yield 'default_dbal_as_dsn' => [[
53+
'transport' => [
54+
'default' => getenv('DOCTRINE_DSN'),
55+
],
56+
]];
57+
5258
yield 'stomp' => [[
5359
'transport' => [
5460
'default' => 'stomp',
@@ -123,6 +129,13 @@ public function provideEnqueueConfigs()
123129
],
124130
]];
125131

132+
yield 'dbal_dsn' => [[
133+
'transport' => [
134+
'default' => 'dbal',
135+
'dbal' => getenv('DOCTRINE_DSN'),
136+
],
137+
]];
138+
126139
yield 'sqs' => [[
127140
'transport' => [
128141
'default' => 'sqs',

pkg/enqueue/Symfony/DefaultTransportFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Enqueue\AmqpExt\AmqpConnectionFactory;
66
use Enqueue\AmqpExt\Symfony\AmqpTransportFactory;
7+
use Enqueue\Dbal\DbalConnectionFactory;
78
use Enqueue\Fs\FsConnectionFactory;
89
use Enqueue\Fs\Symfony\FsTransportFactory;
910
use Enqueue\Null\NullConnectionFactory;
@@ -142,6 +143,10 @@ private function findFactory($dsn)
142143
return new FsTransportFactory('default_fs');
143144
}
144145

146+
if ($connectionFactory instanceof DbalConnectionFactory) {
147+
return new FsTransportFactory('default_dbal');
148+
}
149+
145150
if ($connectionFactory instanceof NullConnectionFactory) {
146151
return new NullTransportFactory('default_null');
147152
}

pkg/enqueue/Tests/Functions/DsnToConnectionFactoryFunctionTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\Tests\Functions;
44

55
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Enqueue\Dbal\DbalConnectionFactory;
67
use Enqueue\Fs\FsConnectionFactory;
78
use Enqueue\Null\NullConnectionFactory;
89
use PHPUnit\Framework\TestCase;
@@ -57,5 +58,9 @@ public static function provideDSNs()
5758
yield ['file:///foo/bar/baz', FsConnectionFactory::class];
5859

5960
yield ['null://', NullConnectionFactory::class];
61+
62+
yield ['mysql://', DbalConnectionFactory::class];
63+
64+
yield ['pgsql://', DbalConnectionFactory::class];
6065
}
6166
}

pkg/enqueue/Tests/Symfony/DefaultTransportFactoryTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,9 @@ public static function provideDSNs()
255255
yield ['null://', 'default_null'];
256256

257257
yield ['file://', 'default_fs'];
258+
259+
yield ['mysql://', 'default_dbal'];
260+
261+
yield ['pgsql://', 'default_dbal'];
258262
}
259263
}

pkg/enqueue/functions.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Enqueue\AmqpExt\AmqpConnectionFactory;
66
use Enqueue\Consumption\QueueConsumer;
7+
use Enqueue\Dbal\DbalConnectionFactory;
78
use Enqueue\Fs\FsConnectionFactory;
89
use Enqueue\Null\NullConnectionFactory;
910
use Enqueue\Psr\PsrConnectionFactory;
@@ -30,6 +31,23 @@ function dsn_to_connection_factory($dsn)
3031
$map['null'] = NullConnectionFactory::class;
3132
}
3233

34+
if (class_exists(DbalConnectionFactory::class)) {
35+
$map['db2'] = DbalConnectionFactory::class;
36+
$map['ibm_db2'] = DbalConnectionFactory::class;
37+
$map['mssql'] = DbalConnectionFactory::class;
38+
$map['pdo_sqlsrv'] = DbalConnectionFactory::class;
39+
$map['mysql'] = DbalConnectionFactory::class;
40+
$map['mysql2'] = DbalConnectionFactory::class;
41+
$map['pdo_mysql'] = DbalConnectionFactory::class;
42+
$map['pgsql'] = DbalConnectionFactory::class;
43+
$map['postgres'] = DbalConnectionFactory::class;
44+
$map['postgresql'] = DbalConnectionFactory::class;
45+
$map['pdo_pgsql'] = DbalConnectionFactory::class;
46+
$map['sqlite'] = DbalConnectionFactory::class;
47+
$map['sqlite3'] = DbalConnectionFactory::class;
48+
$map['pdo_sqlite'] = DbalConnectionFactory::class;
49+
}
50+
3351
list($scheme) = explode('://', $dsn);
3452
if (false == $scheme || false === strpos($dsn, '://')) {
3553
throw new \LogicException(sprintf('The scheme could not be parsed from DSN "%s"', $dsn));

0 commit comments

Comments
 (0)