YamlReferenceDumper.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. use Symfony\Component\Config\Definition\ScalarNode;
  18. use Symfony\Component\Yaml\Inline;
  19. /**
  20. * Dumps a Yaml reference configuration for the given configuration/node instance.
  21. *
  22. * @author Kevin Bond <kevinbond@gmail.com>
  23. */
  24. class YamlReferenceDumper
  25. {
  26. private $reference;
  27. public function dump(ConfigurationInterface $configuration)
  28. {
  29. return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
  30. }
  31. public function dumpAtPath(ConfigurationInterface $configuration, string $path)
  32. {
  33. $rootNode = $node = $configuration->getConfigTreeBuilder()->buildTree();
  34. foreach (explode('.', $path) as $step) {
  35. if (!$node instanceof ArrayNode) {
  36. throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s".', $rootNode->getName(), $path));
  37. }
  38. /** @var NodeInterface[] $children */
  39. $children = $node instanceof PrototypedArrayNode ? $this->getPrototypeChildren($node) : $node->getChildren();
  40. foreach ($children as $child) {
  41. if ($child->getName() === $step) {
  42. $node = $child;
  43. continue 2;
  44. }
  45. }
  46. throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s".', $rootNode->getName(), $path));
  47. }
  48. return $this->dumpNode($node);
  49. }
  50. public function dumpNode(NodeInterface $node)
  51. {
  52. $this->reference = '';
  53. $this->writeNode($node);
  54. $ref = $this->reference;
  55. $this->reference = null;
  56. return $ref;
  57. }
  58. private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = null, int $depth = 0, bool $prototypedArray = false)
  59. {
  60. $comments = [];
  61. $default = '';
  62. $defaultArray = null;
  63. $children = null;
  64. $example = null;
  65. if ($node instanceof BaseNode) {
  66. $example = $node->getExample();
  67. }
  68. // defaults
  69. if ($node instanceof ArrayNode) {
  70. $children = $node->getChildren();
  71. if ($node instanceof PrototypedArrayNode) {
  72. $children = $this->getPrototypeChildren($node);
  73. }
  74. if (!$children && !($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue()))) {
  75. $default = '[]';
  76. }
  77. } elseif ($node instanceof EnumNode) {
  78. $comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
  79. $default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
  80. } else {
  81. $default = '~';
  82. if ($node->hasDefaultValue()) {
  83. $default = $node->getDefaultValue();
  84. if (\is_array($default)) {
  85. if (\count($defaultArray = $node->getDefaultValue())) {
  86. $default = '';
  87. } elseif (!\is_array($example)) {
  88. $default = '[]';
  89. }
  90. } else {
  91. $default = Inline::dump($default);
  92. }
  93. }
  94. }
  95. // required?
  96. if ($node->isRequired()) {
  97. $comments[] = 'Required';
  98. }
  99. // deprecated?
  100. if ($node instanceof BaseNode && $node->isDeprecated()) {
  101. $deprecation = $node->getDeprecation($node->getName(), $parentNode ? $parentNode->getPath() : $node->getPath());
  102. $comments[] = sprintf('Deprecated (%s)', ($deprecation['package'] || $deprecation['version'] ? "Since {$deprecation['package']} {$deprecation['version']}: " : '').$deprecation['message']);
  103. }
  104. // example
  105. if ($example && !\is_array($example)) {
  106. $comments[] = 'Example: '.Inline::dump($example);
  107. }
  108. $default = '' != (string) $default ? ' '.$default : '';
  109. $comments = \count($comments) ? '# '.implode(', ', $comments) : '';
  110. $key = $prototypedArray ? '-' : $node->getName().':';
  111. $text = rtrim(sprintf('%-21s%s %s', $key, $default, $comments), ' ');
  112. if ($node instanceof BaseNode && $info = $node->getInfo()) {
  113. $this->writeLine('');
  114. // indenting multi-line info
  115. $info = str_replace("\n", sprintf("\n%".($depth * 4).'s# ', ' '), $info);
  116. $this->writeLine('# '.$info, $depth * 4);
  117. }
  118. $this->writeLine($text, $depth * 4);
  119. // output defaults
  120. if ($defaultArray) {
  121. $this->writeLine('');
  122. $message = \count($defaultArray) > 1 ? 'Defaults' : 'Default';
  123. $this->writeLine('# '.$message.':', $depth * 4 + 4);
  124. $this->writeArray($defaultArray, $depth + 1);
  125. }
  126. if (\is_array($example)) {
  127. $this->writeLine('');
  128. $message = \count($example) > 1 ? 'Examples' : 'Example';
  129. $this->writeLine('# '.$message.':', $depth * 4 + 4);
  130. $this->writeArray(array_map([Inline::class, 'dump'], $example), $depth + 1, true);
  131. }
  132. if ($children) {
  133. foreach ($children as $childNode) {
  134. $this->writeNode($childNode, $node, $depth + 1, $node instanceof PrototypedArrayNode && !$node->getKeyAttribute());
  135. }
  136. }
  137. }
  138. /**
  139. * Outputs a single config reference line.
  140. */
  141. private function writeLine(string $text, int $indent = 0)
  142. {
  143. $indent = \strlen($text) + $indent;
  144. $format = '%'.$indent.'s';
  145. $this->reference .= sprintf($format, $text)."\n";
  146. }
  147. private function writeArray(array $array, int $depth, bool $asComment = false)
  148. {
  149. $isIndexed = array_values($array) === $array;
  150. foreach ($array as $key => $value) {
  151. if (\is_array($value)) {
  152. $val = '';
  153. } else {
  154. $val = $value;
  155. }
  156. $prefix = $asComment ? '# ' : '';
  157. if ($isIndexed) {
  158. $this->writeLine($prefix.'- '.$val, $depth * 4);
  159. } else {
  160. $this->writeLine(sprintf('%s%-20s %s', $prefix, $key.':', $val), $depth * 4);
  161. }
  162. if (\is_array($value)) {
  163. $this->writeArray($value, $depth + 1, $asComment);
  164. }
  165. }
  166. }
  167. private function getPrototypeChildren(PrototypedArrayNode $node): array
  168. {
  169. $prototype = $node->getPrototype();
  170. $key = $node->getKeyAttribute();
  171. // Do not expand prototype if it isn't an array node nor uses attribute as key
  172. if (!$key && !$prototype instanceof ArrayNode) {
  173. return $node->getChildren();
  174. }
  175. if ($prototype instanceof ArrayNode) {
  176. $keyNode = new ArrayNode($key, $node);
  177. $children = $prototype->getChildren();
  178. if ($prototype instanceof PrototypedArrayNode && $prototype->getKeyAttribute()) {
  179. $children = $this->getPrototypeChildren($prototype);
  180. }
  181. // add children
  182. foreach ($children as $childNode) {
  183. $keyNode->addChild($childNode);
  184. }
  185. } else {
  186. $keyNode = new ScalarNode($key, $node);
  187. }
  188. $info = 'Prototype';
  189. if (null !== $prototype->getInfo()) {
  190. $info .= ': '.$prototype->getInfo();
  191. }
  192. $keyNode->setInfo($info);
  193. return [$key => $keyNode];
  194. }
  195. }