Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 88bc428

Browse files
committed
Added better XML support
1 parent 0e92ae6 commit 88bc428

File tree

2 files changed

+86
-49
lines changed

2 files changed

+86
-49
lines changed

DependencyInjection/Configuration.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ class Configuration implements ConfigurationInterface
1414
*/
1515
public function getConfigTreeBuilder()
1616
{
17+
$needsNormalization = function ($v) {
18+
if (!is_array($v)) {
19+
return false;
20+
}
21+
22+
return isset($v['option']);
23+
};
24+
$doNormalization = function ($v) {
25+
$value = array();
26+
foreach ($v['option'] as $option) {
27+
$value[$option['name']] = $option['value'];
28+
}
29+
30+
return $value;
31+
};
32+
1733
$treeBuilder = new TreeBuilder();
1834
$treeBuilder->root('cmf_routing_auto')
1935
->children()
@@ -26,15 +42,24 @@ public function getConfigTreeBuilder()
2642
->prototype('array')
2743
->children()
2844
->arrayNode('provider')
29-
->useAttributeAsKey('key')
45+
->beforeNormalization()
46+
->ifTrue($needsNormalization)
47+
->then($doNormalization)
48+
->end()
3049
->prototype('scalar')->end()
3150
->end()
3251
->arrayNode('exists_action')
33-
->useAttributeAsKey('key')
52+
->beforeNormalization()
53+
->ifTrue($needsNormalization)
54+
->then($doNormalization)
55+
->end()
3456
->prototype('scalar')->end()
3557
->end()
3658
->arrayNode('not_exists_action')
37-
->useAttributeAsKey('key')
59+
->beforeNormalization()
60+
->ifTrue($needsNormalization)
61+
->then($doNormalization)
62+
->end()
3863
->prototype('scalar')->end()
3964
->end()
4065
->end()
@@ -43,15 +68,24 @@ public function getConfigTreeBuilder()
4368
->arrayNode('content_name')
4469
->children()
4570
->arrayNode('provider')
46-
->useAttributeAsKey('key')
71+
->beforeNormalization()
72+
->ifTrue($needsNormalization)
73+
->then($doNormalization)
74+
->end()
4775
->prototype('scalar')->end()
4876
->end()
4977
->arrayNode('exists_action')
50-
->useAttributeAsKey('key')
78+
->beforeNormalization()
79+
->ifTrue($needsNormalization)
80+
->then($doNormalization)
81+
->end()
5182
->prototype('scalar')->end()
5283
->end()
5384
->arrayNode('not_exists_action')
54-
->useAttributeAsKey('key')
85+
->beforeNormalization()
86+
->ifTrue($needsNormalization)
87+
->then($doNormalization)
88+
->end()
5589
->prototype('scalar')->end()
5690
->end()
5791
->end()

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public function setUp()
2929
),
3030
),
3131
'content_name' => array(
32-
"provider" => array(
33-
"name" => "content_method",
34-
"method" => "getTitle",
32+
'provider' => array(
33+
'name' => 'content_method',
34+
'method' => 'getTitle',
3535
),
36-
"exists_action" => array(
37-
"strategy" => "auto_increment",
38-
"pattern" => "-%d",
36+
'exists_action' => array(
37+
'strategy' => 'auto_increment',
38+
'pattern' => '-%d',
3939
),
40-
"not_exists_action" => array(
41-
"strategy" => "create",
40+
'not_exists_action' => array(
41+
'strategy' => 'create',
4242
),
4343
),
4444
),
@@ -66,69 +66,72 @@ public function testXmlConfig()
6666
$this->assertProcessedConfigurationEquals(
6767
array(
6868
array(
69-
"auto-route-mapping" => array(
69+
'auto-route-mapping' => array(
7070
array(
71-
"class" => "Acme\BasicCmsBundle\Document\Page",
72-
"content-path" => array(
73-
"pages" => array(
74-
"provider" => array(
75-
"option" => array(
71+
'class' => 'Acme\BasicCmsBundle\Document\Page',
72+
'content-path' => array(
73+
array(
74+
'name' => 'pages',
75+
'provider' => array(
76+
'option' => array(
7677
array(
77-
"name" => "name",
78-
"value" => "specified",
78+
'name' => 'name',
79+
'value' => 'specified',
7980
),
8081
array(
81-
"name" => "path",
82-
"value" => "/cms/routes/page",
82+
'name' => 'path',
83+
'value' => '/cms/routes/page',
8384
),
8485
),
8586
),
86-
"exists-action" => array(
87-
"option" => array(
87+
'exists-action' => array(
88+
'option' => array(
8889
array(
89-
"name" => "strategy",
90-
"value" => "use",
90+
'name' => 'strategy',
91+
'value' => 'use',
9192
)
9293
),
9394
),
94-
"not-exists-action" => array(
95-
"option" => array(
96-
"name" => "strategy",
97-
"value" => "create",
95+
'not-exists-action' => array(
96+
'option' => array(
97+
array(
98+
'name' => 'strategy',
99+
'value' => 'create',
100+
),
98101
),
99102
),
100103
),
101104
),
102-
"content-name" => array(
103-
"provider" => array(
104-
"option" => array(
105+
'content-name' => array(
106+
'provider' => array(
107+
'option' => array(
105108
array(
106-
"name" => "name",
107-
"value" => "content_method",
109+
'name' => 'name',
110+
'value' => 'content_method',
108111
),
109112
array(
110-
"name" => "method",
111-
"value" => "getTitle",
113+
'name' => 'method',
114+
'value' => 'getTitle',
112115
),
113116
),
114117
),
115-
"exists-action" => array(
116-
"option" => array(
118+
'exists-action' => array(
119+
'option' => array(
117120
array(
118-
"name" => "strategy",
119-
"value" => "auto_increment",
121+
'name' => 'strategy',
122+
'value' => 'auto_increment',
120123
),
121124
array(
122-
"name" => "pattern",
123-
"value" => "-%d",
125+
'name' => 'pattern',
126+
'value' => '-%d',
124127
),
125128
),
126129
),
127-
"not-exists-action" => array(
128-
"option" => array(
130+
'not-exists-action' => array(
131+
'option' => array(
129132
array(
130-
"name" => "strategy",
131-
"value" => "create",
133+
'name' => 'strategy',
134+
'value' => 'create',
132135
),
133136
),
134137
),

0 commit comments

Comments
 (0)