XmlReferenceDumper.php 10 KB

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