Skip to content

Commit 9f86ad9

Browse files
committed
Adds normalizer class for consistently normalizing database driver values.
1 parent f2b59c7 commit 9f86ad9

2 files changed

Lines changed: 59 additions & 8 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ private function addGeneralSection(ArrayNodeDefinition $node)
110110
*/
111111
private function addDbalSection(ArrayNodeDefinition $node)
112112
{
113+
$dNormalizer = new DriverNormalizer();
114+
$dClosure = function($v) use ($dNormalizer) { return $dNormalizer->normalize($v); };
113115
$node
114116
->children()
115117
->arrayNode('dbal')
@@ -122,7 +124,7 @@ private function addDbalSection(ArrayNodeDefinition $node)
122124
->scalarNode('driver')
123125
->beforeNormalization()
124126
->always()
125-
->then(function($v) { return str_replace('pdo_', '', $v); })
127+
->then($dClosure)
126128
->end()
127129
->defaultValue('mysql')
128130
->end()
@@ -131,7 +133,7 @@ private function addDbalSection(ArrayNodeDefinition $node)
131133
->scalarNode('dsn')
132134
->beforeNormalization()
133135
->always()
134-
->then(function($v) { return str_replace('pdo_', '', $v); })
136+
->then($dClosure)
135137
->end()
136138
->defaultValue('')
137139
->end()
@@ -162,7 +164,7 @@ private function addDbalSection(ArrayNodeDefinition $node)
162164
->end()
163165
->end()
164166
->fixXmlConfig('connection')
165-
->append($this->getDbalConnectionsNode())
167+
->append($this->getDbalConnectionsNode($dNormalizer))
166168
->end()
167169
;
168170
}
@@ -183,8 +185,10 @@ private function addDbalSection(ArrayNodeDefinition $node)
183185
*
184186
* @return ArrayNodeDefinition|NodeDefinition The tree builder
185187
*/
186-
private function getDbalConnectionsNode()
188+
private function getDbalConnectionsNode(DriverNormalizer $normalizer)
187189
{
190+
$closure = function($v) use ($normalizer) { return $normalizer->normalize($v); };
191+
188192
if (Kernel::MAJOR_VERSION > 4 || Kernel::MAJOR_VERSION === 4 && Kernel::MINOR_VERSION >= 2)
189193
{
190194
$treeBuilder = new TreeBuilder('connections');
@@ -204,7 +208,7 @@ private function getDbalConnectionsNode()
204208
->scalarNode('driver')
205209
->beforeNormalization()
206210
->always()
207-
->then(function($v) { return str_replace('pdo_', '', $v); })
211+
->then($closure)
208212
->end()
209213
->defaultValue('mysql')
210214
->end()
@@ -213,7 +217,7 @@ private function getDbalConnectionsNode()
213217
->scalarNode('dsn')
214218
->beforeNormalization()
215219
->always()
216-
->then(function($v) { return str_replace('pdo_', '', $v); })
220+
->then($closure)
217221
->end()
218222
->defaultValue('')
219223
->end()
@@ -225,7 +229,7 @@ private function getDbalConnectionsNode()
225229
->scalarNode('driver')
226230
->beforeNormalization()
227231
->always()
228-
->then(function($v) { return str_replace('pdo_', '', $v); })
232+
->then($closure)
229233
->end()
230234
->defaultValue('mysql')
231235
->end()
@@ -234,7 +238,7 @@ private function getDbalConnectionsNode()
234238
->scalarNode('dsn')
235239
->beforeNormalization()
236240
->always()
237-
->then(function($v) { return str_replace('pdo_', '', $v); })
241+
->then($closure)
238242
->end()
239243
->defaultValue('')
240244
->end()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Propel\Bundle\PropelBundle\DependencyInjection;
4+
5+
/**
6+
* Provides normalization for Database Driver values.
7+
*
8+
* Class DriverNormalizer
9+
* @package Propel\Bundle\PropelBundle\DependencyInjection
10+
*/
11+
class DriverNormalizer
12+
{
13+
/**
14+
* Normalizes the database driver related keys in a Propel datasource configuration, specifically the 'adapter'
15+
* value & the dsn value of the connection configuration.
16+
*
17+
* @param array $datasource
18+
*
19+
* @return array
20+
*/
21+
public function normalizeDatasource($datasource)
22+
{
23+
if ($adapter = $datasource['adapter'] ?? null)
24+
{
25+
$datasource['adapter'] = $this->normalize($adapter);
26+
}
27+
28+
if ($dsn = $datasource['connection']['dsn'] ?? null)
29+
{
30+
$datasource['connection']['dsn'] = $this->normalize($dsn);
31+
}
32+
33+
return $datasource;
34+
}
35+
36+
/**
37+
* Removes the 'pdo_' prefix found in some database driver strings.
38+
*
39+
* @param string $value
40+
*
41+
* @return string
42+
*/
43+
public function normalize($value)
44+
{
45+
return str_replace('pdo_', '', $value);
46+
}
47+
}

0 commit comments

Comments
 (0)