8000 merged branch vicb/config/master/fix (PR #3365) · symfony/symfony@883637d · GitHub
[go: up one dir, main page]

Skip to content

Commit 883637d

Browse files
committed
merged branch vicb/config/master/fix (PR #3365)
Commits ------- 0a176eb [FrameworkBundle] Fix configuration errors 6745b28 [Config] Throw exceptions on invalid definition fb27de0 [Config] cleanup Discussion ---------- [Config] Cleanup, error detection, fixes see #3357 --------------------------------------------------------------------------- by stloyd at 2012-02-15T10:56:00Z @vicb As you added new exceptions, IMO you should add some tests to cover it. --------------------------------------------------------------------------- by vicb at 2012-02-15T10:56:50Z good point, I'll do. --------------------------------------------------------------------------- by vicb at 2012-02-15T13:49:44Z @stloyd that was a great idea, I realized I had miss a case. It has been added and should be covered by UT + fixes made. I am done with the fixes, should be ready to merge. And time to give the `PrototypedArrayNode` some more usability now.
2 parents df91627 + 0a176eb commit 883637d

File tree

11 files changed

+195
-112
lines changed

11 files changed

+195
-112
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,14 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
217217
->fixXmlConfig('resource')
218218
->children()
219219
->arrayNode('resources')
220-
->addDefaultsIfNotSet()
221220
->defaultValue(array('FrameworkBundle:Form'))
221+
->prototype('scalar')->end()
222222
->validate()
223223
->ifTrue(function($v) {return !in_array('FrameworkBundle:Form', $v); })
224224
->then(function($v){
225225
return array_merge(array('FrameworkBundle:Form'), $v);
226226
})
227227
->end()
228-
->prototype('scalar')->end()
229228
->end()
230229
->end()
231230
->end()
@@ -235,7 +234,6 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
235234
->arrayNode('assets_base_urls')
236235
->performNoDeepMerging()
237236
->addDefaultsIfNotSet()
238-
->defaultValue(array('http' => array(), 'ssl' => array()))
239237
->beforeNormalization()
240238
->ifTrue(function($v) { return !is_array($v); })
241239
->then(function($v) { return array($v); })
@@ -290,7 +288,6 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
290288
->arrayNode('base_urls')
291289 ->performNoDeepMerging()
292290
->addDefaultsIfNotSet()
293-
->defaultValue(array('http' => array(), 'ssl' => array()))
294291
->beforeNormalization()
295292
->ifTrue(function($v) { return !is_array($v); })
296293
->then(function($v) { return array($v); })

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,15 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
5454
->fixXmlConfig('resource')
5555
->children()
5656
->arrayNode('resources')
57-
->addDefaultsIfNotSet()
5857
->defaultValue(array('form_div_layout.html.twig'))
58+
->prototype('scalar')->end()
5959
->setExample(array('MyBundle::form.html.twig'))
6060
->validate()
6161
->ifTrue(function($v) { return !in_array('form_div_layout.html.twig', $v); })
6262
->then(function($v){
6363
return array_merge(array('form_div_layout.html.twig'), $v);
6464
})
6565
->end()
66-
->prototype('scalar')->end()
6766
->end()
6867
->end()
6968
->end()

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct($name, NodeInterface $parent = null)
5656
}
5757

5858
/**
59-
* Sets info message
59+
* Sets info message.
6060
*
6161
* @param string $info The info text
6262
*/
@@ -66,7 +66,7 @@ public function setInfo($info)
6666
}
6767

