4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Definition\Builder;
15 * This class provides a fluent interface for building a node.
17 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
19 class NodeBuilder implements NodeParentInterface
22 protected $nodeMapping;
27 public function __construct()
29 $this->nodeMapping = array(
30 'variable' => __NAMESPACE__.'\\VariableNodeDefinition',
31 'scalar' => __NAMESPACE__.'\\ScalarNodeDefinition',
32 'boolean' => __NAMESPACE__.'\\BooleanNodeDefinition',
33 'integer' => __NAMESPACE__.'\\IntegerNodeDefinition',
34 'float' => __NAMESPACE__.'\\FloatNodeDefinition',
35 'array' => __NAMESPACE__.'\\ArrayNodeDefinition',
36 'enum' => __NAMESPACE__.'\\EnumNodeDefinition',
41 * Set the parent node.
43 * @param ParentNodeDefinitionInterface $parent The parent node
47 public function setParent(ParentNodeDefinitionInterface $parent = null)
49 $this->parent = $parent;
55 * Creates a child array node.
57 * @param string $name The name of the node
59 * @return ArrayNodeDefinition The child node
61 public function arrayNode($name)
63 return $this->node($name, 'array');
67 * Creates a child scalar node.
69 * @param string $name the name of the node
71 * @return ScalarNodeDefinition The child node
73 public function scalarNode($name)
75 return $this->node($name, 'scalar');
79 * Creates a child Boolean node.
81 * @param string $name The name of the node
83 * @return BooleanNodeDefinition The child node
85 public function booleanNode($name)
87 return $this->node($name, 'boolean');
91 * Creates a child integer node.
93 * @param string $name the name of the node
95 * @return IntegerNodeDefinition The child node
97 public function integerNode($name)
99 return $this->node($name, 'integer');
103 * Creates a child float node.
105 * @param string $name the name of the node
107 * @return FloatNodeDefinition The child node
109 public function floatNode($name)
111 return $this->node($name, 'float');
115 * Creates a child EnumNode.
117 * @param string $name
119 * @return EnumNodeDefinition
121 public function enumNode($name)
123 return $this->node($name, 'enum');
127 * Creates a child variable node.
129 * @param string $name The name of the node
131 * @return VariableNodeDefinition The builder of the child node
133 public function variableNode($name)
135 return $this->node($name, 'variable');
139 * Returns the parent node.
141 * @return ParentNodeDefinitionInterface|NodeDefinition The parent node
143 public function end()
145 return $this->parent;
149 * Creates a child node.
151 * @param string $name The name of the node
152 * @param string $type The type of the node
154 * @return NodeDefinition The child node
156 * @throws \RuntimeException When the node type is not registered
157 * @throws \RuntimeException When the node class is not found
159 public function node($name, $type)
161 $class = $this->getNodeClass($type);
163 $node = new $class($name);
165 $this->append($node);
171 * Appends a node definition.
175 * $node = new ArrayNodeDefinition('name')
177 * ->scalarNode('foo')->end()
178 * ->scalarNode('baz')->end()
179 * ->append($this->getBarNodeDefinition())
183 * @param NodeDefinition $node
187 public function append(NodeDefinition $node)
189 if ($node instanceof ParentNodeDefinitionInterface) {
190 $builder = clone $this;
191 $builder->setParent(null);
192 $node->setBuilder($builder);
195 if (null !== $this->parent) {
196 $this->parent->append($node);
197 // Make this builder the node parent to allow for a fluid interface
198 $node->setParent($this);
205 * Adds or overrides a node Type.
207 * @param string $type The name of the type
208 * @param string $class The fully qualified name the node definition class
212 public function setNodeClass($type, $class)
214 $this->nodeMapping[strtolower($type)] = $class;
220 * Returns the class name of the node definition.
222 * @param string $type The node type
224 * @return string The node definition class name
226 * @throws \RuntimeException When the node type is not registered
227 * @throws \RuntimeException When the node class is not found
229 protected function getNodeClass($type)
231 $type = strtolower($type);
233 if (!isset($this->nodeMapping[$type])) {
234 throw new \RuntimeException(sprintf('The node type "%s" is not registered.', $type));
237 $class = $this->nodeMapping[$type];
239 if (!class_exists($class)) {
240 throw new \RuntimeException(sprintf('The node class "%s" does not exist.', $class));