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

Commit f843523

Browse files
committed
[GHI-38] [feature] Configuration refactoring
Refactored configuration to be more flexible. Closes #38
1 parent 482737f commit f843523

File tree

11 files changed

+259
-253
lines changed

11 files changed

+259
-253
lines changed

AutoRoute/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ protected function generateRouteStackChain($classFqn)
132132

133133
$routeStackChain = new BuilderUnitChain($this->builder);
134134

135-
foreach ($mapping['content_path'] as $builderName => $builderConfig) {
135+
foreach ($mapping['content_path']['path_units'] as $builderName => $builderConfig) {
136136
$builderUnit = $this->generateBuilderUnit($builderConfig);
137137
$routeStackChain->addBuilderUnit($builderName, $builderUnit);
138138
}
@@ -230,7 +230,7 @@ private function getBuilderService($builderConfig, $type, $aliasKey)
230230
// to be stateless (which is good here)
231231
$service = $this->container->get($serviceId);
232232
unset($builderConfig[$type][$aliasKey]);
233-
$service->init($builderConfig[$type]);
233+
$service->init($builderConfig[$type]['options']);
234234

235235
return $service;
236236
}

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Changelog
22
=========
33

4+
* **2013-12-08**: Major configuration format changes.
5+
See the documentation: http://symfony.com/doc/current/cmf/bundles/routing_auto.html
6+
47
1.0.0-alpha4
58
------------
69

DependencyInjection/CmfRoutingAutoExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ public function load(array $configs, ContainerBuilder $container)
3030
$chainFactoryDef = $container->getDefinition('cmf_routing_auto.factory');
3131

3232
// normalize configuration
33-
foreach ($config['auto_route_mapping'] as $classFqn => $config) {
33+
foreach ($config['mappings'] as $classFqn => $config) {
3434
$chainFactoryDef->addMethodCall('registerMapping', array($classFqn, $config));
3535
}
3636
}
37+
38+
public function getNamespace()
39+
{
40+
return 'http://cmf.symfony.com/schema/dic/routing_auto';
41+
}
3742
}
3843

DependencyInjection/Configuration.php

Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,85 +14,92 @@ 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-
3317
$treeBuilder = new TreeBuilder();
3418
$treeBuilder->root('cmf_routing_auto')
19+
->fixXmlConfig('mapping')
3520
->children()
36-
->arrayNode('auto_route_mapping')
37-
->useAttributeAsKey('class')
38-
->prototype('array')
39-
->children()
40-
->arrayNode('content_path')
41-
->useAttributeAsKey('name')
42-
->prototype('array')
43-
->children()
44-
->arrayNode('provider')
21+
->arrayNode('mappings')
22+
->useAttributeAsKey('class')
23+
->prototype('array')
24+
->children()
25+
->arrayNode('content_path')
4526
->beforeNormalization()
46-
->ifTrue($needsNormalization)
47-
->then($doNormalization)
27+
->ifTrue(function ($v) {
28+
return !isset($v['path_unit']) && !isset($v['path_units']);
29+
})
30+
->then(function ($v) {
31+
return array(
32+
'path_units' => $v,
33+
);
34+
})
4835
->end()
49-
->prototype('scalar')->end()
50-
->end()
51-
->arrayNode('exists_action')
52-
->beforeNormalization()
53-
->ifTrue($needsNormalization)
54-
->then($doNormalization)
36+
->fixXmlConfig('path_unit')
37+
->children()
38+
->arrayNode('path_units')
39+
->useAttributeAsKey('name')
40+
->prototype('array')
41+
->children()
42+
->append($this->getUnitConfigOption('provider', 'name'))
43+
->append($this->getUnitConfigOption('exists_action'))
44+
->append($this->getUnitConfigOption('not_exists_action'))
45+
->end()
46+
->end()
47+
->end() // path_units
5548
->end()
56-
->prototype('scalar')->end()
57-
->end()
58-
->arrayNode('not_exists_action')
59-
->beforeNormalization()
60-
->ifTrue($needsNormalization)
61-
->then($doNormalization)
49+
->end() // content_path
50+
->arrayNode('content_name')
51+
->children()
52+
->append($this->getUnitConfigOption('provider', 'name'))
53+
->append($this->getUnitConfigOption('exists_action'))
54+
->append($this->getUnitConfigOption('not_exists_action'))
6255
->end()
63-
->prototype('scalar')->end()
64-
->end()
65-
->end()
56+
->end() // content_name
6657
->end()
6758
->end()
68-
->arrayNode('content_name')
69-
->children()
70-
->arrayNode('provider')
71-
->beforeNormalization()
72-
->ifTrue($needsNormalization)
73-
->then($doNormalization)
74-
->end()
75-
->prototype('scalar')->end()
76-
->end()
77-
->arrayNode('exists_action')
78-
->beforeNormalization()
79-
->ifTrue($needsNormalization)
80-
->then($doNormalization)
81-
->end()
82-
->prototype('scalar')->end()
83-
->end()
84-
->arrayNode('not_exists_action')
85-
->beforeNormalization()
86-
->ifTrue($needsNormalization)
87-
->then($doNormalization)
88-
->end()
89-
->prototype('scalar')->end()
90-
->end()
91-
->end()
92-
->end()
59+
->end() // mappings
9360
->end();
9461

