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

Commit 32773f2

Browse files
committed
Merge pull request #35 from symfony-cmf/refactor_config
[WIP] Refactor config to better support XML
2 parents 5044037 + 88bc428 commit 32773f2

File tree

3 files changed

+188
-7
lines changed

3 files changed

+188
-7
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()
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\DependencyInjection;
4+
5+
use Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\Configuration;
7+
8+
class ConfigurationTest extends AbstractConfigurationTestCase
9+
{
10+
protected $inputConfig;
11+
12+
public function setUp()
13+
{
14+
$this->inputConfig = array(
15+
'auto_route_mapping' => array(
16+
'Acme\BasicCmsBundle\Document\Page' => array(
17+
'content_path' => array(
18+
'pages' => array(
19+
'provider' => array(
20+
'name' => 'specified',
21+
'path' => '/cms/routes/page',
22+
),
23+
'exists_action' => array(
24+
'strategy' => 'use',
25+
),
26+
'not_exists_action' => array(
27+
'strategy' => 'create',
28+
),
29+
),
30+
),
31+
'content_name' => array(
32+
'provider' => array(
33+
'name' => 'content_method',
34+
'method' => 'getTitle',
35+
),
36+
'exists_action' => array(
37+
'strategy' => 'auto_increment',
38+
'pattern' => '-%d',
39+
),
40+
'not_exists_action' => array(
41+
'strategy' => 'create',
42+
),
43+
),
44+
),
45+
),
46+
);
47+
}
48+
49+
protected function getConfiguration()
50+
{
51+
return new Configuration();
52+
}
53+
54+
public function testYamlConfig()
55+
{
56+
$this->assertProcessedConfigurationEquals(
57+
array(
58+
$this->inputConfig,
59+
),
60+
$this->inputConfig
61+
);
62+
}
63+
64+
public function testXmlConfig()
65+
{
66+
$this->assertProcessedConfigurationEquals(
67+
array(
68+
array(
69+
'auto-route-mapping' => array(
70+
array(
71+
'class' => 'Acme\BasicCmsBundle\Document\Page',
72+
'content-path' => array(
73+
array(
74+
'name' => 'pages',
75+
'provider' => array(
76+
'option' => array(
77+
array(
78+
'name' => 'name',
79+
'value' => 'specified',
80+
),
81+
array(
82+
'name' => 'path',
83+
'value' => '/cms/routes/page',
84+
),
85+
),
86+
),
87+
'exists-action' => array(
88+
'option' => array(
89+
array(
90+
'name' => 'strategy',
91+
'value' => 'use',
92+
)
93+
),
94+
),
95+
'not-exists-action' => array(
96+
'option' => array(
97+
array(
98+
'name' => 'strategy',
99+
'value' => 'create',
100+
),
101+
),
102+
),
103+
),
104+
),
105+
'content-name' => array(
106+
'provider' => array(
107+
'option' => array(
108+
array(
109+
'name' => 'name',
110+
'value' => 'content_method',
111+
),
112+
array(
113+
'name' => 'method',
114+
'value' => 'getTitle',
115+
),
116+
),
117+
),
118+
'exists-action' => array(
119+
'option' => array(
120+
array(
121+
'name' => 'strategy',
122+
'value' => 'auto_increment',
123+
),
124+
array(
125+
'name' => 'pattern',
126+
'value' => '-%d',
127+
),
128+
),
129+
),
130+
'not-exists-action' => array(
131+
'option' => array(
132+
array(
133+
'name' => 'strategy',
134+
'value' => 'create',
135+
),
136+
),
137+
),
138+
),
139+
),
140+
),
141+
),
142+
),
143+
$this->inputConfig
144+
);
145+
}
146+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"aferrandini/urlizer": "1.0.*"
1717
},
1818
"require-dev": {
19-
"symfony-cmf/testing": "1.0.*"
19+
"symfony-cmf/testing": "1.0.*",
20+
"matthiasnoback/symfony-config-test": "0.*"
2021
},
2122
"suggest": {
2223
"doctrine/phpcr-bundle": "To enable support for the PHPCR ODM documents"

0 commit comments

Comments
 (0)