From 8775f2c17ce8da1ae89269ddf2692a0c79ec7f3e Mon Sep 17 00:00:00 2001 From: "Johannes M. Schmitt" Date: Sat, 26 May 2012 12:28:01 -0500 Subject: [PATCH] [Config] replaced setInfo(), setExample() with more generic attributes --- .../DependencyInjection/Configuration.php | 6 +-- .../DependencyInjection/Configuration.php | 2 +- .../Component/Config/Definition/BaseNode.php | 45 +++++++++++++++---- .../Definition/Builder/NodeDefinition.php | 30 ++++++++----- .../Definition/Builder/TreeBuilderTest.php | 8 ++-- 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php index 5f5812f36c..ff2a9ef0e1 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php @@ -56,7 +56,7 @@ class Configuration implements ConfigurationInterface ->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){ @@ -77,7 +77,7 @@ class Configuration implements ConfigurationInterface ->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, '@'); }) @@ -117,7 +117,7 @@ class Configuration implements ConfigurationInterface $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() diff --git a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php index c5e30f360b..6ba5e1a563 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php @@ -36,7 +36,7 @@ class Configuration implements ConfigurationInterface $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') diff --git a/src/Symfony/Component/Config/Definition/BaseNode.php b/src/Symfony/Component/Config/Definition/BaseNode.php index cd3a72cd76..91d5175d09 100644 --- a/src/Symfony/Component/Config/Definition/BaseNode.php +++ b/src/Symfony/Component/Config/Definition/BaseNode.php @@ -29,8 +29,7 @@ abstract class BaseNode implements NodeInterface protected $allowOverwrite; protected $required; protected $equivalentValues; - protected $info; - protected $example; + protected $attributes = array(); /** * Constructor. @@ -55,14 +54,44 @@ abstract class BaseNode implements NodeInterface $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); } /** @@ -72,7 +101,7 @@ abstract class BaseNode implements NodeInterface */ public function getInfo() { - return $this->info; + return $this->getAttribute('info'); } /** @@ -82,7 +111,7 @@ abstract class BaseNode implements NodeInterface */ public function setExample($example) { - $this->example = $example; + $this->setAttribute('example', $example); } /** @@ -92,7 +121,7 @@ abstract class BaseNode implements NodeInterface */ public function getExample() { - return $this->example; + return $this->getAttribute('example'); } /** diff --git a/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php b/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php index ef3a1b5a62..614ceff3fc 100644 --- a/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php +++ b/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php @@ -32,8 +32,7 @@ abstract class NodeDefinition implements NodeParentInterface protected $trueEquivalent; protected $falseEquivalent; protected $parent; - protected $info; - protected $example; + protected $attributes = array(); /** * Constructor @@ -72,11 +71,9 @@ abstract class NodeDefinition implements NodeParentInterface * * @return NodeDefinition */ - public function setInfo($info) + public function info($info) { - $this->info = $info; - - return $this; + return $this->attribute('info', $info); } /** @@ -86,9 +83,22 @@ abstract class NodeDefinition implements NodeParentInterface * * @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; } @@ -125,9 +135,7 @@ abstract class NodeDefinition implements NodeParentInterface } $node = $this->createNode(); - - $node->setInfo($this->info); - $node->setExample($this->example); + $node->setAttributes($this->attributes); return $node; } diff --git a/src/Symfony/Component/Config/Tests/Definition/Builder/TreeBuilderTest.php b/src/Symfony/Component/Config/Tests/Definition/Builder/TreeBuilderTest.php index 35aac24b1e..ca2b0e637f 100644 --- a/src/Symfony/Component/Config/Tests/Definition/Builder/TreeBuilderTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/Builder/TreeBuilderTest.php @@ -94,9 +94,9 @@ class TreeBuilderTest extends \PHPUnit_Framework_TestCase { $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(); @@ -112,9 +112,9 @@ class TreeBuilderTest extends \PHPUnit_Framework_TestCase $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();