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

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.             $builder $builder ?? new NodeBuilder();
  28.             $this->root $builder->node($name$type)->setParent($this);
  29.         }
  30.     }
  31.     /**
  32.      * Creates the root node.
  33.      *
  34.      * @param string $name The name of the root node
  35.      * @param string $type The type of the root node
  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.      * @deprecated since Symfony 4.3, pass the root name to the constructor instead
  42.      */
  43.     public function root($name$type 'array'NodeBuilder $builder null)
  44.     {
  45.         @trigger_error(sprintf('The "%s()" method called for the "%s" configuration is deprecated since Symfony 4.3, pass the root name to the constructor instead.'__METHOD__$name), \E_USER_DEPRECATED);
  46.         $builder $builder ?? new NodeBuilder();
  47.         return $this->root $builder->node($name$type)->setParent($this);
  48.     }
  49.     /**
  50.      * @return NodeDefinition|ArrayNodeDefinition The root node (as an ArrayNodeDefinition when the type is 'array')
  51.      */
  52.     public function getRootNode(): NodeDefinition
  53.     {
  54.         if (null === $this->root) {
  55.             throw new \RuntimeException(sprintf('Calling "%s()" before creating the root node is not supported, migrate to the new constructor signature instead.'__METHOD__));
  56.         }
  57.         return $this->root;
  58.     }
  59.     /**
  60.      * Builds the tree.
  61.      *
  62.      * @return NodeInterface
  63.      *
  64.      * @throws \RuntimeException
  65.      */
  66.     public function buildTree()
  67.     {
  68.         $this->assertTreeHasRootNode();
  69.         if (null !== $this->tree) {
  70.             return $this->tree;
  71.         }
  72.         return $this->tree $this->root->getNode(true);
  73.     }
  74.     public function setPathSeparator(string $separator)
  75.     {
  76.         $this->assertTreeHasRootNode();
  77.         // unset last built as changing path separator changes all nodes
  78.         $this->tree null;
  79.         $this->root->setPathSeparator($separator);
  80.     }
  81.     /**
  82.      * @throws \RuntimeException if root node is not defined
  83.      */
  84.     private function assertTreeHasRootNode()
  85.     {
  86.         if (null === $this->root) {
  87.             throw new TreeWithoutRootNodeException('The configuration tree has no root node.');
  88.         }
  89.     }
  90. }