vendor/symfony/config/Definition/Builder/TreeBuilder.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Config\Definition\Builder;
  11. use Symfony\Component\Config\Definition\Exception\TreeWithoutRootNodeException;
  12. use Symfony\Component\Config\Definition\NodeInterface;
  13. /**
  14.  * This is the entry class for building a config tree.
  15.  *
  16.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17.  */
  18. class TreeBuilder implements NodeParentInterface
  19. {
  20.     protected $tree;
  21.     protected $root;
  22.     public function __construct(string $name nullstring $type 'array'NodeBuilder $builder null)
  23.     {
  24.         if (null === $name) {
  25.             @trigger_error('A tree builder without a root node is deprecated since Symfony 4.2 and will not be supported anymore in 5.0.'E_USER_DEPRECATED);
  26.         } else {
  27.             $this->root($name$type$builder);
  28.         }
  29.     }
  30.     /**
  31.      * Creates the root node.
  32.      *
  33.      * @param string      $name    The name of the root node
  34.      * @param string      $type    The type of the root node
  35.      * @param NodeBuilder $builder A custom node builder instance
  36.      *
  37.      * @return ArrayNodeDefinition|NodeDefinition The root node (as an ArrayNodeDefinition when the type is 'array')
  38.      *
  39.      * @throws \RuntimeException When the node type is not supported
  40.      */
  41.     public function root($name$type 'array'NodeBuilder $builder null)
  42.     {
  43.         $builder $builder ?: new NodeBuilder();
  44.         return $this->root $builder->node($name$type)->setParent($this);
  45.     }
  46.     public function getRootNode(): NodeDefinition
  47.     {
  48.         if (null === $this->root) {
  49.             throw new \RuntimeException(sprintf('Calling %s() before creating the root node is not supported, migrate to the new constructor signature instead.'__METHOD__));
  50.         }
  51.         return $this->root;
  52.     }
  53.     /**
  54.      * Builds the tree.
  55.      *
  56.      * @return NodeInterface
  57.      *
  58.      * @throws \RuntimeException
  59.      */
  60.     public function buildTree()
  61.     {
  62.         $this->assertTreeHasRootNode();
  63.         if (null !== $this->tree) {
  64.             return $this->tree;
  65.         }
  66.         return $this->tree $this->root->getNode(true);
  67.     }
  68.     public function setPathSeparator(string $separator)
  69.     {
  70.         $this->assertTreeHasRootNode();
  71.         // unset last built as changing path separator changes all nodes
  72.         $this->tree null;
  73.         $this->root->setPathSeparator($separator);
  74.     }
  75.     /**
  76.      * @throws \RuntimeException if root node is not defined
  77.      */
  78.     private function assertTreeHasRootNode()
  79.     {
  80.         if (null === $this->root) {
  81.             throw new TreeWithoutRootNodeException('The configuration tree has no root node.');
  82.         }
  83.     }
  84. }