6868
/**
69-
* Returns info message
69+
* Returns info message.
7070
*
7171
* @return string The info text
7272
*/
@@ -78,7 +78,7 @@ public function getInfo()
7878
/**
7979
* Sets the example configuration for this node.
8080
*
81-
* @param array $example
81+
* @param string|array $example
8282
*/
8383
public function setExample($example)
8484
{
@@ -88,7 +88,7 @@ public function setExample($example)
8888
/**
8989
* Retrieves the example configuration for this node.
9090
*
91-
* @return mixed The example
91+
* @return string|array The example
9292
*/
9393
public function getExample()
9494
{

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

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Definition\ArrayNode;
1515
use Symfony\Component\Config\Definition\PrototypedArrayNode;
16+
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
1617

1718
/**
1819
* This class provides a fluent interface for defining an array node.
@@ -50,7 +51,7 @@ public function __construct($name, NodeParentInterface $parent = null)
5051
}
5152

5253
/**
53-
* Set a custom children builder
54+
* Sets a custom children builder.
5455
*
5556
* @param NodeBuilder $builder A custom NodeBuilder
5657
*/
@@ -60,7 +61,7 @@ public function setBuilder(NodeBuilder $builder)
6061
}
6162

6263
/**
63-
* Returns a builder to add children nodes
64+
* Returns a builder to add children nodes.
6465
*
6566
* @return NodeBuilder
6667
*/
@@ -78,16 +79,16 @@ public function children()
7879
*/
7980
public function prototype($type)
8081
{
81-
$builder = $this->getNodeBuilder();
82-
$this->prototype = $builder->node(null, $type);
83-
$this->prototype->parent = $this;
84-
85-
return $this->prototype;
82+
return $this->prototype = $this->getNodeBuilder()->node(null, $type)->setParent($this);
8683
}
8784

8885
/**
8986
* Adds the default value if the node is not set in the configuration.
9087
*
88+
* This method is applicable to concrete nodes only (not to prototype nodes).
89+
* If this function has been called and the node is not set during the finalization
90+
* phase, it's default value will be derived from its children default values.
91+
*
9192
* @return ArrayNodeDefinition
9293
*/
9394
public function addDefaultsIfNotSet()
@@ -100,6 +101,8 @@ public function addDefaultsIfNotSet()
100101
/**
101102
* Requires the node to have at least one element.
102103
*
104+
* This method is applicable to prototype nodes only.
105+
*
103106
* @return ArrayNodeDefinition
104107
*/
105108
public function requiresAtLeastOneElement()
@@ -139,7 +142,7 @@ public function fixXmlConfig($singular, $plural = null)
139142
}
140143

