TreeBuilder.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\BaseNode;
  12. use Symfony\Component\Config\Definition\BooleanNode;
  13. use Symfony\Component\Config\Definition\ArrayNode;
  14. use Symfony\Component\Config\Definition\ScalarNode;
  15. use Symfony\Component\Config\Definition\VariableNode;
  16. /**
  17. * This is the entry class for building your own config tree.
  18. *
  19. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20. */
  21. class TreeBuilder
  22. {
  23. protected $root;
  24. protected $tree;
  25. /**
  26. * Creates the NodeBuilder for the root node
  27. *
  28. * @param string $name The name of the node
  29. * @param string $type The type of the node
  30. *
  31. * @return Symfony\Component\Config\Definition\Builder\NodeBuilder
  32. */
  33. public function root($name, $type)
  34. {
  35. $this->tree = null;
  36. return $this->root = new NodeBuilder($name, $type, $this);
  37. }
  38. /**
  39. * Builds the tree.
  40. *
  41. * @return Symfony\Component\Config\Definition\NodeInterface
  42. */
  43. public function buildTree()
  44. {
  45. if (null === $this->root) {
  46. throw new \RuntimeException('You haven\'t added a root node.');
  47. }
  48. if (null !== $this->tree) {
  49. return $this->tree;
  50. }
  51. $this->root->parent = null;
  52. return $this->tree = $this->createConfigNode($this->root);
  53. }
  54. /**
  55. * Creates a node.
  56. *
  57. * @param NodeBuilder $node The builder of the node
  58. *
  59. * @return Symfony\Component\Config\Definition\NodeInterface
  60. */
  61. protected function createConfigNode(NodeBuilder $node)
  62. {
  63. $method = 'create'.$node->type.'ConfigNode';
  64. if (!method_exists($this, $method)) {
  65. throw new \RuntimeException(sprintf('Unknown node type: "%s"', $node->type));
  66. }
  67. return $this->$method($node);
  68. }
  69. /**
  70. * Creates a boolean node.
  71. *
  72. * @param NodeBuilder $node The builder of the node
  73. *
  74. * @return Symfony\Component\Config\Definition\BooleanNode
  75. */
  76. protected function createBooleanConfigNode(NodeBuilder $node)
  77. {
  78. $configNode = new BooleanNode($node->name, $node->parent);
  79. $this->configureVariableNode($configNode, $node);
  80. return $configNode;
  81. }
  82. /**
  83. * Creates a scalar node.
  84. *
  85. * @param NodeBuilder $node the builder of the node
  86. *
  87. * @return Symfony\Component\Config\Definition\ScalarNode
  88. */
  89. protected function createScalarConfigNode(NodeBuilder $node)
  90. {
  91. $configNode = new ScalarNode($node->name, $node->parent);
  92. $this->configureVariableNode($configNode, $node);
  93. return $configNode;
  94. }
  95. /**
  96. * Configures a variable node.
  97. *
  98. * @param VariableNode $configNode The node to configure
  99. * @param NodeBuilder $node The builder of the node
  100. */
  101. protected function configureVariableNode(VariableNode $configNode, NodeBuilder $node)
  102. {
  103. if (null !== $node->normalization) {
  104. $configNode->setNormalizationClosures(
  105. $this->buildExpressions($node->normalization->before)
  106. );
  107. }
  108. if (null !== $node->merge) {
  109. $configNode->setAllowOverwrite($node->merge->allowOverwrite);
  110. }
  111. if (true === $node->default) {
  112. $configNode->setDefaultValue($node->defaultValue);
  113. }
  114. if (false === $node->allowEmptyValue) {
  115. $configNode->setAllowEmptyValue($node->allowEmptyValue);
  116. }
  117. $configNode->addEquivalentValue(null, $node->nullEquivalent);
  118. $configNode->addEquivalentValue(true, $node->trueEquivalent);
  119. $configNode->addEquivalentValue(false, $node->falseEquivalent);
  120. $configNode->setRequired($node->required);
  121. if (null !== $node->validation) {
  122. $configNode->setFinalValidationClosures(
  123. $this->buildExpressions($node->validation->rules)
  124. );
  125. }
  126. }
  127. /**
  128. * Creates a variable node.
  129. *
  130. * @param NodeBuilder $node the builder of the node
  131. *
  132. * @return Symfony\Component\Config\Definition\VariableNode
  133. */
  134. protected function createVariableConfigNode(NodeBuilder $node)
  135. {
  136. $configNode = new VariableNode($node->name, $node->parent);
  137. $this->configureVariableNode($configNode, $node);
  138. return $configNode;
  139. }
  140. /**
  141. * Creates an array node.
  142. *
  143. * @param NodeBuilder $node The builder of the node
  144. *
  145. * @return Symfony\Component\Config\Definition\ArrayNode
  146. */
  147. protected function createArrayConfigNode(NodeBuilder $node)
  148. {
  149. $configNode = new ArrayNode($node->name, $node->parent);
  150. $configNode->setAddIfNotSet($node->addDefaults);
  151. $configNode->setAllowNewKeys($node->allowNewKeys);
  152. $configNode->addEquivalentValue(null, $node->nullEquivalent);
  153. $configNode->addEquivalentValue(true, $node->trueEquivalent);
  154. $configNode->addEquivalentValue(false, $node->falseEquivalent);
  155. $configNode->setPerformDeepMerging($node->performDeepMerging);
  156. $configNode->setRequired($node->required);
  157. $configNode->setIgnoreExtraKeys($node->ignoreExtraKeys);
  158. if (null !== $node->key) {
  159. $configNode->setKeyAttribute($node->key, $node->removeKeyItem);
  160. }
  161. if (true === $node->atLeastOne) {
  162. $configNode->setMinNumberOfElements(1);
  163. }
  164. if (null !== $node->normalization) {
  165. $configNode->setNormalizationClosures(
  166. $this->buildExpressions($node->normalization->before)
  167. );
  168. $configNode->setXmlRemappings($node->normalization->remappings);
  169. }
  170. if (null !== $node->merge) {
  171. $configNode->setAllowOverwrite($node->merge->allowOverwrite);
  172. $configNode->setAllowFalse($node->merge->allowFalse);
  173. }
  174. foreach ($node->children as $child) {
  175. $child->parent = $configNode;
  176. $configNode->addChild($this->createConfigNode($child));
  177. }
  178. if (null !== $node->prototype) {
  179. $node->prototype->parent = $configNode;
  180. $configNode->setPrototype($this->createConfigNode($node->prototype));
  181. }
  182. if (null !== $node->defaultValue) {
  183. $configNode->setDefaultValue($node->defaultValue);
  184. }
  185. if (null !== $node->validation) {
  186. $configNode->setFinalValidationClosures(
  187. $this->buildExpressions($node->validation->rules)
  188. );
  189. }
  190. return $configNode;
  191. }
  192. /**
  193. * Builds the expressions.
  194. *
  195. * @param array $expressions An array of ExprBuilder instances to build
  196. *
  197. * @return array
  198. */
  199. protected function buildExpressions(array $expressions)
  200. {
  201. foreach ($expressions as $k => $expr) {
  202. if (!$expr instanceof ExprBuilder) {
  203. continue;
  204. }
  205. $expressions[$k] = function($v) use($expr) {
  206. $ifPart = $expr->ifPart;
  207. if (true !== $ifPart($v)) {
  208. return $v;
  209. }
  210. $thenPart = $expr->thenPart;
  211. return $thenPart($v);
  212. };
  213. }
  214. return $expressions;
  215. }
  216. }