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\ArrayNode;
15 use Symfony\Component\Config\Definition\ConfigurationInterface;
16 use Symfony\Component\Config\Definition\EnumNode;
17 use Symfony\Component\Config\Definition\NodeInterface;
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 PrototypedArrayNode) {
100 $prototype->setName($key);
101 $children = array($key => $prototype);
102 } elseif ($prototype instanceof ArrayNode) {
103 $children = $prototype->getChildren();
105 if ($prototype->hasDefaultValue()) {
106 $prototypeValue = $prototype->getDefaultValue();
108 switch (\get_class($prototype)) {
109 case 'Symfony\Component\Config\Definition\ScalarNode':
110 $prototypeValue = 'scalar value';
113 case 'Symfony\Component\Config\Definition\FloatNode':
114 case 'Symfony\Component\Config\Definition\IntegerNode':
115 $prototypeValue = 'numeric value';
118 case 'Symfony\Component\Config\Definition\BooleanNode':
119 $prototypeValue = 'true|false';
122 case 'Symfony\Component\Config\Definition\EnumNode':
123 $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues()));
127 $prototypeValue = 'value';
133 // get attributes and elements
134 foreach ($children as $child) {
135 if (!$child instanceof ArrayNode) {
139 $name = str_replace('_', '-', $child->getName());
140 $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
144 if ($info = $child->getInfo()) {
148 if ($example = $child->getExample()) {
149 $comments[] = 'Example: '.$example;
152 if ($child->isRequired()) {
153 $comments[] = 'Required';
156 if ($child->isDeprecated()) {
157 $comments[] = sprintf('Deprecated (%s)', $child->getDeprecationMessage($child->getName(), $node->getPath()));
160 if ($child instanceof EnumNode) {
161 $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
164 if (\count($comments)) {
165 $rootAttributeComments[$name] = implode(";\n", $comments);
169 if ($child->hasDefaultValue()) {
170 $value = $child->getDefaultValue();
174 $rootAttributes[$name] = $value;
177 $rootChildren[] = $child;
185 if (\count($rootComments)) {
186 foreach ($rootComments as $comment) {
187 $this->writeLine('<!-- '.$comment.' -->', $depth);
191 // attribute comments
192 if (\count($rootAttributeComments)) {
193 foreach ($rootAttributeComments as $attrName => $comment) {
194 $commentDepth = $depth + 4 + \strlen($attrName) + 2;
195 $commentLines = explode("\n", $comment);
196 $multiline = (\count($commentLines) > 1);
197 $comment = implode(PHP_EOL.str_repeat(' ', $commentDepth), $commentLines);
200 $this->writeLine('<!--', $depth);
201 $this->writeLine($attrName.': '.$comment, $depth + 4);
202 $this->writeLine('-->', $depth);
204 $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth);
209 // render start tag + attributes
210 $rootIsVariablePrototype = isset($prototypeValue);
211 $rootIsEmptyTag = (0 === \count($rootChildren) && !$rootIsVariablePrototype);
212 $rootOpenTag = '<'.$rootName;
213 if (1 >= ($attributesCount = \count($rootAttributes))) {
214 if (1 === $attributesCount) {
215 $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes)));
218 $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>';
220 if ($rootIsVariablePrototype) {
221 $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
224 $this->writeLine($rootOpenTag, $depth);
226 $this->writeLine($rootOpenTag, $depth);
230 foreach ($rootAttributes as $attrName => $attrValue) {
231 $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue));
233 $this->writeLine($attr, $depth + 4);
235 if ($attributesCount === $i++) {
236 $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth);
238 if ($rootIsVariablePrototype) {
239 $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
245 // render children tags
246 foreach ($rootChildren as $child) {
247 $this->writeLine('');
248 $this->writeNode($child, $depth + 4);
252 if (!$rootIsEmptyTag && !$rootIsVariablePrototype) {
253 $this->writeLine('');
255 $rootEndTag = '</'.$rootName.'>';
256 $this->writeLine($rootEndTag, $depth);
261 * Outputs a single config reference line.
263 * @param string $text
266 private function writeLine($text, $indent = 0)
268 $indent = \strlen($text) + $indent;
269 $format = '%'.$indent.'s';
271 $this->reference .= sprintf($format, $text).PHP_EOL;
275 * Renders the string conversion of the value.
277 * @param mixed $value
281 private function writeValue($value)
283 if ('%%%%not_defined%%%%' === $value) {
287 if (\is_string($value) || is_numeric($value)) {
291 if (false === $value) {
295 if (true === $value) {
299 if (null === $value) {
307 if (\is_array($value)) {
308 return implode(',', $value);