vendor/symfony/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.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 is the entry class for building a config tree.
       
    16  *
       
    17  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
       
    18  */
       
    19 class TreeBuilder implements NodeParentInterface
       
    20 {
       
    21     protected $tree;
       
    22     protected $root;
       
    23     protected $builder;
       
    24 
       
    25     /**
       
    26      * Creates the root node.
       
    27      *
       
    28      * @param string      $name     The name of the root node
       
    29      * @param string      $type     The type of the root node
       
    30      * @param NodeBuilder $builder  A custom node builder instance
       
    31      *
       
    32      * @return ArrayNodeDefinition|NodeDefinition The root node (as an ArrayNodeDefinition when the type is 'array')
       
    33      *
       
    34      * @throws \RuntimeException When the node type is not supported
       
    35      */
       
    36     public function root($name, $type = 'array', NodeBuilder $builder = null)
       
    37     {
       
    38         $builder = null === $builder ? new NodeBuilder() : $builder;
       
    39 
       
    40         $this->root = $builder->node($name, $type);
       
    41         $this->root->setParent($this);
       
    42 
       
    43         return $this->root;
       
    44     }
       
    45 
       
    46     /**
       
    47      * Builds the tree.
       
    48      *
       
    49      * @return NodeInterface
       
    50      */
       
    51     public function buildTree()
       
    52     {
       
    53         if (null === $this->root) {
       
    54             throw new \RuntimeException('The configuration tree has no root node.');
       
    55         }
       
    56         if (null !== $this->tree) {
       
    57             return $this->tree;
       
    58         }
       
    59 
       
    60         return $this->tree = $this->root->getNode(true);
       
    61     }
       
    62 }