TreeBuilder.php 6.4 KB

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