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\Config\Definition\ScalarNode;
20 use Symfony\Component\Yaml\Inline;
23 * Dumps a Yaml reference configuration for the given configuration/node instance.
25 * @author Kevin Bond <kevinbond@gmail.com>
27 class YamlReferenceDumper
31 public function dump(ConfigurationInterface $configuration)
33 return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
36 public function dumpNode(NodeInterface $node)
38 $this->reference = '';
39 $this->writeNode($node);
40 $ref = $this->reference;
41 $this->reference = null;
47 * @param NodeInterface $node
49 * @param bool $prototypedArray
51 private function writeNode(NodeInterface $node, $depth = 0, $prototypedArray = false)
57 $example = $node->getExample();
60 if ($node instanceof ArrayNode) {
61 $children = $node->getChildren();
63 if ($node instanceof PrototypedArrayNode) {
64 $children = $this->getPrototypeChildren($node);
68 if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) {
70 } elseif (!is_array($example)) {
74 } elseif ($node instanceof EnumNode) {
75 $comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
76 $default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
80 if ($node->hasDefaultValue()) {
81 $default = $node->getDefaultValue();
83 if (is_array($default)) {
84 if (count($defaultArray = $node->getDefaultValue())) {
86 } elseif (!is_array($example)) {
90 $default = Inline::dump($default);
96 if ($node->isRequired()) {
97 $comments[] = 'Required';
101 if ($example && !is_array($example)) {
102 $comments[] = 'Example: '.$example;
105 $default = (string) $default != '' ? ' '.$default : '';
106 $comments = count($comments) ? '# '.implode(', ', $comments) : '';
108 $key = $prototypedArray ? '-' : $node->getName().':';
109 $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);
118 $this->writeLine($text, $depth * 4);
122 $this->writeLine('');
124 $message = count($defaultArray) > 1 ? 'Defaults' : 'Default';
126 $this->writeLine('# '.$message.':', $depth * 4 + 4);
128 $this->writeArray($defaultArray, $depth + 1);
131 if (is_array($example)) {
132 $this->writeLine('');
134 $message = count($example) > 1 ? 'Examples' : 'Example';
136 $this->writeLine('# '.$message.':', $depth * 4 + 4);
138 $this->writeArray($example, $depth + 1);
142 foreach ($children as $childNode) {
143 $this->writeNode($childNode, $depth + 1, $node instanceof PrototypedArrayNode && !$node->getKeyAttribute());
149 * Outputs a single config reference line.
151 * @param string $text
154 private function writeLine($text, $indent = 0)
156 $indent = strlen($text) + $indent;
157 $format = '%'.$indent.'s';
159 $this->reference .= sprintf($format, $text)."\n";
162 private function writeArray(array $array, $depth)
164 $isIndexed = array_values($array) === $array;
166 foreach ($array as $key => $value) {
167 if (is_array($value)) {
174 $this->writeLine('- '.$val, $depth * 4);
176 $this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
179 if (is_array($value)) {
180 $this->writeArray($value, $depth + 1);
186 * @param PrototypedArrayNode $node
190 private function getPrototypeChildren(PrototypedArrayNode $node)
192 $prototype = $node->getPrototype();
193 $key = $node->getKeyAttribute();
195 // Do not expand prototype if it isn't an array node nor uses attribute as key
196 if (!$key && !$prototype instanceof ArrayNode) {
197 return $node->getChildren();
200 if ($prototype instanceof ArrayNode) {
201 $keyNode = new ArrayNode($key, $node);
202 $children = $prototype->getChildren();
204 if ($prototype instanceof PrototypedArrayNode && $prototype->getKeyAttribute()) {
205 $children = $this->getPrototypeChildren($prototype);
209 foreach ($children as $childNode) {
210 $keyNode->addChild($childNode);
213 $keyNode = new ScalarNode($key, $node);
217 if (null !== $prototype->getInfo()) {
218 $info .= ': '.$prototype->getInfo();
220 $keyNode->setInfo($info);
222 return array($key => $keyNode);