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;
21 * Dumps a XML reference configuration for the given configuration/node instance.
23 * @author Wouter J <waldio.webdesign@gmail.com>
25 class XmlReferenceDumper
29 public function dump(ConfigurationInterface $configuration, $namespace = null)
31 return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace);
34 public function dumpNode(NodeInterface $node, $namespace = null)
36 $this->reference = '';
37 $this->writeNode($node, 0, true, $namespace);
38 $ref = $this->reference;
39 $this->reference = null;
45 * @param NodeInterface $node
47 * @param bool $root If the node is the root node
48 * @param string $namespace The namespace of the node
50 private function writeNode(NodeInterface $node, $depth = 0, $root = false, $namespace = null)
52 $rootName = ($root ? 'config' : $node->getName());
53 $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null));
56 if ($node->getParent()) {
57 $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) {
58 return $rootName === $mapping[1];
61 if (count($remapping)) {
62 list($singular) = current($remapping);
63 $rootName = $singular;
66 $rootName = str_replace('_', '-', $rootName);
68 $rootAttributes = array();
69 $rootAttributeComments = array();
70 $rootChildren = array();
71 $rootComments = array();
73 if ($node instanceof ArrayNode) {
74 $children = $node->getChildren();
76 // comments about the root node
77 if ($rootInfo = $node->getInfo()) {
78 $rootComments[] = $rootInfo;
82 $rootComments[] = 'Namespace: '.$rootNamespace;
85 // render prototyped nodes
86 if ($node instanceof PrototypedArrayNode) {
87 $prototype = $node->getPrototype();
90 if (null !== $prototype->getInfo()) {
91 $info .= ': '.$prototype->getInfo();
93 array_unshift($rootComments, $info);
95 if ($key = $node->getKeyAttribute()) {
96 $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key;
99 if ($prototype instanceof ArrayNode) {
100 $children = $prototype->getChildren();
102 if ($prototype->hasDefaultValue()) {
103 $prototypeValue = $prototype->getDefaultValue();
105 switch (get_class($prototype)) {
106 case 'Symfony\Component\Config\Definition\ScalarNode':
107 $prototypeValue = 'scalar value';
110 case 'Symfony\Component\Config\Definition\FloatNode':
111 case 'Symfony\Component\Config\Definition\IntegerNode':
112 $prototypeValue = 'numeric value';
115 case 'Symfony\Component\Config\Definition\BooleanNode':
116 $prototypeValue = 'true|false';
119 case 'Symfony\Component\Config\Definition\EnumNode':
120 $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues()));
124 $prototypeValue = 'value';
130 // get attributes and elements
131 foreach ($children as $child) {
132 if (!$child instanceof ArrayNode) {
136 $name = str_replace('_', '-', $child->getName());
137 $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
141 if ($info = $child->getInfo()) {
145 if ($example = $child->getExample()) {
146 $comments[] = 'Example: '.$example;
149 if ($child->isRequired()) {
150 $comments[] = 'Required';
153 if ($child instanceof EnumNode) {
154 $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
157 if (count($comments)) {
158 $rootAttributeComments[$name] = implode(";\n", $comments);
162 if ($child->hasDefaultValue()) {
163 $value = $child->getDefaultValue();
167 $rootAttributes[$name] = $value;
170 $rootChildren[] = $child;
178 if (count($rootComments)) {
179 foreach ($rootComments as $comment) {
180 $this->writeLine('<!-- '.$comment.' -->', $depth);
184 // attribute comments
185 if (count($rootAttributeComments)) {
186 foreach ($rootAttributeComments as $attrName => $comment) {
187 $commentDepth = $depth + 4 + strlen($attrName) + 2;
188 $commentLines = explode("\n", $comment);
189 $multiline = (count($commentLines) > 1);
190 $comment = implode(PHP_EOL.str_repeat(' ', $commentDepth), $commentLines);
193 $this->writeLine('<!--', $depth);
194 $this->writeLine($attrName.': '.$comment, $depth + 4);
195 $this->writeLine('-->', $depth);
197 $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth);
202 // render start tag + attributes
203 $rootIsVariablePrototype = isset($prototypeValue);
204 $rootIsEmptyTag = (0 === count($rootChildren) && !$rootIsVariablePrototype);
205 $rootOpenTag = '<'.$rootName;
206 if (1 >= ($attributesCount = count($rootAttributes))) {
207 if (1 === $attributesCount) {
208 $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes)));
211 $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>';
213 if ($rootIsVariablePrototype) {
214 $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
217 $this->writeLine($rootOpenTag, $depth);
219 $this->writeLine($rootOpenTag, $depth);
223 foreach ($rootAttributes as $attrName => $attrValue) {
224 $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue));
226 $this->writeLine($attr, $depth + 4);
228 if ($attributesCount === $i++) {
229 $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth);
231 if ($rootIsVariablePrototype) {
232 $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
238 // render children tags
239 foreach ($rootChildren as $child) {
240 $this->writeLine('');
241 $this->writeNode($child, $depth + 4);
245 if (!$rootIsEmptyTag && !$rootIsVariablePrototype) {
246 $this->writeLine('');
248 $rootEndTag = '</'.$rootName.'>';
249 $this->writeLine($rootEndTag, $depth);
254 * Outputs a single config reference line.
256 * @param string $text
259 private function writeLine($text, $indent = 0)
261 $indent = strlen($text) + $indent;
262 $format = '%'.$indent.'s';
264 $this->reference .= sprintf($format, $text).PHP_EOL;
268 * Renders the string conversion of the value.
270 * @param mixed $value
274 private function writeValue($value)
276 if ('%%%%not_defined%%%%' === $value) {
280 if (is_string($value) || is_numeric($value)) {
284 if (false === $value) {
288 if (true === $value) {
292 if (null === $value) {
300 if (is_array($value)) {
301 return implode(',', $value);