YamlReferenceDumper.php 7.5 KB

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