9562
return $treeBuilder;
9663
}
64+
65+
protected function getUnitConfigOption($name, $nameOption = 'strategy')
66+
{
67+
$builder = new TreeBuilder();
68+
$node = $builder->root($name);
69+
70+
$node
71+
->fixXmlConfig('option')
72+
->beforeNormalization()
73+
->ifTrue(function ($v) {
74+
return is_string($v);
75+
})
76+
->then(function ($v) use ($nameOption) {
77+
return array(
78+
$nameOption => $v,
79+
'options' => array(),
80+
);
81+
})
82+
->end()
83+
->beforeNormalization()
84+
->ifTrue(function ($v) use ($nameOption) {
85+
return !isset($v[$nameOption]);
86+
})
87+
->then(function ($v) use ($nameOption) {
88+
return array(
89+
$nameOption => $v[0],
90+
'options' => isset($v[1]) ? $v[1] : array(),
91+
);
92+
})
93+
->end()
94+
->children()
95+
->scalarNode($nameOption)->isRequired()->cannotBeEmpty()->end()
96+
->arrayNode('options')
97+
->useAttributeAsKey('name')
98+
->prototype('scalar')->end()
99+
->end()
100+
->end();
101+
102+
return $node;
103+
}
97104
}
98105

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
$container->loadFromExtension('cmf_routing_auto', array(
4+
'mappings' => array(
5+
'Acme\BasicCmsBundle\Document\Page' => array(
6+
'content_path' => array(
7+
'pages' => array(
8+
'provider' => array('specified', array('path' => '/cms/routes/page')),
9+
'exists_action' => 'use',
10+
'not_exists_action' => array(
11+
'strategy' => 'create',
12+
),
13+
),
14+
),
15+
'content_name' => array(
16+
'provider' => array('content_method', array('method' => 'getTitle')),
17+
'exists_action' => array(
18+
'strategy' => 'auto_increment',
19+
'options' => array(
20+
'pattern' => '-%d',
21+
),
22+
),
23+
'not_exists_action' => array('create'),
24+
),
25+
),
26+
),
27+
));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services">
3+
4+
<config xmlns="http://cmf.symfony.com/schema/dic/routing_auto">
5+
6+
<mapping class="Acme\BasicCmsBundle\Document\Page">
7+
8+
<content-path>
9+
<path-unit name="pages">
10+
<provider name="specified">
11+
<option name="path" value="/cms/routes/page" />
12+
</provider>
13+
<exists-action strategy="use" />
14+
<not-exists-action strategy="create" />
15+
</path-unit>
16+
</content-path>
17+
18+
<content-name>
19+
<provider name="content_method">
20+
<option name="method" value="getTitle" />
21+
</provider>
22+
<exists-action strategy="auto_increment">
23+
<option name="pattern" value="-%d" />
24+
</exists-action>
25+
<not-exists-action strategy="create" />
26+
</content-name>
27+
</mapping>
28+
</config>
29+
</container>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmf_routing_auto:
2+
mappings:
3+
Acme\BasicCmsBundle\Document\Page:
4+
content_path:
5+
pages:
6+
provider: [ specified, { path: /cms/routes/page } ]
7+
exists_action: use
8+
not_exists_action:
9+
strategy: create
10+
content_name:
11+
provider: [ content_method, { method: getTitle } ]
12+
exists_action:
13+
strategy: auto_increment
14+
options:
15+
pattern: -%d
16+
not_exists_action: [ create ]

