Skip to content

Commit

Permalink
[Config] replaced setInfo(), setExample() with more generic attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh committed May 26, 2012
1 parent 9e95199 commit 8775f2c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
Expand Up @@ -56,7 +56,7 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
->arrayNode('resources')
->addDefaultChildrenIfNoneSet()
->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
->setExample(array('MyBundle::form.html.twig'))
->example(array('MyBundle::form.html.twig'))
->validate()
->ifTrue(function($v) { return !in_array('form_div_layout.html.twig', $v); })
->then(function($v){
Expand All @@ -77,7 +77,7 @@ private function addGlobalsSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('globals')
->useAttributeAsKey('key')
->setExample(array('foo' => '"@bar"', 'pi' => 3.14))
->example(array('foo' => '"@bar"', 'pi' => 3.14))
->prototype('array')
->beforeNormalization()
->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
Expand Down Expand Up @@ -117,7 +117,7 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
$rootNode
->children()
->scalarNode('autoescape')->end()
->scalarNode('base_template_class')->setExample('Twig_Template')->end()
->scalarNode('base_template_class')->example('Twig_Template')->end()
->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
->scalarNode('debug')->defaultValue('%kernel.debug%')->end()
Expand Down
Expand Up @@ -36,7 +36,7 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->booleanNode('verbose')->defaultTrue()->setInfo('DEPRECATED, it is not useful anymore and can be removed safely from your configuration')->end()
->booleanNode('verbose')->defaultTrue()->info('DEPRECATED, it is not useful anymore and can be removed safely from your configuration')->end()
->booleanNode('toolbar')->defaultFalse()->end()
->scalarNode('position')
->defaultValue('bottom')
Expand Down
45 changes: 37 additions & 8 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Expand Up @@ -29,8 +29,7 @@ abstract class BaseNode implements NodeInterface
protected $allowOverwrite;
protected $required;
protected $equivalentValues;
protected $info;
protected $example;
protected $attributes = array();

/**
* Constructor.
Expand All @@ -55,14 +54,44 @@ public function __construct($name, NodeInterface $parent = null)
$this->equivalentValues = array();
}

public function setAttribute($key, $value)
{
$this->attributes[$key] = $value;
}

public function getAttribute($key, $default = null)
{
return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
}

public function hasAttribute($key)
{
return isset($this->attributes[$key]);
}

public function getAttributes()
{
return $this->attributes;
}

public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
}

public function removeAttribute($key)
{
unset($this->attributes[$key]);
}

/**
* Sets info message.
* Sets an info message.
*
* @param string $info The info text
* @param string $info
*/
public function setInfo($info)
{
$this->info = $info;
$this->setAttribute('info', $info);
}

/**
Expand All @@ -72,7 +101,7 @@ public function setInfo($info)
*/
public function getInfo()
{
return $this->info;
return $this->getAttribute('info');
}

/**
Expand All @@ -82,7 +111,7 @@ public function getInfo()
*/
public function setExample($example)
{
$this->example = $example;
$this->setAttribute('example', $example);
}

/**
Expand All @@ -92,7 +121,7 @@ public function setExample($example)
*/
public function getExample()
{
return $this->example;
return $this->getAttribute('example');
}

/**
Expand Down
30 changes: 19 additions & 11 deletions src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php
Expand Up @@ -32,8 +32,7 @@ abstract class NodeDefinition implements NodeParentInterface
protected $trueEquivalent;
protected $falseEquivalent;
protected $parent;
protected $info;
protected $example;
protected $attributes = array();

/**
* Constructor
Expand Down Expand Up @@ -72,11 +71,9 @@ public function setParent(NodeParentInterface $parent)
*
* @return NodeDefinition
*/
public function setInfo($info)
public function info($info)
{
$this->info = $info;

return $this;
return $this->attribute('info', $info);
}

/**
Expand All @@ -86,9 +83,22 @@ public function setInfo($info)
*
* @return NodeDefinition
*/
public function setExample($example)
public function example($example)
{
return $this->attribute('example', $example);
}

/**
* Sets an attribute on the node.
*
* @param string $key
* @param mixed $value
*
* @return NodeDefinition
*/
public function attribute($key, $value)
{
$this->example = $example;
$this->attributes[$key] = $value;

return $this;
}
Expand Down Expand Up @@ -125,9 +135,7 @@ public function getNode($forceRootNode = false)
}

$node = $this->createNode();

$node->setInfo($this->info);
$node->setExample($this->example);
$node->setAttributes($this->attributes);

return $node;
}
Expand Down
Expand Up @@ -94,9 +94,9 @@ public function testDefinitionInfoGetsTransferedToNode()
{
$builder = new TreeBuilder();

$builder->root('test')->setInfo('root info')
$builder->root('test')->info('root info')
->children()
->node('child', 'variable')->setInfo('child info')->defaultValue('default')
->node('child', 'variable')->info('child info')->defaultValue('default')
->end()
->end();

Expand All @@ -112,9 +112,9 @@ public function testDefinitionExampleGetsTransferedToNode()
$builder = new TreeBuilder();

$builder->root('test')
->setExample(array('key' => 'value'))
->example(array('key' => 'value'))
->children()
->node('child', 'variable')->setInfo('child info')->defaultValue('default')->setExample('example')
->node('child', 'variable')->info('child info')->defaultValue('default')->example('example')
->end()
->end();

Expand Down

0 comments on commit 8775f2c

Please sign in to comment.