8000 [Config] Cleanup, error detection, fixes by vicb · Pull Request #3365 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] Cleanup, error detection, fixes #3365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,14 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
->fixXmlConfig('resource')
->children()
->arrayNode('resources')
->addDefaultsIfNotSet()
->defaultValue(array('FrameworkBundle:Form'))
->prototype('scalar')->end()
->validate()
->ifTrue(function($v) {return !in_array('FrameworkBundle:Form', $v); })
->then(function($v){
return array_merge(array('FrameworkBundle:Form'), $v);
})
->end()
->prototype('scalar')->end()
->end()
->end()
->end()
Expand All @@ -235,7 +234,6 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
->arrayNode('assets_base_urls')
->performNoDeepMerging()
->addDefaultsIfNotSet()
->defaultValue(array('http' => array(), 'ssl' => array()))
->beforeNormalization()
->ifTrue(function($v) { return !is_array($v); })
->then(function($v) { return array($v); })
Expand Down Expand Up @@ -290,7 +288,6 @@ private function addTemplatingSection(ArrayNodeDefinition $rootNode)
->arrayNode('base_urls')
->performNoDeepMerging()
->addDefaultsIfNotSet()
->defaultValue(array('http' => array(), 'ssl' => array()))
->beforeNormalization()
->ifTrue(function($v) { return !is_array($v); })
->then(function($v) { return array($v); })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,15 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
->fixXmlConfig('resource')
->children()
->arrayNode('resources')
->addDefaultsIfNotSet()
->defaultValue(array('form_div_layout.html.twig'))
->prototype('scalar')->end()
->setExample(array('MyBundle::form.html.twig'))
->validate()
->ifTrue(function($v) { return !in_array('form_div_layout.html.twig', $v); })
->then(function($v){
return array_merge(array('form_div_layout.html.twig'), $v);
})
->end()
->prototype('scalar')->end()
->end()
->end()
->end()
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct($name, NodeInterface $parent = null)
}

/**
* Sets info message
* Sets info message.
*
* @param string $info The info text
*/
Expand All @@ -66,7 +66,7 @@ public function setInfo($info)
}

/**
* Returns info message
* Returns info message.
*
* @return string The info text
*/
Expand All @@ -78,7 +78,7 @@ public function getInfo()
/**
* Sets the example configuration for this node.
*
* @param array $example
* @param string|array $example
*/
public function setExample($example)
{
Expand All @@ -88,7 +88,7 @@ public function setExample($example)
/**
* Retrieves the example configuration for this node.
*
* @return mixed The example
* @return string|array The example
*/
public function getExample()
{
Expand Down
A3E2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\Definition\ArrayNode;
< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker=" ">use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;

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

/**
* Set a custom children builder
* Sets a custom children builder.
*
* @param NodeBuilder $builder A custom NodeBuilder
*/
Expand All @@ -60,7 +61,7 @@ public function setBuilder(NodeBuilder $builder)
}

/**
* Returns a builder to add children nodes
* Returns a builder to add children nodes.
*
* @return NodeBuilder
*/
Expand All @@ -78,16 +79,16 @@ public function children()
*/
public function prototype($type)
{
$builder = $this->getNodeBuilder();
$this->prototype = $builder->node(null, $type);
$this->prototype->parent = $this;

return $this->prototype;
return $this->prototype = $this->getNodeBuilder()->node(null, $type)->setParent($this);
}

/**
* Adds the default value if the node is not set in the configuration.
*
* This method is applicable to concrete nodes only (not to prototype nodes).
* If this function has been called and the node is not set during the finalization
* phase, it's default value will be derived from its children default values.
*
* @return ArrayNodeDefinition
*/
public function addDefaultsIfNotSet()
Expand All @@ -100,6 +101,8 @@ public function addDefaultsIfNotSet()
/**
* Requires the node to have at least one element.
*
* This method is applicable to prototype nodes only.
*
* @return ArrayNodeDefinition
*/
public function requiresAtLeastOneElement()
Expand Down Expand Up @@ -139,7 +142,7 @@ public function fixXmlConfig($singular, $plural = null)
}