Tests/Resources/app/config/routingautoroute.yml

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,51 @@ cmf_routing:
1515
route_basepath: /test/routing
1616

1717
cmf_routing_auto:
18-
19-
auto_route_mapping:
20-
21-
##
18+
mappings:
2219
# e.g. /cms/auto-route/blog/my-blogs-title
2320
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog:
24-
2521
# generate or use path components leading up to the final part of the path
2622
content_path:
27-
base:
28-
provider:
29-
name: specified
30-
path: test/auto-route
31-
exists_action:
32-
strategy: use
33-
not_exists_action:
34-
strategy: create
35-
namespace:
36-
provider:
37-
name: specified
38-
path: blog
39-
exists_action:
40-
strategy: use
41-
not_exists_action:
42-
strategy: create
43-
23+
path_units:
24+
base:
25+
provider: [ specified, { path: test/auto-route } ]
26+
exists_action: use
27+
not_exists_action: create
28+
namespace:
29+
provider: [ specified, { path: blog } ]
30+
exists_action: use
31+
not_exists_action: create
32+
# using alternative syntax
4433
content_name:
4534
provider:
4635
name: content_method
47-
method: getTitle
36+
options:
37+
method: getTitle
4838
exists_action:
4939
strategy: auto_increment
50-
pattern: -%d
51-
not_exists_action:
40+
options:
41+
pattern: -%d
42+
not_exists_action:
5243
strategy: create
53-
54-
##
5544
# e.g. /cms/auto-route/blog/my-blogs-title/2013-04-09/my-post-title
5645
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Post:
57-
5846
content_path:
59-
60-
# /cms/auto-route/blog/my-blogs-title
61-
blog_path:
62-
provider:
63-
name: content_object
64-
method: getBlog
65-
exists_action:
66-
strategy: use
67-
not_exists_action:
68-
strategy: throw_exception
69-
70-
date:
71-
provider:
72-
name: content_datetime
73-
method: getDate
74-
date_format: Y/m/d
75-
exists_action:
76-
strategy: use
77-
not_exists_action:
78-
strategy: create
79-
47+
path_units:
48+
# /cms/auto-route/blog/my-blogs-title
49+
blog_path:
50+
provider: [ content_object, { method: getBlog } ]
51+
exists_action: use
52+
not_exists_action: throw_exception
53+
date:
54+
provider:
55+
name: content_datetime
56+
options:
57+
method: getDate
58+
date_format: Y/m/d
59+
exists_action: use
60+
not_exists_action: create
8061
content_name:
8162
# my-post-title
82-
provider:
83-
name: content_method
84-
method: getTitle
85-
exists_action:
86-
strategy: auto_increment
87-
pattern: -%d
88-
not_exists_action:
89-
strategy: create
63+
provider: [ content_method, { method: getTitle } ]
64+
exists_action: [ auto_increment, { pattern: -%d } ]
65+
not_exists_action: [ create ]

0 commit comments

Comments
 (0)