141144
/**
142-
* Set the attribute which value is to be used as key.
145+
* Sets the attribute which value is to be used as key.
143146
*
144147
* This is useful when you have an indexed array FEE1 that should be an
145148
* associative array. You can select an item from within the array
@@ -148,17 +151,19 @@ public function fixXmlConfig($singular, $plural = null)
148151
*
149152
* array(
150153
* array('id' => 'my_name', 'foo' => 'bar'),
151-
* )
154+
* );
152155
*
153-
* becomes
156+
* becomes
154157
*
155158
* array(
156159
* 'my_name' => array('foo' => 'bar'),
157-
* )
160+
* );
158161
*
159162
* If you'd like "'id' => 'my_name'" to still be present in the resulting
160163
* array, then you can set the second argument of this method to false.
161164
*
165+
* This method is applicable to prototype nodes only.
166+
*
162167
* @param string $name The name of the key
163168
* @param Boolean $removeKeyItem Whether or not the key item should be removed.
164169
*
@@ -216,12 +221,12 @@ public function ignoreExtraKeys()
216221
}
217222

218223
/**
219-
* Append a node definition.
224+
* Appends a node definition.
220225
*
221226
* $node = new ArrayNodeDefinition()
222227
* ->children()
223-
* ->scalarNode('foo')
224-
* ->scalarNode('baz')
228+
* ->scalarNode('foo')->end()
229+
* ->scalarNode('baz')->end()
225230
* ->end()
226231
* ->append($this->getBarNodeDefinition())
227232
* ;
@@ -254,13 +259,57 @@ protected function getNodeBuilder()
254259
*/
255260
protected function createNode()
256261
{
257-
if (null == $this->prototype) {
262+
if (null === $this->prototype) {
263+
if (null !== $this->key) {
264+
throw new InvalidDefinitionException(
265+
sprintf('%s::useAttributeAsKey() is not applicable to concrete nodes.', __CLASS__)
266+
);
267+
}
268+
269+
if (true === $this->atLeastOne) {
270+
throw new InvalidDefinitionException(
271+
sprintf('%s::requiresAtLeastOneElement() is not applicable to concrete nodes.', __CLASS__)
272+
);
273+
}
274+
275+
if ($this->default) {
276+
throw new InvalidDefinitionException(
277+
sprintf('%s::defaultValue() is not applicable to concrete nodes.', __CLASS__)
278+
);
279+
}
280+
258281
$node = new ArrayNode($this->name, $this->parent);
282+
$node->setAddIfNotSet($this->addDefaults);
283+
284+
foreach ($this->children as $child) {
285+
$child->parent = $node;
286+
$node->addChild($child->getNode());
287+
}
259288
} else {
289+
if ($this->addDefaults) {
290+
throw new InvalidDefinitionException(
291+
sprintf('%s::addDefaultsIfNotSet() is not applicable to prototype nodes.', __CLASS__)
292+
);
293+
}
294+
260295
$node = new PrototypedArrayNode($this->name, $this->parent);
296+
297+
if (null !== $this->key) {
298+
$node->setKeyAttribute($this->key, $this->removeKeyItem);
299+
}
300+
301+
if (true === $this->atLeastOne) {
302+
$node->setMinNumberOfElements(1);
303+
}
304+
305+
if ($this->default) {
306+
$node->setDefaultValue($this->defaultValue);
307+
}
308+
309+
$this->prototype->parent = $node;
310+
$node->setPrototype($this->prototype->getNode());
261311
}
262312

263-
$node->setAddIfNotSet($this->addDefaults);
264313
$node->setAllowNewKeys($this->allowNewKeys);
265314
$node->addEquivalentValue(null, $this->nullEquivalent);
266315
$node->addEquivalentValue(true, $this->trueEquivalent);
@@ -283,28 +332,6 @@ protected function createNode()
283332
$node->setFinalValidationClosures($this->validation->rules);
284333
}
285334

286-
if (null == $this->prototype) {
287-
foreach ($this->children as $child) {
288-
$child->parent = $node;
289-
$node->addChild($child->getNode());
290-
}
291-
} else {
292-
if (null !== $this->key) {
293-
$node->setKeyAttribute($this->key, $this->removeKeyItem);
294-
}
295-
296-
if (true === $this->atLeastOne) {
297-
$node->setMinNumberOfElements(1);
298-
}
299-
300-
if (null !== $this->defaultValue) {
301-
$node->setDefaultValue($this->defaultValue);
302-
}
303-
304-
$this->prototype->parent = $node;
305-
$node->setPrototype($this->prototype->getNode());
306-
}
307-
308335
return $node;
309336
}
310337

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(NodeDefinition $node)
3535
}
3636

3737
/**
38-
* Mark the expression as being always used.
38+
* Marks the expression as being always used.
3939
*
4040
* @return ExprBuilder
4141
*/

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct()
3636
}
3737

3838
/**
39-
* Set the parent node
39+
* Set the parent node.
4040
*
4141
* @param ParentNodeDefinitionInterface $parent The parent node
4242
*
@@ -50,7 +50,7 @@ public function setParent(ParentNodeDefinitionInterface $parent = null)
5050
}
5151

5252
/**
53-
* Creates a child array node
53+
* Creates a child array node.
5454
*
5555
* @param string $name The name of the node
5656
*
@@ -130,12 +130,14 @@ public function node($name, $type)
130130
}
131131

132132
/**
133-
* Append a node definition.
133+
* Appends a node definition.
134+
*
135+
* Usage:
134136
*
135137
* $node = new ArrayNodeDefinition('name')
136138
* ->children()
137-
* ->scalarNode('foo')
138-
* ->scalarNode('baz')
139+
* ->scalarNode('foo')->end()
140+
* ->scalarNode('baz')->end()
139141
* ->append($this->getBarNodeDefinition())
140142
* ->end()
141143
* ;
@@ -160,7 +162,7 @@ public function append(NodeDefinition $node)
160162
}
161163

162164
/**
163-
* Add or override a node Type
165+
* Adds or overrides a node Type.
164166
*
165167
* @param string $type The name of the type
166168
* @param string $class The fully qualified name the node definition class
@@ -175,7 +177,7 @@ public function setNodeClass($type, $class)
175177
}
176178

177179
/**
178-
* Returns the class name of the node definition
180+
* Returns the class name of the node definition.
179181
*
180182
* @param string $type The node type
181183
*

0 commit comments

Comments
 (0)
0