4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Definition\Dumper;
14 use Symfony\Component\Config\Definition\ConfigurationInterface;
15 use Symfony\Component\Config\Definition\NodeInterface;
16 use Symfony\Component\Config\Definition\ArrayNode;
17 use Symfony\Component\Config\Definition\EnumNode;
18 use Symfony\Component\Config\Definition\PrototypedArrayNode;
19 use Symfony\Component\Yaml\Inline;
22 * Dumps a Yaml reference configuration for the given configuration/node instance.
24 * @author Kevin Bond <kevinbond@gmail.com>
26 class YamlReferenceDumper
30 public function dump(ConfigurationInterface $configuration)
32 return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
35 public function dumpNode(NodeInterface $node)
37 $this->reference = '';
38 $this->writeNode($node);
39 $ref = $this->reference;
40 $this->reference = null;
46 * @param NodeInterface $node
49 private function writeNode(NodeInterface $node, $depth = 0)
55 $example = $node->getExample();
58 if ($node instanceof ArrayNode) {
59 $children = $node->getChildren();
61 if ($node instanceof PrototypedArrayNode) {
62 $prototype = $node->getPrototype();
64 if ($prototype instanceof ArrayNode) {
65 $children = $prototype->getChildren();
68 // check for attribute as key
69 if ($key = $node->getKeyAttribute()) {
70 $keyNodeClass = 'Symfony\Component\Config\Definition\\'.($prototype instanceof ArrayNode ? 'ArrayNode' : 'ScalarNode');
71 $keyNode = new $keyNodeClass($key, $node);
74 if (null !== $prototype->getInfo()) {
75 $info .= ': '.$prototype->getInfo();
77 $keyNode->setInfo($info);
80 foreach ($children as $childNode) {
81 $keyNode->addChild($childNode);
83 $children = array($key => $keyNode);
88 if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) {
90 } elseif (!is_array($example)) {
94 } elseif ($node instanceof EnumNode) {
95 $comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
96 $default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
100 if ($node->hasDefaultValue()) {
101 $default = $node->getDefaultValue();
103 if (is_array($default)) {
104 if (count($defaultArray = $node->getDefaultValue())) {
106 } elseif (!is_array($example)) {
110 $default = Inline::dump($default);
116 if ($node->isRequired()) {
117 $comments[] = 'Required';
121 if ($example && !is_array($example)) {
122 $comments[] = 'Example: '.$example;
125 $default = (string) $default != '' ? ' '.$default : '';
126 $comments = count($comments) ? '# '.implode(', ', $comments) : '';
128 $text = rtrim(sprintf('%-21s%s %s', $node->getName().':', $default, $comments), ' ');
130 if ($info = $node->getInfo()) {
131 $this->writeLine('');
132 // indenting multi-line info
133 $info = str_replace("\n", sprintf("\n%".($depth * 4).'s# ', ' '), $info);
134 $this->writeLine('# '.$info, $depth * 4);
137 $this->writeLine($text, $depth * 4);
141 $this->writeLine('');
143 $message = count($defaultArray) > 1 ? 'Defaults' : 'Default';
145 $this->writeLine('# '.$message.':', $depth * 4 + 4);
147 $this->writeArray($defaultArray, $depth + 1);
150 if (is_array($example)) {
151 $this->writeLine('');
153 $message = count($example) > 1 ? 'Examples' : 'Example';
155 $this->writeLine('# '.$message.':', $depth * 4 + 4);
157 $this->writeArray($example, $depth + 1);
161 foreach ($children as $childNode) {
162 $this->writeNode($childNode, $depth + 1);
168 * Outputs a single config reference line.
170 * @param string $text
173 private function writeLine($text, $indent = 0)
175 $indent = strlen($text) + $indent;
176 $format = '%'.$indent.'s';
178 $this->reference .= sprintf($format, $text)."\n";
181 private function writeArray(array $array, $depth)
183 $isIndexed = array_values($array) === $array;
185 foreach ($array as $key => $value) {
186 if (is_array($value)) {
193 $this->writeLine('- '.$val, $depth * 4);
195 $this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
198 if (is_array($value)) {
199 $this->writeArray($value, $depth + 1);