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

Skip to content

Commit e31d4f1

Browse files
committed
[Config] allowed the disabling of key normalization on some array nodes
This reverts #6086
1 parent 1de60c9 commit e31d4f1

File tree

9 files changed

+105
-94
lines changed

9 files changed

+105
-94
lines changed

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.2.0
55
-----
66

7+
* added a `keepKeys()` method for array nodes (to avoid key normalization)
78
* added numerical type handling for config definitions
89
* added convenience methods for optional configuration sections to ArrayNodeDefinition
910
* added a utils class for XML manipulations

src/Symfony/Component/Config/Definition/ArrayNode.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
2929
protected $addIfNotSet;
3030
protected $performDeepMerging;
3131
protected $ignoreExtraKeys;
32+
protected $normalizeKeys;
3233

3334
/**
3435
* Constructor.
@@ -47,6 +48,41 @@ public function __construct($name, NodeInterface $parent = null)
4748
$this->addIfNotSet = false;
4849
$this->allowNewKeys = true;
4950
$this->performDeepMerging = true;
51+
$this->normalizeKeys = true;
52+
}
53+
54+
public function setNormalizeKeys($normalizeKeys)
55+
{
56+
$this->normalizeKeys = (Boolean) $normalizeKeys;
57+
}
58+
59+
/**
60+
* Normalizes keys between the different configuration formats.
61+
*
62+
* Namely, you mostly have foo_bar in YAML while you have foo-bar in XML.
63+
* After running this method, all keys are normalized to foo_bar.
64+
*
65+
* If you have a mixed key like foo-bar_moo, it will not be altered.
66+
* The key will also not be altered if the target key already exists.
67+
*
68+
* @param mixed $value
69+
*
70+
* @return array The value with normalized keys
71+
*/
72+
protected function preNormalize($value)
73+
{
74+
if (!$this->normalizeKeys || !is_array($value)) {
75+
return $value;
76+
}
77+
78+
foreach ($value as $k => $v) {
79+
if (false !== strpos($k, '-') && false === strpos($k, '_') && !array_key_exists($normalizedKey = str_replace('-', '_', $k), $value)) {
80+
$value[$normalizedKey] = $v;
81+
unset($value[$k]);
82+
}
83+
}
84+
85+
return $value;
5086
}
5187

