YamlReferenceDumper.php 8.0 KB

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