123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- namespace Symfony\Component\Config\Definition\Builder;
- class NodeBuilder implements NodeParentInterface
- {
- protected $parent;
- protected $nodeMapping;
- public function __construct()
- {
- $this->nodeMapping = array(
- 'variable' => __NAMESPACE__.'\\VariableNodeDefinition',
- 'scalar' => __NAMESPACE__.'\\ScalarNodeDefinition',
- 'boolean' => __NAMESPACE__.'\\BooleanNodeDefinition',
- 'integer' => __NAMESPACE__.'\\IntegerNodeDefinition',
- 'float' => __NAMESPACE__.'\\FloatNodeDefinition',
- 'array' => __NAMESPACE__.'\\ArrayNodeDefinition',
- 'enum' => __NAMESPACE__.'\\EnumNodeDefinition',
- );
- }
-
- public function setParent(ParentNodeDefinitionInterface $parent = null)
- {
- $this->parent = $parent;
- return $this;
- }
-
- public function arrayNode($name)
- {
- return $this->node($name, 'array');
- }
-
- public function scalarNode($name)
- {
- return $this->node($name, 'scalar');
- }
-
- public function booleanNode($name)
- {
- return $this->node($name, 'boolean');
- }
-
- public function integerNode($name)
- {
- return $this->node($name, 'integer');
- }
-
- public function floatNode($name)
- {
- return $this->node($name, 'float');
- }
-
- public function enumNode($name)
- {
- return $this->node($name, 'enum');
- }
-
- public function variableNode($name)
- {
- return $this->node($name, 'variable');
- }
-
- public function end()
- {
- return $this->parent;
- }
-
- public function node($name, $type)
- {
- $class = $this->getNodeClass($type);
- $node = new $class($name);
- $this->append($node);
- return $node;
- }
-
- public function append(NodeDefinition $node)
- {
- if ($node instanceof ParentNodeDefinitionInterface) {
- $builder = clone $this;
- $builder->setParent(null);
- $node->setBuilder($builder);
- }
- if (null !== $this->parent) {
- $this->parent->append($node);
-
- $node->setParent($this);
- }
- return $this;
- }
-
- public function setNodeClass($type, $class)
- {
- $this->nodeMapping[strtolower($type)] = $class;
- return $this;
- }
-
- protected function getNodeClass($type)
- {
- $type = strtolower($type);
- if (!isset($this->nodeMapping[$type])) {
- throw new \RuntimeException(sprintf('The node type "%s" is not registered.', $type));
- }
- $class = $this->nodeMapping[$type];
- if (!class_exists($class)) {
- throw new \RuntimeException(sprintf('The node class "%s" does not exist.', $class));
- }
- return $class;
- }
- }
|