ReferenceDumper.php 5.4 KB

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