YamlReferenceDumper.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Yaml\Inline;
  17. /**
  18. * Dumps a Yaml reference configuration for the given configuration/node instance.
  19. *
  20. * @author Kevin Bond <kevinbond@gmail.com>
  21. */
  22. class YamlReferenceDumper
  23. {
  24. private $reference;
  25. public function dump(ConfigurationInterface $configuration)
  26. {
  27. return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
  28. }
  29. public function dumpNode(NodeInterface $node)
  30. {
  31. $this->reference = '';
  32. $this->writeNode($node);
  33. $ref = $this->reference;
  34. $this->reference = null;
  35. return $ref;
  36. }
  37. /**
  38. * @param NodeInterface $node
  39. * @param int $depth
  40. */
  41. private function writeNode(NodeInterface $node, $depth = 0)
  42. {
  43. $comments = array();
  44. $default = '';
  45. $defaultArray = null;
  46. $children = null;
  47. $example = $node->getExample();
  48. // defaults
  49. if ($node instanceof ArrayNode) {
  50. $children = $node->getChildren();
  51. if ($node instanceof PrototypedArrayNode) {
  52. $prototype = $node->getPrototype();
  53. if ($prototype instanceof ArrayNode) {
  54. $children = $prototype->getChildren();
  55. }
  56. // check for attribute as key
  57. if ($key = $node->getKeyAttribute()) {
  58. $keyNodeClass = 'Symfony\Component\Config\Definition\\'.($prototype instanceof ArrayNode ? 'ArrayNode' : 'ScalarNode');
  59. $keyNode = new $keyNodeClass($key, $node);
  60. $info = 'Prototype';
  61. if (null !== $prototype->getInfo()) {
  62. $info .= ': '.$prototype->getInfo();
  63. }
  64. $keyNode->setInfo($info);
  65. // add children
  66. foreach ($children as $childNode) {
  67. $keyNode->addChild($childNode);
  68. }
  69. $children = array($key => $keyNode);
  70. }
  71. }
  72. if (!$children) {
  73. if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
  74. $default = '';
  75. } elseif (!\is_array($example)) {
  76. $default = '[]';
  77. }
  78. }
  79. } elseif ($node instanceof EnumNode) {
  80. $comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
  81. $default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
  82. } else {
  83. $default = '~';
  84. if ($node->hasDefaultValue()) {
  85. $default = $node->getDefaultValue();
  86. if (\is_array($default)) {
  87. if (\count($defaultArray = $node->getDefaultValue())) {
  88. $default = '';
  89. } elseif (!\is_array($example)) {
  90. $default = '[]';
  91. }
  92. } else {
  93. $default = Inline::dump($default);
  94. }
  95. }
  96. }
  97. // required?
  98. if ($node->isRequired()) {
  99. $comments[] = 'Required';
  100. }
  101. // example
  102. if ($example && !\is_array($example)) {
  103. $comments[] = 'Example: '.$example;
  104. }
  105. $default = '' != (string) $default ? ' '.$default : '';
  106. $comments = \count($comments) ? '# '.implode(', ', $comments) : '';
  107. $text = rtrim(sprintf('%-21s%s %s', $node->getName().':', $default, $comments), ' ');
  108. if ($info = $node->getInfo()) {
  109. $this->writeLine('');
  110. // indenting multi-line info
  111. $info = str_replace("\n", sprintf("\n%".($depth * 4).'s# ', ' '), $info);
  112. $this->writeLine('# '.$info, $depth * 4);
  113. }
  114. $this->writeLine($text, $depth * 4);
  115. // output defaults
  116. if ($defaultArray) {
  117. $this->writeLine('');
  118. $message = \count($defaultArray) > 1 ? 'Defaults' : 'Default';
  119. $this->writeLine('# '.$message.':', $depth * 4 + 4);
  120. $this->writeArray($defaultArray, $depth + 1);
  121. }
  122. if (\is_array($example)) {
  123. $this->writeLine('');
  124. $message = \count($example) > 1 ? 'Examples' : 'Example';
  125. $this->writeLine('# '.$message.':', $depth * 4 + 4);
  126. $this->writeArray($example, $depth + 1);
  127. }
  128. if ($children) {
  129. foreach ($children as $childNode) {
  130. $this->writeNode($childNode, $depth + 1);
  131. }
  132. }
  133. }
  134. /**
  135. * Outputs a single config reference line.
  136. *
  137. * @param string $text
  138. * @param int $indent
  139. */
  140. private function writeLine($text, $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. }