5288
/**

src/Symfony/Component/Config/Definition/BaseNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ final public function merge($leftSide, $rightSide)
247247
*/
248248
final public function normalize($value)
249249
{
250+
$value = $this->preNormalize($value);
251+
250252
// run custom normalization closures
251253
foreach ($this->normalizationClosures as $closure) {
252254
$value = $closure($value);
@@ -266,6 +268,18 @@ final public function normalize($value)
266268
return $this->normalizeValue($value);
267269
}
268270

271+
/**
272+
* Normalizes the value before any other normalization is applied.
273+
*
274+
* @param $value
275+
*
276+
* @return $value The normalized array value
277+
*/
278+
protected function preNormalize($value)
279+
{
280+
return $value;
281+
}
282+
269283
/**
270284
* Finalizes a value, applying all finalization closures.
271285
*

src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition
3434
protected $addDefaults;
3535
protected $addDefaultChildren;
3636
protected $nodeBuilder;
37+
protected $normalizeKeys;
3738

3839
/**
3940
* {@inheritDoc}
@@ -51,6 +52,7 @@ public function __construct($name, NodeParentInterface $parent = null)
5152
$this->performDeepMerging = true;
5253
$this->nullEquivalent = array();
5354
$this->trueEquivalent = array();
55+
$this->normalizeKeys = true;
5456
}
5557

5658
/**
@@ -281,6 +283,18 @@ public function ignoreExtraKeys()
281283
return $this;
282284
}
283285

286+
/**
287+
* Disables key normalization.
288+
*
289+
* @return ArrayNodeDefinition
290+
*/
291+
public function keepKeys()
292+
{
293+
$this->normalizeKeys = false;
294+
295+
return $this;
296+
}
297+
284298
/**
285299
* Appends a node definition.
286300
*
@@ -368,6 +382,7 @@ protected function createNode()
368382
$node->setPerformDeepMerging($this->performDeepMerging);
369383
$node->setRequired($this->required);
370384
$node->setIgnoreExtraKeys($this->ignoreExtraKeys);
385+
$node->setNormalizeKeys($this->normalizeKeys);
371386

372387
if (null !== $this->normalization) {
373388
$node->setNormalizationClosures($this->normalization->before);

src/Symfony/Component/Config/Definition/Processor.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ class Processor
2323
*
2424
* @param NodeInterface $configTree The node tree describing the configuration
2525
* @param array $configs An array of configuration items to process
26-
* @param bool $normalizeKeys Flag indicating if config key normalization is needed. True by default.
2726
*
2827
* @return array The processed configuration
2928
*/
30-
public function process(NodeInterface $configTree, array $configs, $normalizeKeys = true)
29+
public function process(NodeInterface $configTree, array $configs)
3130
{
32-
if ($normalizeKeys) {
33-
$configs = self::normalizeKeys($configs);
34-
}
35-
3631
$currentConfig = array();
3732
foreach ($configs as $config) {
3833
$config = $configTree->normalize($config);
@@ -47,42 +42,12 @@ public function process(NodeInterface $configTree, array $configs, $normalizeKey
4742
*
4843
* @param ConfigurationInterface $configuration The configuration class
4944
* @param array $configs An array of configuration items to process
50-
* @param bool $normalizeKeys Flag indicating if config key normalization is needed. True by default.
5145
*
5246
* @return array The processed configuration
5347
*/
54-
public function processConfiguration(ConfigurationInterface $configuration, array $configs, $normalizeKeys = true)
55-
{
56-
return $this->process($configuration->getConfigTreeBuilder()->buildTree(), $configs, $normalizeKeys);
57-
}
58-
59-
/**
60-
* This method normalizes keys between the different configuration formats
61-
*
62-
* Namely, you mostly have foo_bar in YAML while you have foo-bar in XML.
63-
* After running this method, all keys are normalized to foo_bar.
64-
*
65-
* If you have a mixed key like foo-bar_moo, it will not be altered.
66-
* The key will also not be altered if the target key already exists.
67-
*
68-
* @param array $config
69-
*
70-
* @return array the config with normalized keys
71-
*/
72-
public static function normalizeKeys(array $config)
48+
public function processConfiguration(ConfigurationInterface $configuration, array $configs)
7349
{
74-
foreach ($config as $key => $value) {
75-
if (is_array($value)) {
76-
$config[$key] = self::normalizeKeys($value);
77-
}
78-
79-
if (false !== strpos($key, '-') && false === strpos($key, '_') && !array_key_exists($normalizedKey = str_replace('-', '_', $key), $config)) {
80-
$config[$normalizedKey] = $config[$key];
81-
unset($config[$key]);
82-
}
83-
}
84-
85-
return $config;
50+
return $this->process($configuration->getConfigTreeBuilder()->buildTree(), $configs);
8651
}
8752

8853
/**

src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Tests\Definition;
1313

1414
use Symfony\Component\Config\Definition\ArrayNode;
15+
use Symfony\Component\Config\Definition\PrototypedArrayNode;
1516

1617
class ArrayNodeTest extends \PHPUnit_Framework_TestCase
1718
{
@@ -54,4 +55,35 @@ public function testIgnoreExtraKeysNoException()
5455
$node->normalize(array('foo' => 'bar'));
5556
$this->assertTrue(true, 'No exception was thrown when setIgnoreExtraKeys is true');
5657
}
58+
59+
/**
60+
* @dataProvider getPreNormalizationTests
61+
*/
62+
public function testPreNormalize($denormalized, $normalized)
63+
{
64+
$node = new ArrayNode('foo');
65+
66+
$r = new \ReflectionMethod($node, 'preNormalize');
67+
$r->setAccessible(true);
68+
69+
$this->assertSame($normalized, $r->invoke($node, $denormalized));
70+
}
71+
72+
public function getPreNormalizationTests()
73+
{
74+
return array(
75+
array(
76+
array('foo-bar' => 'foo'),
77+
array('foo_bar' => 'foo'),
78+
),
79+
array(
80+
array('foo-bar_moo' => 'foo'),
81+
array('foo-bar_moo' => 'foo'),
82+
),
83+
array(
84+
array('foo-bar' => null, 'foo_bar' => 'foo'),
85+
array('foo-bar' => null, 'foo_bar' => 'foo'),
86+
)
87+
);
88+
}
5789
}

src/Symfony/Component/Config/Tests/Definition/ProcessorTest.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/Symfony/Component/Config/Tests/Definition/PrototypedArrayNodeTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
class PrototypedArrayNodeTest extends \PHPUnit_Framework_TestCase
1919
{
20-
2120
public function testGetDefaultValueReturnsAnEmptyArrayForPrototypes()
2221
{
2322
$node = new PrototypedArrayNode('root');
@@ -31,8 +30,8 @@ public function testGetDefaultValueReturnsDefaultValueForPrototypes()
3130
$node = new PrototypedArrayNode('root');
3231
$prototype = new ArrayNode(null, $node);
3332
$node->setPrototype($prototype);
34-
$node->setDefaultValue(array ('test'));
35-
$this->assertEquals(array ('test'), $node->getDefaultValue());
33+
$node->setDefaultValue(array('test'));
34+
$this->assertEquals(array('test'), $node->getDefaultValue());
3635
}
3736

3837
// a remapped key (e.g. "mapping" -> "mappings") should be unset after being used

src/Symfony/Component/DependencyInjection/Extension/Extension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public function getAlias()
7575
return Container::underscore($classBaseName);
7676
}
7777

78-
final protected function processConfiguration(ConfigurationInterface $configuration, array $configs, $normalizeKeys = true)
78+
final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
7979
{
8080
$processor = new Processor();
8181

82-
return $processor->processConfiguration($configuration, $configs, $normalizeKeys);
82+
return $processor->processConfiguration($configuration, $configs);
8383
}
8484

8585
/**

0 commit comments

Comments
 (0)