/**
* Set the attribute which value is to be used as key.
* Sets the attribute which value is to be used as key.
*
* This is useful when you have an indexed array that should be an
* associative array. You can select an item from within the array
Expand All @@ -148,17 +151,19 @@ public function fixXmlConfig($singular, $plural = null)
*
* array(
* array('id' => 'my_name', 'foo' => 'bar'),
* )
* );
*
* becomes
* becomes
*
* array(
* 'my_name' => array('foo' => 'bar'),
* )
* );
*
* If you'd like "'id' => 'my_name'" to still be present in the resulting
* array, then you can set the second argument of this method to false.
*
* This method is applicable to prototype nodes only.
*
* @param string $name The name of the key
* @param Boolean $removeKeyItem Whether or not the key item should be removed.
*
Expand Down Expand Up @@ -216,12 +221,12 @@ public function ignoreExtraKeys()
}

/**
* Append a node definition.
* Appends a node definition.
*
* $node = new ArrayNodeDefinition()
* ->children()
* ->scalarNode('foo')
* ->scalarNode('baz')
* ->scalarNode('foo')->end()
* ->scalarNode('baz')->end()
* ->end()
* ->append($this->getBarNodeDefinition())
* ;
Expand Down Expand Up @@ -254,13 +259,57 @@ protected function getNodeBuilder()
*/
protected function createNode()
{
if (null == $this->prototype) {
if (null === $this->prototype) {
if (null !== $this->key) {
throw new InvalidDefinitionException(
sprintf('%s::useAttributeAsKey() is not applicable to concrete nodes.', __CLASS__)
);
}

if (true === $this->atLeastOne) {
throw new InvalidDefinitionException(
sprintf('%s::requiresAtLeastOneElement() is not applicable to concrete nodes.', __CLASS__)
);
}

if ($this->default) {
throw new InvalidDefinitionException(
sprintf('%s::defaultValue() is not applicable to concrete nodes.', __CLASS__)
);
}

$node = new ArrayNode($this->name, $this->parent);
$node->setAddIfNotSet($this->addDefaults);

foreach ($this->children as $child) {
$child->parent = $node;
$node->addChild($child->getNode());
}
} else {
if ($this->addDefaults) {
throw new InvalidDefinitionException(
sprintf('%s::addDefaultsIfNotSet() is not applicable to prototype nodes.', __CLASS__)
);
}

$node = new PrototypedArrayNode($this->name, $this->parent);

if (null !== $this->key) {
$node->setKeyAttribute($this->key, $this->removeKeyItem);
}

if (true === $this->atLeastOne) {
$node->setMinNumberOfElements(1);
}

if ($this->default) {
$node->setDefaultValue($this->defaultValue);
}

$this->prototype->parent = $node;
$node->setPrototype($this->prototype->getNode());
}

$node->setAddIfNotSet($this->addDefaults);
$node->setAllowNewKeys($this->allowNewKeys);
$node->addEquivalentValue(null, $this->nullEquivalent);
$node->addEquivalentValue(true, $this->trueEquivalent);
Expand All @@ -283,28 +332,6 @@ protected function createNode()
$node->setFinalValidationClosures($this->validation->rules);
}

if (null == $this->prototype) {
foreach ($this->children as $child) {
$child->parent = $node;
$node->addChild($child->getNode());
}
} else {
if (null !== $this->key) {
$node->setKeyAttribute($this->key, $this->removeKeyItem);
}

if (true === $this->atLeastOne) {
$node->setMinNumberOfElements(1);
}

if (null !== $this->defaultValue) {
$node->setDefaultValue($this->defaultValue);
}

$this->prototype->parent = $node;
$node->setPrototype($this->prototype->getNode());
}

return $node;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(NodeDefinition $node)
}

/**
* Mark the expression as being always used.
* Marks the expression as being always used.
*
* @return ExprBuilder
*/
Expand Down
16 changes: 9 additions & 7 deletions src/Symfony/Component/Config/Definition/Builder/NodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct()
}

/**
* Set the parent node
* Set the parent node.
*
* @param ParentNodeDefinitionInterface $parent The parent node
*
Expand All @@ -50,7 +50,7 @@ public function setParent(ParentNodeDefinitionInterface $parent = null)
}

/**
* Creates a child array node
* Creates a child array node.
*
* @param string $name The name of the node
*
Expand Down Expand Up @@ -130,12 +130,14 @@ public function node($name, $type)
}

/**
* Append a node definition.
* Appends a node definition.
*
* Usage:
*
* $node = new ArrayNodeDefinition('name')
* ->children()
* ->scalarNode('foo')
* ->scalarNode('baz')
* ->scalarNode('foo')->end()
* ->scalarNode('baz')->end()
* ->append($this->getBarNodeDefinition())
* ->end()
* ;
Expand All @@ -160,7 +162,7 @@ public function append(NodeDefinition $node)
}

/**
* Add or override a node Type
* Adds or overrides a node Type.
*
* @param string $type The name of the type
* @param string $class The fully qualified name the node definition class
Expand All @@ -175,7 +177,7 @@ public function setNodeClass($type, $class)
}

/**
* Returns the class name of the node definition
* Returns the class name of the node definition.
*
* @param string $type The node type
*
Expand Down
Loading
0