NodeBuilder.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /**
  12. * This class provides a fluent interface for building a node.
  13. *
  14. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  15. */
  16. class NodeBuilder implements NodeParentInterface
  17. {
  18. protected $parent;
  19. protected $nodeMapping;
  20. /**
  21. * Constructor
  22. *
  23. */
  24. public function __construct()
  25. {
  26. $this->nodeMapping = array(
  27. 'variable' => __NAMESPACE__.'\\VariableNodeDefinition',
  28. 'scalar' => __NAMESPACE__.'\\ScalarNodeDefinition',
  29. 'boolean' => __NAMESPACE__.'\\BooleanNodeDefinition',
  30. 'integer' => __NAMESPACE__.'\\IntegerNodeDefinition',
  31. 'float' => __NAMESPACE__.'\\FloatNodeDefinition',
  32. 'array' => __NAMESPACE__.'\\ArrayNodeDefinition',
  33. 'enum' => __NAMESPACE__.'\\EnumNodeDefinition',
  34. );
  35. }
  36. /**
  37. * Set the parent node.
  38. *
  39. * @param ParentNodeDefinitionInterface $parent The parent node
  40. *
  41. * @return NodeBuilder This node builder
  42. */
  43. public function setParent(ParentNodeDefinitionInterface $parent = null)
  44. {
  45. $this->parent = $parent;
  46. return $this;
  47. }
  48. /**
  49. * Creates a child array node.
  50. *
  51. * @param string $name The name of the node
  52. *
  53. * @return ArrayNodeDefinition The child node
  54. */
  55. public function arrayNode($name)
  56. {
  57. return $this->node($name, 'array');
  58. }
  59. /**
  60. * Creates a child scalar node.
  61. *
  62. * @param string $name the name of the node
  63. *
  64. * @return ScalarNodeDefinition The child node
  65. */
  66. public function scalarNode($name)
  67. {
  68. return $this->node($name, 'scalar');
  69. }
  70. /**
  71. * Creates a child Boolean node.
  72. *
  73. * @param string $name The name of the node
  74. *
  75. * @return BooleanNodeDefinition The child node
  76. */
  77. public function booleanNode($name)
  78. {
  79. return $this->node($name, 'boolean');
  80. }
  81. /**
  82. * Creates a child integer node.
  83. *
  84. * @param string $name the name of the node
  85. *
  86. * @return IntegerNodeDefinition The child node
  87. */
  88. public function integerNode($name)
  89. {
  90. return $this->node($name, 'integer');
  91. }
  92. /**
  93. * Creates a child float node.
  94. *
  95. * @param string $name the name of the node
  96. *
  97. * @return FloatNodeDefinition The child node
  98. */
  99. public function floatNode($name)
  100. {
  101. return $this->node($name, 'float');
  102. }
  103. /**
  104. * Creates a child EnumNode.
  105. *
  106. * @param string $name
  107. *
  108. * @return EnumNodeDefinition
  109. */
  110. public function enumNode($name)
  111. {
  112. return $this->node($name, 'enum');
  113. }
  114. /**
  115. * Creates a child variable node.
  116. *
  117. * @param string $name The name of the node
  118. *
  119. * @return VariableNodeDefinition The builder of the child node
  120. */
  121. public function variableNode($name)
  122. {
  123. return $this->node($name, 'variable');
  124. }
  125. /**
  126. * Returns the parent node.
  127. *
  128. * @return ParentNodeDefinitionInterface The parent node
  129. */
  130. public function end()
  131. {
  132. return $this->parent;
  133. }
  134. /**
  135. * Creates a child node.
  136. *
  137. * @param string $name The name of the node
  138. * @param string $type The type of the node
  139. *
  140. * @return NodeDefinition The child node
  141. *
  142. * @throws \RuntimeException When the node type is not registered
  143. * @throws \RuntimeException When the node class is not found
  144. */
  145. public function node($name, $type)
  146. {
  147. $class = $this->getNodeClass($type);
  148. $node = new $class($name);
  149. $this->append($node);
  150. return $node;
  151. }
  152. /**
  153. * Appends a node definition.
  154. *
  155. * Usage:
  156. *
  157. * $node = new ArrayNodeDefinition('name')
  158. * ->children()
  159. * ->scalarNode('foo')->end()
  160. * ->scalarNode('baz')->end()
  161. * ->append($this->getBarNodeDefinition())
  162. * ->end()
  163. * ;
  164. *
  165. * @param NodeDefinition $node
  166. *
  167. * @return NodeBuilder This node builder
  168. */
  169. public function append(NodeDefinition $node)
  170. {
  171. if ($node instanceof ParentNodeDefinitionInterface) {
  172. $builder = clone $this;
  173. $builder->setParent(null);
  174. $node->setBuilder($builder);
  175. }
  176. if (null !== $this->parent) {
  177. $this->parent->append($node);
  178. // Make this builder the node parent to allow for a fluid interface
  179. $node->setParent($this);
  180. }
  181. return $this;
  182. }
  183. /**
  184. * Adds or overrides a node Type.
  185. *
  186. * @param string $type The name of the type
  187. * @param string $class The fully qualified name the node definition class
  188. *
  189. * @return NodeBuilder This node builder
  190. */
  191. public function setNodeClass($type, $class)
  192. {
  193. $this->nodeMapping[strtolower($type)] = $class;
  194. return $this;
  195. }
  196. /**
  197. * Returns the class name of the node definition.
  198. *
  199. * @param string $type The node type
  200. *
  201. * @return string The node definition class name
  202. *
  203. * @throws \RuntimeException When the node type is not registered
  204. * @throws \RuntimeException When the node class is not found
  205. */
  206. protected function getNodeClass($type)
  207. {
  208. $type = strtolower($type);
  209. if (!isset($this->nodeMapping[$type])) {
  210. throw new \RuntimeException(sprintf('The node type "%s" is not registered.', $type));
  211. }
  212. $class = $this->nodeMapping[$type];
  213. if (!class_exists($class)) {
  214. throw new \RuntimeException(sprintf('The node class "%s" does not exist.', $class));
  215. }
  216. return $class;
  217. }
  218. }