[Config] Component refactoring
The Config component API have changed and the extension configuration files must be updated accordingly:
1. Array nodes must enclosed their children definition in ->children() ... ->end() calls:
Before:
$treeBuilder->root('zend', 'array')
->arrayNode('logger')
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end();
After:
$treeBuilder->root('zend', 'array')
->children()
->arrayNode('logger')
->children()
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end()
->end()
->end();
2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition)
Before:
$treeBuilder->root('doctrine', 'array')
->arrayNode('dbal')
->builder($this->getDbalConnectionsNode())
->end();
After:
$treeBuilder->root('doctrine', 'array')
->children()
->arrayNode('dbal')
->append($this->getDbalConnectionsNode())
->end()
->end();
3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition):
Before:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(NodeBuilder $node)
{
...
}
After:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(ArrayNodeDefinition $node)
{
...
}
4. The NodeBuilder API has changed (this is seldom used):
Before:
$node = new NodeBuilder('connections', 'array');
After:
The recommended way is to use a tree builder:
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('connections', 'array');
An other way would be:
$builder = new NodeBuilder();
$node = $builder->node('connections', 'array');
Some notes:
- Tree root nodes should most always be array nodes, so this as been made the default:
$treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine')
- There could be more than one ->children() ... ->end() sections. This could help with the readability:
$treeBuilder->root('doctrine')
->children()
->scalarNode('default_connection')->end()
->end()
->fixXmlConfig('type')
->children()
->arrayNode('types')
....
->end()
->end()
2011-03-14 17:29:56 +00:00
|
|
|
<?php
|
|
|
|
|
2011-05-31 09:57:06 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
[Config] Component refactoring
The Config component API have changed and the extension configuration files must be updated accordingly:
1. Array nodes must enclosed their children definition in ->children() ... ->end() calls:
Before:
$treeBuilder->root('zend', 'array')
->arrayNode('logger')
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end();
After:
$treeBuilder->root('zend', 'array')
->children()
->arrayNode('logger')
->children()
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end()
->end()
->end();
2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition)
Before:
$treeBuilder->root('doctrine', 'array')
->arrayNode('dbal')
->builder($this->getDbalConnectionsNode())
->end();
After:
$treeBuilder->root('doctrine', 'array')
->children()
->arrayNode('dbal')
->append($this->getDbalConnectionsNode())
->end()
->end();
3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition):
Before:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(NodeBuilder $node)
{
...
}
After:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(ArrayNodeDefinition $node)
{
...
}
4. The NodeBuilder API has changed (this is seldom used):
Before:
$node = new NodeBuilder('connections', 'array');
After:
The recommended way is to use a tree builder:
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('connections', 'array');
An other way would be:
$builder = new NodeBuilder();
$node = $builder->node('connections', 'array');
Some notes:
- Tree root nodes should most always be array nodes, so this as been made the default:
$treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine')
- There could be more than one ->children() ... ->end() sections. This could help with the readability:
$treeBuilder->root('doctrine')
->children()
->scalarNode('default_connection')->end()
->end()
->fixXmlConfig('type')
->children()
->arrayNode('types')
....
->end()
->end()
2011-03-14 17:29:56 +00:00
|
|
|
namespace Symfony\Tests\Component\Config\Definition\Builder;
|
|
|
|
|
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
|
|
|
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
|
2012-02-20 22:07:03 +00:00
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
|
[Config] Component refactoring
The Config component API have changed and the extension configuration files must be updated accordingly:
1. Array nodes must enclosed their children definition in ->children() ... ->end() calls:
Before:
$treeBuilder->root('zend', 'array')
->arrayNode('logger')
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end();
After:
$treeBuilder->root('zend', 'array')
->children()
->arrayNode('logger')
->children()
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end()
->end()
->end();
2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition)
Before:
$treeBuilder->root('doctrine', 'array')
->arrayNode('dbal')
->builder($this->getDbalConnectionsNode())
->end();
After:
$treeBuilder->root('doctrine', 'array')
->children()
->arrayNode('dbal')
->append($this->getDbalConnectionsNode())
->end()
->end();
3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition):
Before:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(NodeBuilder $node)
{
...
}
After:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(ArrayNodeDefinition $node)
{
...
}
4. The NodeBuilder API has changed (this is seldom used):
Before:
$node = new NodeBuilder('connections', 'array');
After:
The recommended way is to use a tree builder:
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('connections', 'array');
An other way would be:
$builder = new NodeBuilder();
$node = $builder->node('connections', 'array');
Some notes:
- Tree root nodes should most always be array nodes, so this as been made the default:
$treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine')
- There could be more than one ->children() ... ->end() sections. This could help with the readability:
$treeBuilder->root('doctrine')
->children()
->scalarNode('default_connection')->end()
->end()
->fixXmlConfig('type')
->children()
->arrayNode('types')
....
->end()
->end()
2011-03-14 17:29:56 +00:00
|
|
|
|
|
|
|
class ArrayNodeDefinitionTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testAppendingSomeNode()
|
|
|
|
{
|
|
|
|
$parent = new ArrayNodeDefinition('root');
|
|
|
|
$child = new ScalarNodeDefinition('child');
|
|
|
|
|
2012-02-20 22:07:03 +00:00
|
|
|
$parent
|
[Config] Component refactoring
The Config component API have changed and the extension configuration files must be updated accordingly:
1. Array nodes must enclosed their children definition in ->children() ... ->end() calls:
Before:
$treeBuilder->root('zend', 'array')
->arrayNode('logger')
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end();
After:
$treeBuilder->root('zend', 'array')
->children()
->arrayNode('logger')
->children()
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end()
->end()
->end();
2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition)
Before:
$treeBuilder->root('doctrine', 'array')
->arrayNode('dbal')
->builder($this->getDbalConnectionsNode())
->end();
After:
$treeBuilder->root('doctrine', 'array')
->children()
->arrayNode('dbal')
->append($this->getDbalConnectionsNode())
->end()
->end();
3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition):
Before:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(NodeBuilder $node)
{
...
}
After:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(ArrayNodeDefinition $node)
{
...
}
4. The NodeBuilder API has changed (this is seldom used):
Before:
$node = new NodeBuilder('connections', 'array');
After:
The recommended way is to use a tree builder:
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('connections', 'array');
An other way would be:
$builder = new NodeBuilder();
$node = $builder->node('connections', 'array');
Some notes:
- Tree root nodes should most always be array nodes, so this as been made the default:
$treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine')
- There could be more than one ->children() ... ->end() sections. This could help with the readability:
$treeBuilder->root('doctrine')
->children()
->scalarNode('default_connection')->end()
->end()
->fixXmlConfig('type')
->children()
->arrayNode('types')
....
->end()
->end()
2011-03-14 17:29:56 +00:00
|
|
|
->children()
|
|
|
|
->scalarNode('foo')->end()
|
|
|
|
->scalarNode('bar')->end()
|
|
|
|
->end()
|
|
|
|
->append($child);
|
|
|
|
|
2011-12-17 14:22:10 +00:00
|
|
|
$this->assertCount(3, $this->getField($parent, 'children'));
|
[Config] Component refactoring
The Config component API have changed and the extension configuration files must be updated accordingly:
1. Array nodes must enclosed their children definition in ->children() ... ->end() calls:
Before:
$treeBuilder->root('zend', 'array')
->arrayNode('logger')
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end();
After:
$treeBuilder->root('zend', 'array')
->children()
->arrayNode('logger')
->children()
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end()
->end()
->end();
2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition)
Before:
$treeBuilder->root('doctrine', 'array')
->arrayNode('dbal')
->builder($this->getDbalConnectionsNode())
->end();
After:
$treeBuilder->root('doctrine', 'array')
->children()
->arrayNode('dbal')
->append($this->getDbalConnectionsNode())
->end()
->end();
3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition):
Before:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(NodeBuilder $node)
{
...
}
After:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(ArrayNodeDefinition $node)
{
...
}
4. The NodeBuilder API has changed (this is seldom used):
Before:
$node = new NodeBuilder('connections', 'array');
After:
The recommended way is to use a tree builder:
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('connections', 'array');
An other way would be:
$builder = new NodeBuilder();
$node = $builder->node('connections', 'array');
Some notes:
- Tree root nodes should most always be array nodes, so this as been made the default:
$treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine')
- There could be more than one ->children() ... ->end() sections. This could help with the readability:
$treeBuilder->root('doctrine')
->children()
->scalarNode('default_connection')->end()
->end()
->fixXmlConfig('type')
->children()
->arrayNode('types')
....
->end()
->end()
2011-03-14 17:29:56 +00:00
|
|
|
$this->assertTrue(in_array($child, $this->getField($parent, 'children')));
|
|
|
|
}
|
|
|
|
|
2012-02-14 23:48:14 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
|
|
|
|
* @dataProvider providePrototypeNodeSpecificCalls
|
|
|
|
*/
|
|
|
|
public function testPrototypeNodeSpecificOption($method, $args)
|
|
|
|
{
|
|
|
|
$node = new ArrayNodeDefinition('root');
|
|
|
|
|
2012-02-16 06:27:52 +00:00
|
|
|
call_user_func_array(array($node, $method), $args);
|
2012-02-14 23:48:14 +00:00
|
|
|
|
|
|
|
$node->getNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providePrototypeNodeSpecificCalls()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('defaultValue', array(array())),
|
2012-02-20 18:15:01 +00:00
|
|
|
array('addDefaultChildrenIfNoneSet', array()),
|
2012-02-14 23:48:14 +00:00
|
|
|
array('requiresAtLeastOneElement', array()),
|
|
|
|
array('useAttributeAsKey', array('foo'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
|
|
|
|
*/
|
|
|
|
public function testConcreteNodeSpecificOption()
|
|
|
|
{
|
|
|
|
$node = new ArrayNodeDefinition('root');
|
|
|
|
$node->addDefaultsIfNotSet()->prototype('array');
|
|
|
|
$node->getNode();
|
|
|
|
}
|
|
|
|
|
2012-02-16 12:55:25 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
|
|
|
|
*/
|
|
|
|
public function testPrototypeNodesCantHaveADefaultValueWhenUsingDefaulChildren()
|
|
|
|
{
|
|
|
|
$node = new ArrayNodeDefinition('root');
|
|
|
|
$node
|
|
|
|
->defaultValue(array())
|
2012-02-20 18:15:01 +00:00
|
|
|
->addDefaultChildrenIfNoneSet('foo')
|
2012-02-16 12:55:25 +00:00
|
|
|
->prototype('array')
|
|
|
|
;
|
|
|
|
$node->getNode();
|
|
|
|
}
|
|
|
|
|
2012-02-20 22:07:03 +00:00
|
|
|
public function testPrototypedArrayNodeDefaultWhenUsingDefaultChildren()
|
2012-02-16 12:55:25 +00:00
|
|
|
{
|
|
|
|
$node = new ArrayNodeDefinition('root');
|
|
|
|
$node
|
2012-02-20 18:15:01 +00:00
|
|
|
->addDefaultChildrenIfNoneSet()
|
2012-02-16 12:55:25 +00:00
|
|
|
->prototype('array')
|
|
|
|
;
|
|
|
|
$tree = $node->getNode();
|
|
|
|
$this->assertEquals(array(array()), $tree->getDefaultValue());
|
|
|
|
}
|
|
|
|
|
2012-02-20 22:07:03 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providePrototypedArrayNodeDefaults
|
|
|
|
*/
|
|
|
|
public function testPrototypedArrayNodeDefault($args, $shouldThrowWhenUsingAttrAsKey, $shouldThrowWhenNotUsingAttrAsKey, $defaults)
|
|
|
|
{
|
|
|
|
$node = new ArrayNodeDefinition('root');
|
|
|
|
$node
|
|
|
|
->addDefaultChildrenIfNoneSet($args)
|
|
|
|
->prototype('array')
|
|
|
|
;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tree = $node->getNode();
|
|
|
|
$this->assertFalse($shouldThrowWhenNotUsingAttrAsKey);
|
|
|
|
$this->assertEquals($defaults, $tree->getDefaultValue());
|
|
|
|
} catch (InvalidDefinitionException $e) {
|
|
|
|
$this->assertTrue($shouldThrowWhenNotUsingAttrAsKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
$node = new ArrayNodeDefinition('root');
|
|
|
|
$node
|
|
|
|
->useAttributeAsKey('attr')
|
|
|
|
->addDefaultChildrenIfNoneSet($args)
|
|
|
|
->prototype('array')
|
|
|
|
;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tree = $node->getNode();
|
|
|
|
$this->assertFalse($shouldThrowWhenUsingAttrAsKey);
|
|
|
|
$this->assertEquals($defaults, $tree->getDefaultValue());
|
|
|
|
} catch (InvalidDefinitionException $e) {
|
|
|
|
$this->assertTrue($shouldThrowWhenUsingAttrAsKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providePrototypedArrayNodeDefaults()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(null, true, false, array(array())),
|
|
|
|
array(2, true, false, array(array(), array())),
|
|
|
|
array('2', false, true, array('2' => array())),
|
|
|
|
array('foo', false, true, array('foo' => array())),
|
|
|
|
array(array('foo'), false, true, array('foo' => array())),
|
|
|
|
array(array('foo', 'bar'), false, true, array('foo' => array(), 'bar' => array())),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-20 17:01:14 +00:00
|
|
|
public function testNestedPrototypedArrayNodes()
|
|
|
|
{
|
|
|
|
$node = new ArrayNodeDefinition('root');
|
|
|
|
$node
|
2012-02-20 18:15:01 +00:00
|
|
|
->addDefaultChildrenIfNoneSet()
|
2012-02-20 17:01:14 +00:00
|
|
|
->prototype('array')
|
|
|
|
->prototype('array')
|
|
|
|
;
|
2012-02-20 22:07:03 +00:00
|
|
|
$node->getNode();
|
2012-02-20 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
[Config] Component refactoring
The Config component API have changed and the extension configuration files must be updated accordingly:
1. Array nodes must enclosed their children definition in ->children() ... ->end() calls:
Before:
$treeBuilder->root('zend', 'array')
->arrayNode('logger')
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end();
After:
$treeBuilder->root('zend', 'array')
->children()
->arrayNode('logger')
->children()
->scalarNode('priority')->defaultValue('INFO')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end()
->end()
->end();
2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition)
Before:
$treeBuilder->root('doctrine', 'array')
->arrayNode('dbal')
->builder($this->getDbalConnectionsNode())
->end();
After:
$treeBuilder->root('doctrine', 'array')
->children()
->arrayNode('dbal')
->append($this->getDbalConnectionsNode())
->end()
->end();
3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition):
Before:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(NodeBuilder $node)
{
...
}
After:
$root = $treeBuilder->root('doctrine', 'array');
$this->addDbalSection($root);
public function addDbalSection(ArrayNodeDefinition $node)
{
...
}
4. The NodeBuilder API has changed (this is seldom used):
Before:
$node = new NodeBuilder('connections', 'array');
After:
The recommended way is to use a tree builder:
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('connections', 'array');
An other way would be:
$builder = new NodeBuilder();
$node = $builder->node('connections', 'array');
Some notes:
- Tree root nodes should most always be array nodes, so this as been made the default:
$treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine')
- There could be more than one ->children() ... ->end() sections. This could help with the readability:
$treeBuilder->root('doctrine')
->children()
->scalarNode('default_connection')->end()
->end()
->fixXmlConfig('type')
->children()
->arrayNode('types')
....
->end()
->end()
2011-03-14 17:29:56 +00:00
|
|
|
protected function getField($object, $field)
|
|
|
|
{
|
|
|
|
$reflection = new \ReflectionProperty($object, $field);
|
|
|
|
$reflection->setAccessible(true);
|
|
|
|
|
|
|
|
return $reflection->getValue($object);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|