XmlReferenceDumper.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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\Dumper;
  11. use Symfony\Component\Config\Definition\ArrayNode;
  12. use Symfony\Component\Config\Definition\ConfigurationInterface;
  13. use Symfony\Component\Config\Definition\EnumNode;
  14. use Symfony\Component\Config\Definition\NodeInterface;
  15. use Symfony\Component\Config\Definition\PrototypedArrayNode;
  16. /**
  17. * Dumps a XML reference configuration for the given configuration/node instance.
  18. *
  19. * @author Wouter J <waldio.webdesign@gmail.com>
  20. */
  21. class XmlReferenceDumper
  22. {
  23. private $reference;
  24. public function dump(ConfigurationInterface $configuration, $namespace = null)
  25. {
  26. return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace);
  27. }
  28. public function dumpNode(NodeInterface $node, $namespace = null)
  29. {
  30. $this->reference = '';
  31. $this->writeNode($node, 0, true, $namespace);
  32. $ref = $this->reference;
  33. $this->reference = null;
  34. return $ref;
  35. }
  36. /**
  37. * @param NodeInterface $node
  38. * @param int $depth
  39. * @param bool $root If the node is the root node
  40. * @param string $namespace The namespace of the node
  41. */
  42. private function writeNode(NodeInterface $node, $depth = 0, $root = false, $namespace = null)
  43. {
  44. $rootName = ($root ? 'config' : $node->getName());
  45. $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null));
  46. // xml remapping
  47. if ($node->getParent()) {
  48. $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) {
  49. return $rootName === $mapping[1];
  50. });
  51. if (\count($remapping)) {
  52. list($singular) = current($remapping);
  53. $rootName = $singular;
  54. }
  55. }
  56. $rootName = str_replace('_', '-', $rootName);
  57. $rootAttributes = array();
  58. $rootAttributeComments = array();
  59. $rootChildren = array();
  60. $rootComments = array();
  61. if ($node instanceof ArrayNode) {
  62. $children = $node->getChildren();
  63. // comments about the root node
  64. if ($rootInfo = $node->getInfo()) {
  65. $rootComments[] = $rootInfo;
  66. }
  67. if ($rootNamespace) {
  68. $rootComments[] = 'Namespace: '.$rootNamespace;
  69. }
  70. // render prototyped nodes
  71. if ($node instanceof PrototypedArrayNode) {
  72. $prototype = $node->getPrototype();
  73. $info = 'prototype';
  74. if (null !== $prototype->getInfo()) {
  75. $info .= ': '.$prototype->getInfo();
  76. }
  77. array_unshift($rootComments, $info);
  78. if ($key = $node->getKeyAttribute()) {
  79. $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key;
  80. }
  81. if ($prototype instanceof ArrayNode) {
  82. $children = $prototype->getChildren();
  83. } else {
  84. if ($prototype->hasDefaultValue()) {
  85. $prototypeValue = $prototype->getDefaultValue();
  86. } else {
  87. switch (\get_class($prototype)) {
  88. case 'Symfony\Component\Config\Definition\ScalarNode':
  89. $prototypeValue = 'scalar value';
  90. break;
  91. case 'Symfony\Component\Config\Definition\FloatNode':
  92. case 'Symfony\Component\Config\Definition\IntegerNode':
  93. $prototypeValue = 'numeric value';
  94. break;
  95. case 'Symfony\Component\Config\Definition\BooleanNode':
  96. $prototypeValue = 'true|false';
  97. break;
  98. case 'Symfony\Component\Config\Definition\EnumNode':
  99. $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues()));
  100. break;
  101. default:
  102. $prototypeValue = 'value';
  103. }
  104. }
  105. }
  106. }
  107. // get attributes and elements
  108. foreach ($children as $child) {
  109. if (!$child instanceof ArrayNode) {
  110. // get attributes
  111. // metadata
  112. $name = str_replace('_', '-', $child->getName());
  113. $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
  114. // comments
  115. $comments = array();
  116. if ($info = $child->getInfo()) {
  117. $comments[] = $info;
  118. }
  119. if ($example = $child->getExample()) {
  120. $comments[] = 'Example: '.$example;
  121. }
  122. if ($child->isRequired()) {
  123. $comments[] = 'Required';
  124. }
  125. if ($child instanceof EnumNode) {
  126. $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
  127. }
  128. if (\count($comments)) {
  129. $rootAttributeComments[$name] = implode(";\n", $comments);
  130. }
  131. // default values
  132. if ($child->hasDefaultValue()) {
  133. $value = $child->getDefaultValue();
  134. }
  135. // append attribute
  136. $rootAttributes[$name] = $value;
  137. } else {
  138. // get elements
  139. $rootChildren[] = $child;
  140. }
  141. }
  142. }
  143. // render comments
  144. // root node comment
  145. if (\count($rootComments)) {
  146. foreach ($rootComments as $comment) {
  147. $this->writeLine('<!-- '.$comment.' -->', $depth);
  148. }
  149. }
  150. // attribute comments
  151. if (\count($rootAttributeComments)) {
  152. foreach ($rootAttributeComments as $attrName => $comment) {
  153. $commentDepth = $depth + 4 + \strlen($attrName) + 2;
  154. $commentLines = explode("\n", $comment);
  155. $multiline = (\count($commentLines) > 1);
  156. $comment = implode(PHP_EOL.str_repeat(' ', $commentDepth), $commentLines);
  157. if ($multiline) {
  158. $this->writeLine('<!--', $depth);
  159. $this->writeLine($attrName.': '.$comment, $depth + 4);
  160. $this->writeLine('-->', $depth);
  161. } else {
  162. $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth);
  163. }
  164. }
  165. }
  166. // render start tag + attributes
  167. $rootIsVariablePrototype = isset($prototypeValue);
  168. $rootIsEmptyTag = (0 === \count($rootChildren) && !$rootIsVariablePrototype);
  169. $rootOpenTag = '<'.$rootName;
  170. if (1 >= ($attributesCount = \count($rootAttributes))) {
  171. if (1 === $attributesCount) {
  172. $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes)));
  173. }
  174. $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>';
  175. if ($rootIsVariablePrototype) {
  176. $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
  177. }
  178. $this->writeLine($rootOpenTag, $depth);
  179. } else {
  180. $this->writeLine($rootOpenTag, $depth);
  181. $i = 1;
  182. foreach ($rootAttributes as $attrName => $attrValue) {
  183. $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue));
  184. $this->writeLine($attr, $depth + 4);
  185. if ($attributesCount === $i++) {
  186. $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth);
  187. if ($rootIsVariablePrototype) {
  188. $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
  189. }
  190. }
  191. }
  192. }
  193. // render children tags
  194. foreach ($rootChildren as $child) {
  195. $this->writeLine('');
  196. $this->writeNode($child, $depth + 4);
  197. }
  198. // render end tag
  199. if (!$rootIsEmptyTag && !$rootIsVariablePrototype) {
  200. $this->writeLine('');
  201. $rootEndTag = '</'.$rootName.'>';
  202. $this->writeLine($rootEndTag, $depth);
  203. }
  204. }
  205. /**
  206. * Outputs a single config reference line.
  207. *
  208. * @param string $text
  209. * @param int $indent
  210. */
  211. private function writeLine($text, $indent = 0)
  212. {
  213. $indent = \strlen($text) + $indent;
  214. $format = '%'.$indent.'s';
  215. $this->reference .= sprintf($format, $text).PHP_EOL;
  216. }
  217. /**
  218. * Renders the string conversion of the value.
  219. *
  220. * @param mixed $value
  221. *
  222. * @return string
  223. */
  224. private function writeValue($value)
  225. {
  226. if ('%%%%not_defined%%%%' === $value) {
  227. return '';
  228. }
  229. if (\is_string($value) || is_numeric($value)) {
  230. return $value;
  231. }
  232. if (false === $value) {
  233. return 'false';
  234. }
  235. if (true === $value) {
  236. return 'true';
  237. }
  238. if (null === $value) {
  239. return 'null';
  240. }
  241. if (empty($value)) {
  242. return '';
  243. }
  244. if (\is_array($value)) {
  245. return implode(',', $value);
  246. }
  247. }
  248. }