|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Config\Definition\Dumper; |
| 13 | + |
| 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 | + |
| 20 | +/** |
| 21 | + * Dumps a XML reference configuration for the given configuration/node instance. |
| 22 | + * |
| 23 | + * @author Wouter J <waldio.webdesign@gmail.com> |
| 24 | + */ |
| 25 | +class XmlReferenceDumper |
| 26 | +{ |
| 27 | + private $reference; |
| 28 | + |
| 29 | + public function dump(ConfigurationInterface $configuration, $namespace = null) |
| 30 | + { |
| 31 | + return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace); |
| 32 | + } |
| 33 | + |
| 34 | + public function dumpNode(NodeInterface $node, $namespace = null) |
| 35 | + { |
| 36 | + $this->reference = ''; |
| 37 | + $this->writeNode($node, 0, true, $namespace); |
| 38 | + $ref = $this->reference; |
| 39 | + $this->reference = null; |
| 40 | + |
| 41 | + return $ref; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param NodeInterface $node |
| 46 | + * @param integer $depth |
| 47 | + * @param boolean $root If the node is the root node |
| 48 | + * @param string $namespace The namespace of the node |
| 49 | + */ |
| 50 | + private function writeNode(NodeInterface $node, $depth = 0, $root = false, $namespace = null) |
| 51 | + { |
| 52 | + $rootName = ($root ? 'config' : $node->getName()); |
| 53 | + $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null)); |
| 54 | + |
| 55 | + // xml remapping |
| 56 | + if ($node->getParent()) { |
| 57 | + $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) { |
| 58 | + return $rootName === $mapping[1]; |
| 59 | + }); |
| 60 | + |
| 61 | + if (count($remapping)) { |
| 62 | + list($singular, $plural) = current($remapping); |
| 63 | + $rootName = $singular; |
| 64 | + } |
| 65 | + } |
| 66 | + $rootName = str_replace('_', '-', $rootName); |
| 67 | + |
| 68 | + $rootAttributes = array(); |
| 69 | + $rootAttributeComments = array(); |
| 70 | + $rootChildren = array(); |
| 71 | + $rootComments = array(); |
| 72 | + |
| 73 | + if ($node instanceof ArrayNode) { |
| 74 | + $children = $node->getChildren(); |
| 75 | + |
| 76 | + // comments about the root node |
| 77 | + if ($rootInfo = $node->getInfo()) { |
| 78 | + $rootComments[] = $rootInfo; |
| 79 | + } |
| 80 | + |
| 81 | + if ($rootNamespace) { |
| 82 | + $rootComments[] = 'Namespace: '.$rootNamespace; |
| 83 | + } |
| 84 | + |
| 85 | + // render prototyped nodes |
| 86 | + if ($node instanceof PrototypedArrayNode) { |
| 87 | + array_unshift($rootComments, 'prototype'); |
| 88 | + |
| 89 | + if ($key = $node->getKeyAttribute()) { |
| 90 | + $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key; |
| 91 | + } |
| 92 | + |
| 93 | + $prototype = $node->getPrototype(); |
| 94 | + |
| 95 | + if ($prototype instanceof ArrayNode) { |
| 96 | + $children = $prototype->getChildren(); |
| 97 | + } else { |
| 98 | + if ($prototype->hasDefaultValue()) { |
| 99 | + $prototypeValue = $prototype->getDefaultValue(); |
| 100 | + } else { |
| 101 | + switch (get_class($prototype)) { |
| 102 | + case 'Symfony\Component\Config\Definition\ScalarNode': |
| 103 | + $prototypeValue = 'scalar value'; |
| 104 | + break; |
| 105 | + |
| 106 | + case 'Symfony\Component\Config\Definition\FloatNode': |
| 107 | + case 'Symfony\Component\Config\Definition\IntegerNode': |
| 108 | + $prototypeValue = 'numeric value'; |
| 109 | + break; |
| 110 | + |
| 111 | + case 'Symfony\Component\Config\Definition\BooleanNode': |
| 112 | + $prototypeValue = 'true|false'; |
| 113 | + break; |
| 114 | + |
| 115 | + case 'Symfony\Component\Config\Definition\EnumNode': |
| 116 | + $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues())); |
| 117 | + break; |
| 118 | + |
| 119 | + default: |
| 120 | + $prototypeValue = 'value'; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // get attributes and elements |
| 127 | + foreach ($children as $child) { |
| 128 | + if (!$child instanceof ArrayNode) { |
| 129 | + // get attributes |
| 130 | + |
| 131 | + // metadata |
| 132 | + $name = str_replace('_', '-', $child->getName()); |
| 133 | + $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world |
| 134 | + |
| 135 | + // comments |
| 136 | + $comments = array(); |
| 137 | + if ($info = $child->getInfo()) { |
| 138 | + $comments[] = $info; |
| 139 | + } |
| 140 | + |
| 141 | + if ($example = $child->getExample()) { |
| 142 | + $comments[] = 'Example: '.$example; |
| 143 | + } |
| 144 | + |
| 145 | + if ($child->isRequired()) { |
| 146 | + $comments[] = 'Required'; |
| 147 | + } |
| 148 | + |
| 149 | + if ($child instanceof EnumNode) { |
| 150 | + $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues())); |
| 151 | + } |
| 152 | + |
| 153 | + if (count($comments)) { |
| 154 | + $rootAttributeComments[$name] = implode(";\n", $comments); |
| 155 | + } |
| 156 | + |
| 157 | + // default values |
| 158 | + if ($child->hasDefaultValue()) { |
| 159 | + $value = $child->getDefaultValue(); |
| 160 | + } |
| 161 | + |
| 162 | + // append attribute |
| 163 | + $rootAttributes[$name] = $value; |
| 164 | + } else { |
| 165 | + // get elements |
| 166 | + $rootChildren[] = $child; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + // render comments |
| 172 | + |
| 173 | + // root node comment |
| 174 | + if (count($rootComments)) { |
| 175 | + foreach ($rootComments as $comment) { |
| 176 | + $this->writeLine('<!-- '.$comment.' -->', $depth); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // attribute comments |
| 181 | + if (count($rootAttributeComments)) { |
| 182 | + foreach ($rootAttributeComments as $attrName => $comment) { |
| 183 | + $commentDepth = $depth + 4 + strlen($attrName) + 2; |
| 184 | + $commentLines = explode("\n", $comment); |
| 185 | + $multiline = (count($commentLines) > 1); |
| 186 | + $comment = implode(PHP_EOL.str_repeat(' ', $commentDepth), $commentLines); |
| 187 | + |
| 188 | + if ($multiline) { |
| 189 | + $this->writeLine('<!--', $depth); |
| 190 | + $this->writeLine($attrName.': '.$comment, $depth + 4); |
| 191 | + $this->writeLine('-->', $depth); |
| 192 | + } else { |
| 193 | + $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // render start tag + attributes |
| 199 | + $rootIsVariablePrototype = isset($prototypeValue); |
| 200 | + $rootIsEmptyTag = (0 === count($rootChildren) && !$rootIsVariablePrototype); |
| 201 | + $rootOpenTag = '<'.$rootName; |
| 202 | + if (1 >= ($attributesCount = count($rootAttributes))) { |
| 203 | + if (1 === $attributesCount) { |
| 204 | + $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes))); |
| 205 | + } |
| 206 | + |
| 207 | + $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>'; |
| 208 | + |
| 209 | + if ($rootIsVariablePrototype) { |
| 210 | + $rootOpenTag .= $prototypeValue.'</'.$rootName.'>'; |
| 211 | + } |
| 212 | + |
| 213 | + $this->writeLine($rootOpenTag, $depth); |
| 214 | + } else { |
| 215 | + $this->writeLine($rootOpenTag, $depth); |
| 216 | + |
| 217 | + $i = 1; |
| 218 | + |
| 219 | + foreach ($rootAttributes as $attrName => $attrValue) { |
| 220 | + $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue)); |
| 221 | + |
| 222 | + $this->writeLine($attr, $depth + 4); |
| 223 | + |
| 224 | + if ($attributesCount === $i++) { |
| 225 | + $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth); |
| 226 | + |
| 227 | + if ($rootIsVariablePrototype) { |
| 228 | + $rootOpenTag .= $prototypeValue.'</'.$rootName.'>'; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // render children tags |
| 235 | + foreach ($rootChildren as $child) { |
| 236 | + $this->writeLine(''); |
| 237 | + $this->writeNode($child, $depth + 4); |
| 238 | + } |
| 239 | + |
| 240 | + // render end tag |
| 241 | + if (!$rootIsEmptyTag && !$rootIsVariablePrototype) { |
| 242 | + $this->writeLine(''); |
| 243 | + |
| 244 | + $rootEndTag = '</'.$rootName.'>'; |
| 245 | + $this->writeLine($rootEndTag, $depth); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Outputs a single config reference line |
| 251 | + * |
| 252 | + * @param string $text |
| 253 | + * @param int $indent |
| 254 | + */ |
| 255 | + private function writeLine($text, $indent = 0) |
| 256 | + { |
| 257 | + $indent = strlen($text) + $indent; |
| 258 | + $format = '%'.$indent.'s'; |
| 259 | + |
| 260 | + $this->reference .= sprintf($format, $text)."\n"; |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * Renders the string conversion of the value. |
| 265 | + * |
| 266 | + * @param mixed $value |
| 267 | + * |
| 268 | + * @return string |
| 269 | + */ |
| 270 | + private function writeValue($value) |
| 271 | + { |
| 272 | + if ('%%%%not_defined%%%%' === $value) { |
| 273 | + return ''; |
| 274 | + } |
| 275 | + |
| 276 | + if (is_string($value) || is_numeric($value)) { |
| 277 | + return $value; |
| 278 | + } |
| 279 | + |
| 280 | + if (false === $value) { |
| 281 | + return 'false'; |
| 282 | + } |
| 283 | + |
| 284 | + if (true === $value) { |
| 285 | + return 'true'; |
| 286 | + } |
| 287 | + |
| 288 | + if (null === $value) { |
| 289 | + return 'null'; |
| 290 | + } |
| 291 | + |
| 292 | + if (empty($value)) { |
| 293 | + return ''; |
| 294 | + } |
| 295 | + |
| 296 | + if (is_array($value)) { |
| 297 | + return implode(',', $value); |
| 298 | + } |
| 299 | + } |
| 300 | +} |
0 commit comments