TreeBuilder.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\NodeInterface;
  12. /**
  13. * This is the entry class for building a config tree.
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class TreeBuilder implements NodeParentInterface
  18. {
  19. protected $tree;
  20. protected $root;
  21. public function __construct(string $name, string $type = 'array', NodeBuilder $builder = null)
  22. {
  23. $builder = $builder ?: new NodeBuilder();
  24. $this->root = $builder->node($name, $type)->setParent($this);
  25. }
  26. /**
  27. * @return NodeDefinition|ArrayNodeDefinition The root node (as an ArrayNodeDefinition when the type is 'array')
  28. */
  29. public function getRootNode(): NodeDefinition
  30. {
  31. return $this->root;
  32. }
  33. /**
  34. * Builds the tree.
  35. *
  36. * @return NodeInterface
  37. *
  38. * @throws \RuntimeException
  39. */
  40. public function buildTree()
  41. {
  42. if (null !== $this->tree) {
  43. return $this->tree;
  44. }
  45. return $this->tree = $this->root->getNode(true);
  46. }
  47. public function setPathSeparator(string $separator)
  48. {
  49. // unset last built as changing path separator changes all nodes
  50. $this->tree = null;
  51. $this->root->setPathSeparator($separator);
  52. }
  53. }