vendor/symfony/src/Symfony/Component/Config/Definition/Builder/NodeBuilder.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Config\Definition\Builder;
       
    13 
       
    14 /**
       
    15  * This class provides a fluent interface for building a node.
       
    16  *
       
    17  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
       
    18  */
       
    19 class NodeBuilder implements NodeParentInterface
       
    20 {
       
    21     protected $parent;
       
    22     protected $nodeMapping;
       
    23 
       
    24     /**
       
    25      * Constructor
       
    26      *
       
    27      */
       
    28     public function __construct()
       
    29     {
       
    30         $this->nodeMapping = array(
       
    31             'variable'    => __NAMESPACE__.'\\VariableNodeDefinition',
       
    32             'scalar'      => __NAMESPACE__.'\\ScalarNodeDefinition',
       
    33             'boolean'     => __NAMESPACE__.'\\BooleanNodeDefinition',
       
    34             'array'       => __NAMESPACE__.'\\ArrayNodeDefinition',
       
    35         );
       
    36     }
       
    37 
       
    38     /**
       
    39      * Set the parent node
       
    40      *
       
    41      * @param ParentNodeDefinitionInterface $parent The parent node
       
    42      *
       
    43      * @return NodeBuilder This node builder
       
    44      */
       
    45     public function setParent(ParentNodeDefinitionInterface $parent = null)
       
    46     {
       
    47         $this->parent = $parent;
       
    48 
       
    49         return $this;
       
    50     }
       
    51 
       
    52     /**
       
    53      * Creates a child array node
       
    54      *
       
    55      * @param string $name The name of the node
       
    56      *
       
    57      * @return ArrayNodeDefinition The child node
       
    58      */
       
    59     public function arrayNode($name)
       
    60     {
       
    61         return $this->node($name, 'array');
       
    62     }
       
    63 
       
    64     /**
       
    65      * Creates a child scalar node.
       
    66      *
       
    67      * @param string $name the name of the node
       
    68      *
       
    69      * @return ScalarNodeDefinition The child node
       
    70      */
       
    71     public function scalarNode($name)
       
    72     {
       
    73         return $this->node($name, 'scalar');
       
    74     }
       
    75 
       
    76     /**
       
    77      * Creates a child Boolean node.
       
    78      *
       
    79      * @param string $name The name of the node
       
    80      *
       
    81      * @return BooleanNodeDefinition The child node
       
    82      */
       
    83     public function booleanNode($name)
       
    84     {
       
    85         return $this->node($name, 'boolean');
       
    86     }
       
    87 
       
    88     /**
       
    89      * Creates a child variable node.
       
    90      *
       
    91      * @param string $name The name of the node
       
    92      *
       
    93      * @return VariableNodeDefinition The builder of the child node
       
    94      */
       
    95     public function variableNode($name)
       
    96     {
       
    97         return $this->node($name, 'variable');
       
    98     }
       
    99 
       
   100     /**
       
   101      * Returns the parent node.
       
   102      *
       
   103      * @return ParentNodeDefinitionInterface The parent node
       
   104      */
       
   105     public function end()
       
   106     {
       
   107         return $this->parent;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Creates a child node.
       
   112      *
       
   113      * @param string $name The name of the node
       
   114      * @param string $type The type of the node
       
   115      *
       
   116      * @return NodeDefinition The child node
       
   117      *
       
   118      * @throws \RuntimeException When the node type is not registered
       
   119      * @throws \RuntimeException When the node class is not found
       
   120      */
       
   121     public function node($name, $type)
       
   122     {
       
   123         $class = $this->getNodeClass($type);
       
   124 
       
   125         $node = new $class($name);
       
   126 
       
   127         if ($node instanceof ParentNodeDefinitionInterface) {
       
   128             $builder = clone $this;
       
   129             $builder->setParent(null);
       
   130             $node->setBuilder($builder);
       
   131         }
       
   132 
       
   133         if (null !== $this->parent) {
       
   134             $this->parent->append($node);
       
   135             // Make this builder the node parent to allow for a fluid interface
       
   136             $node->setParent($this);
       
   137         }
       
   138 
       
   139         return $node;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Add or override a node Type
       
   144      *
       
   145      * @param string $type The name of the type
       
   146      * @param string $class The fully qualified name the node definition class
       
   147      */
       
   148     public function setNodeClass($type, $class)
       
   149     {
       
   150         $this->nodeMapping[strtolower($type)] = $class;
       
   151 
       
   152         return $this;
       
   153     }
       
   154 
       
   155     /**
       
   156      * Returns the class name of the node definition
       
   157      *
       
   158      * @param string $type The node type
       
   159      *
       
   160      * @return string The node definition class name
       
   161      *
       
   162      * @throws \RuntimeException When the node type is not registered
       
   163      * @throws \RuntimeException When the node class is not found
       
   164      */
       
   165     protected function getNodeClass($type)
       
   166     {
       
   167         $type = strtolower($type);
       
   168 
       
   169         if (!isset($this->nodeMapping[$type])) {
       
   170             throw new \RuntimeException(sprintf('The node type "%s" is not registered.', $type));
       
   171         }
       
   172 
       
   173         $class = $this->nodeMapping[$type];
       
   174 
       
   175         if (!class_exists($class)) {
       
   176             throw new \RuntimeException(sprintf('The node class "%s" does not exist.', $class));
       
   177         }
       
   178 
       
   179         return $class;
       
   180     }
       
   181 
       
   182 }