XmlReferenceDumper.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. private function writeNode(NodeInterface $node, int $depth = 0, bool $root = false, string $namespace = null)
  37. {
  38. $rootName = ($root ? 'config' : $node->getName());
  39. $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null));
  40. // xml remapping
  41. if ($node->getParent()) {
  42. $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) {
  43. return $rootName === $mapping[1];
  44. });
  45. if (\count($remapping)) {
  46. list($singular) = current($remapping);
  47. $rootName = $singular;
  48. }
  49. }
  50. $rootName = str_replace('_', '-', $rootName);
  51. $rootAttributes = [];
  52. $rootAttributeComments = [];
  53. $rootChildren = [];
  54. $rootComments = [];
  55. if ($node instanceof ArrayNode) {
  56. $children = $node->getChildren();
  57. // comments about the root node
  58. if ($rootInfo = $node->getInfo()) {
  59. $rootComments[] = $rootInfo;
  60. }
  61. if ($rootNamespace) {
  62. $rootComments[] = 'Namespace: '.$rootNamespace;
  63. }
  64. // render prototyped nodes
  65. if ($node instanceof PrototypedArrayNode) {
  66. $prototype = $node->getPrototype();
  67. $info = 'prototype';
  68. if (null !== $prototype->getInfo()) {
  69. $info .= ': '.$prototype->getInfo();
  70. }
  71. array_unshift($rootComments, $info);
  72. if ($key = $node->getKeyAttribute()) {
  73. $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key;
  74. }
  75. if ($prototype instanceof PrototypedArrayNode) {
  76. $prototype->setName($key);
  77. $children = [$key => $prototype];
  78. } elseif ($prototype instanceof ArrayNode) {
  79. $children = $prototype->getChildren();
  80. } else {
  81. if ($prototype->hasDefaultValue()) {
  82. $prototypeValue = $prototype->getDefaultValue();
  83. } else {
  84. switch (\get_class($prototype)) {
  85. case 'Symfony\Component\Config\Definition\ScalarNode':
  86. $prototypeValue = 'scalar value';
  87. break;
  88. case 'Symfony\Component\Config\Definition\FloatNode':
  89. case 'Symfony\Component\Config\Definition\IntegerNode':
  90. $prototypeValue = 'numeric value';
  91. break;
  92. case 'Symfony\Component\Config\Definition\BooleanNode':
  93. $prototypeValue = 'true|false';
  94. break;
  95. case 'Symfony\Component\Config\Definition\EnumNode':
  96. $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues()));
  97. break;
  98. default:
  99. $prototypeValue = 'value';
  100. }
  101. }
  102. }
  103. }
  104. // get attributes and elements
  105. foreach ($children as $child) {
  106. if (!$child instanceof ArrayNode) {
  107. // get attributes
  108. // metadata
  109. $name = str_replace('_', '-', $child->getName());
  110. $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
  111. // comments
  112. $comments = [];
  113. if ($info = $child->getInfo()) {
  114. $comments[] = $info;
  115. }
  116. if ($example = $child->getExample()) {
  117. $comments[] = 'Example: '.$example;
  118. }
  119. if ($child->isRequired()) {
  120. $comments[] = 'Required';
  121. }
  122. if ($child->isDeprecated()) {
  123. $comments[] = sprintf('Deprecated (%s)', $child->getDeprecationMessage($child->getName(), $node->getPath()));
  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. private function writeLine(string $text, int $indent = 0)
  209. {
  210. $indent = \strlen($text) + $indent;
  211. $format = '%'.$indent.'s';
  212. $this->reference .= sprintf($format, $text).PHP_EOL;
  213. }
  214. /**
  215. * Renders the string conversion of the value.
  216. *
  217. * @param mixed $value
  218. */
  219. private function writeValue($value): string
  220. {
  221. if ('%%%%not_defined%%%%' === $value) {
  222. return '';
  223. }
  224. if (\is_string($value) || is_numeric($value)) {
  225. return $value;
  226. }
  227. if (false === $value) {
  228. return 'false';
  229. }
  230. if (true === $value) {
  231. return 'true';
  232. }
  233. if (null === $value) {
  234. return 'null';
  235. }
  236. if (empty($value)) {
  237. return '';
  238. }
  239. if (\is_array($value)) {
  240. return implode(',', $value);
  241. }
  242. }
  243. }