[Config] replaced setInfo(), setExample() with more generic attributes

This commit is contained in:
Johannes M. Schmitt 2012-05-26 12:28:01 -05:00
parent 9e9519913d
commit 8775f2c17c
5 changed files with 64 additions and 27 deletions

View File

@ -56,7 +56,7 @@ class Configuration implements ConfigurationInterface
->arrayNode('resources') ->arrayNode('resources')
->addDefaultChildrenIfNoneSet() ->addDefaultChildrenIfNoneSet()
->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end() ->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
->setExample(array('MyBundle::form.html.twig')) ->example(array('MyBundle::form.html.twig'))
->validate() ->validate()
->ifTrue(function($v) { return !in_array('form_div_layout.html.twig', $v); }) ->ifTrue(function($v) { return !in_array('form_div_layout.html.twig', $v); })
->then(function($v){ ->then(function($v){
@ -77,7 +77,7 @@ class Configuration implements ConfigurationInterface
->children() ->children()
->arrayNode('globals') ->arrayNode('globals')
->useAttributeAsKey('key') ->useAttributeAsKey('key')
->setExample(array('foo' => '"@bar"', 'pi' => 3.14)) ->example(array('foo' => '"@bar"', 'pi' => 3.14))
->prototype('array') ->prototype('array')
->beforeNormalization() ->beforeNormalization()
->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); }) ->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
@ -117,7 +117,7 @@ class Configuration implements ConfigurationInterface
$rootNode $rootNode
->children() ->children()
->scalarNode('autoescape')->end() ->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('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
->scalarNode('charset')->defaultValue('%kernel.charset%')->end() ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
->scalarNode('debug')->defaultValue('%kernel.debug%')->end() ->scalarNode('debug')->defaultValue('%kernel.debug%')->end()

View File

@ -36,7 +36,7 @@ class Configuration implements ConfigurationInterface
$rootNode $rootNode
->children() ->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() ->booleanNode('toolbar')->defaultFalse()->end()
->scalarNode('position') ->scalarNode('position')
->defaultValue('bottom') ->defaultValue('bottom')

View File

@ -29,8 +29,7 @@ abstract class BaseNode implements NodeInterface
protected $allowOverwrite; protected $allowOverwrite;
protected $required; protected $required;
protected $equivalentValues; protected $equivalentValues;
protected $info; protected $attributes = array();
protected $example;
/** /**
* Constructor. * Constructor.
@ -55,14 +54,44 @@ abstract class BaseNode implements NodeInterface
$this->equivalentValues = array(); $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) public function setInfo($info)
{ {
$this->info = $info; $this->setAttribute('info', $info);
} }
/** /**
@ -72,7 +101,7 @@ abstract class BaseNode implements NodeInterface
*/ */
public function getInfo() public function getInfo()
{ {
return $this->info; return $this->getAttribute('info');
} }
/** /**
@ -82,7 +111,7 @@ abstract class BaseNode implements NodeInterface
*/ */
public function setExample($example) public function setExample($example)
{ {
$this->example = $example; $this->setAttribute('example', $example);
} }
/** /**
@ -92,7 +121,7 @@ abstract class BaseNode implements NodeInterface
*/ */
public function getExample() public function getExample()
{ {
return $this->example; return $this->getAttribute('example');
} }
/** /**

View File

@ -32,8 +32,7 @@ abstract class NodeDefinition implements NodeParentInterface
protected $trueEquivalent; protected $trueEquivalent;
protected $falseEquivalent; protected $falseEquivalent;
protected $parent; protected $parent;
protected $info; protected $attributes = array();
protected $example;
/** /**
* Constructor * Constructor
@ -72,11 +71,9 @@ abstract class NodeDefinition implements NodeParentInterface
* *
* @return NodeDefinition * @return NodeDefinition
*/ */
public function setInfo($info) public function info($info)
{ {
$this->info = $info; return $this->attribute('info', $info);
return $this;
} }
/** /**
@ -86,9 +83,22 @@ abstract class NodeDefinition implements NodeParentInterface
* *
* @return NodeDefinition * @return NodeDefinition
*/ */
public function setExample($example) public function example($example)
{ {
$this->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->attributes[$key] = $value;
return $this; return $this;
} }
@ -125,9 +135,7 @@ abstract class NodeDefinition implements NodeParentInterface
} }
$node = $this->createNode(); $node = $this->createNode();
$node->setAttributes($this->attributes);
$node->setInfo($this->info);
$node->setExample($this->example);
return $node; return $node;
} }

View File

@ -94,9 +94,9 @@ class TreeBuilderTest extends \PHPUnit_Framework_TestCase
{ {
$builder = new TreeBuilder(); $builder = new TreeBuilder();
$builder->root('test')->setInfo('root info') $builder->root('test')->info('root info')
->children() ->children()
->node('child', 'variable')->setInfo('child info')->defaultValue('default') ->node('child', 'variable')->info('child info')->defaultValue('default')
->end() ->end()
->end(); ->end();
@ -112,9 +112,9 @@ class TreeBuilderTest extends \PHPUnit_Framework_TestCase
$builder = new TreeBuilder(); $builder = new TreeBuilder();
$builder->root('test') $builder->root('test')
->setExample(array('key' => 'value')) ->example(array('key' => 'value'))
->children() ->children()
->node('child', 'variable')->setInfo('child info')->defaultValue('default')->setExample('example') ->node('child', 'variable')->info('child info')->defaultValue('default')->example('example')
->end() ->end()
->end(); ->end();