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

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