TreeBuilder.php 2.9 KB

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