TreeBuilder.php 8.2 KB

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