XmlReferenceDumper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 an 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, string $namespace = null)
  25. {
  26. return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace);
  27. }
  28. public function dumpNode(NodeInterface $node, string $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. [$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. $deprecation = $child->getDeprecation($child->getName(), $node->getPath());
  124. $comments[] = sprintf('Deprecated (%s)', ($deprecation['package'] || $deprecation['version'] ? "Since {$deprecation['package']} {$deprecation['version']}: " : '').$deprecation['message']);
  125. }
  126. if ($child instanceof EnumNode) {
  127. $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
  128. }
  129. if (\count($comments)) {
  130. $rootAttributeComments[$name] = implode(";\n", $comments);
  131. }
  132. // default values
  133. if ($child->hasDefaultValue()) {
  134. $value = $child->getDefaultValue();
  135. }
  136. // append attribute
  137. $rootAttributes[$name] = $value;
  138. } else {
  139. // get elements
  140. $rootChildren[] = $child;
  141. }
  142. }
  143. }
  144. // render comments
  145. // root node comment
  146. if (\count($rootComments)) {
  147. foreach ($rootComments as $comment) {
  148. $this->writeLine('<!-- '.$comment.' -->', $depth);
  149. }
  150. }
  151. // attribute comments
  152. if (\count($rootAttributeComments)) {
  153. foreach ($rootAttributeComments as $attrName => $comment) {
  154. $commentDepth = $depth + 4 + \strlen($attrName) + 2;
  155. $commentLines = explode("\n", $comment);
  156. $multiline = (\count($commentLines) > 1);
  157. $comment = implode(\PHP_EOL.str_repeat(' ', $commentDepth), $commentLines);
  158. if ($multiline) {
  159. $this->writeLine('<!--', $depth);
  160. $this->writeLine($attrName.': '.$comment, $depth + 4);
  161. $this->writeLine('-->', $depth);
  162. } else {
  163. $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth);
  164. }
  165. }
  166. }
  167. // render start tag + attributes
  168. $rootIsVariablePrototype = isset($prototypeValue);
  169. $rootIsEmptyTag = (0 === \count($rootChildren) && !$rootIsVariablePrototype);
  170. $rootOpenTag = '<'.$rootName;
  171. if (1 >= ($attributesCount = \count($rootAttributes))) {
  172. if (1 === $attributesCount) {
  173. $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes)));
  174. }
  175. $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>';
  176. if ($rootIsVariablePrototype) {
  177. $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
  178. }
  179. $this->writeLine($rootOpenTag, $depth);
  180. } else {
  181. $this->writeLine($rootOpenTag, $depth);
  182. $i = 1;
  183. foreach ($rootAttributes as $attrName => $attrValue) {
  184. $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue));
  185. $this->writeLine($attr, $depth + 4);
  186. if ($attributesCount === $i++) {
  187. $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth);
  188. if ($rootIsVariablePrototype) {
  189. $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
  190. }
  191. }
  192. }
  193. }
  194. // render children tags
  195. foreach ($rootChildren as $child) {
  196. $this->writeLine('');
  197. $this->writeNode($child, $depth + 4);
  198. }
  199. // render end tag
  200. if (!$rootIsEmptyTag && !$rootIsVariablePrototype) {
  201. $this->writeLine('');
  202. $rootEndTag = '</'.$rootName.'>';
  203. $this->writeLine($rootEndTag, $depth);
  204. }
  205. }
  206. /**
  207. * Outputs a single config reference line.
  208. */
  209. private function writeLine(string $text, int $indent = 0)
  210. {
  211. $indent = \strlen($text) + $indent;
  212. $format = '%'.$indent.'s';
  213. $this->reference .= sprintf($format, $text).\PHP_EOL;
  214. }
  215. /**
  216. * Renders the string conversion of the value.
  217. *
  218. * @param mixed $value
  219. */
  220. private function writeValue($value): string
  221. {
  222. if ('%%%%not_defined%%%%' === $value) {
  223. return '';
  224. }
  225. if (\is_string($value) || is_numeric($value)) {
  226. return $value;
  227. }
  228. if (false === $value) {
  229. return 'false';
  230. }
  231. if (true === $value) {
  232. return 'true';
  233. }
  234. if (null === $value) {
  235. return 'null';
  236. }
  237. if (empty($value)) {
  238. return '';
  239. }
  240. if (\is_array($value)) {
  241. return implode(',', $value);
  242. }
  243. return '';
  244. }
  245. }