123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- namespace Symfony\Component\Config\Definition\Dumper;
- use Symfony\Component\Config\Definition\ArrayNode;
- use Symfony\Component\Config\Definition\ConfigurationInterface;
- use Symfony\Component\Config\Definition\EnumNode;
- use Symfony\Component\Config\Definition\NodeInterface;
- use Symfony\Component\Config\Definition\PrototypedArrayNode;
- class XmlReferenceDumper
- {
- private $reference;
- public function dump(ConfigurationInterface $configuration, $namespace = null)
- {
- return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace);
- }
- public function dumpNode(NodeInterface $node, $namespace = null)
- {
- $this->reference = '';
- $this->writeNode($node, 0, true, $namespace);
- $ref = $this->reference;
- $this->reference = null;
- return $ref;
- }
-
- private function writeNode(NodeInterface $node, $depth = 0, $root = false, $namespace = null)
- {
- $rootName = ($root ? 'config' : $node->getName());
- $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null));
-
- if ($node->getParent()) {
- $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) {
- return $rootName === $mapping[1];
- });
- if (\count($remapping)) {
- list($singular) = current($remapping);
- $rootName = $singular;
- }
- }
- $rootName = str_replace('_', '-', $rootName);
- $rootAttributes = array();
- $rootAttributeComments = array();
- $rootChildren = array();
- $rootComments = array();
- if ($node instanceof ArrayNode) {
- $children = $node->getChildren();
-
- if ($rootInfo = $node->getInfo()) {
- $rootComments[] = $rootInfo;
- }
- if ($rootNamespace) {
- $rootComments[] = 'Namespace: '.$rootNamespace;
- }
-
- if ($node instanceof PrototypedArrayNode) {
- $prototype = $node->getPrototype();
- $info = 'prototype';
- if (null !== $prototype->getInfo()) {
- $info .= ': '.$prototype->getInfo();
- }
- array_unshift($rootComments, $info);
- if ($key = $node->getKeyAttribute()) {
- $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key;
- }
- if ($prototype instanceof ArrayNode) {
- $children = $prototype->getChildren();
- } else {
- if ($prototype->hasDefaultValue()) {
- $prototypeValue = $prototype->getDefaultValue();
- } else {
- switch (\get_class($prototype)) {
- case 'Symfony\Component\Config\Definition\ScalarNode':
- $prototypeValue = 'scalar value';
- break;
- case 'Symfony\Component\Config\Definition\FloatNode':
- case 'Symfony\Component\Config\Definition\IntegerNode':
- $prototypeValue = 'numeric value';
- break;
- case 'Symfony\Component\Config\Definition\BooleanNode':
- $prototypeValue = 'true|false';
- break;
- case 'Symfony\Component\Config\Definition\EnumNode':
- $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues()));
- break;
- default:
- $prototypeValue = 'value';
- }
- }
- }
- }
-
- foreach ($children as $child) {
- if (!$child instanceof ArrayNode) {
-
-
- $name = str_replace('_', '-', $child->getName());
- $value = '%%%%not_defined%%%%';
-
- $comments = array();
- if ($info = $child->getInfo()) {
- $comments[] = $info;
- }
- if ($example = $child->getExample()) {
- $comments[] = 'Example: '.$example;
- }
- if ($child->isRequired()) {
- $comments[] = 'Required';
- }
- if ($child instanceof EnumNode) {
- $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
- }
- if (\count($comments)) {
- $rootAttributeComments[$name] = implode(";\n", $comments);
- }
-
- if ($child->hasDefaultValue()) {
- $value = $child->getDefaultValue();
- }
-
- $rootAttributes[$name] = $value;
- } else {
-
- $rootChildren[] = $child;
- }
- }
- }
-
-
- if (\count($rootComments)) {
- foreach ($rootComments as $comment) {
- $this->writeLine('<!-- '.$comment.' -->', $depth);
- }
- }
-
- if (\count($rootAttributeComments)) {
- foreach ($rootAttributeComments as $attrName => $comment) {
- $commentDepth = $depth + 4 + \strlen($attrName) + 2;
- $commentLines = explode("\n", $comment);
- $multiline = (\count($commentLines) > 1);
- $comment = implode(PHP_EOL.str_repeat(' ', $commentDepth), $commentLines);
- if ($multiline) {
- $this->writeLine('<!--', $depth);
- $this->writeLine($attrName.': '.$comment, $depth + 4);
- $this->writeLine('-->', $depth);
- } else {
- $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth);
- }
- }
- }
-
- $rootIsVariablePrototype = isset($prototypeValue);
- $rootIsEmptyTag = (0 === \count($rootChildren) && !$rootIsVariablePrototype);
- $rootOpenTag = '<'.$rootName;
- if (1 >= ($attributesCount = \count($rootAttributes))) {
- if (1 === $attributesCount) {
- $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes)));
- }
- $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>';
- if ($rootIsVariablePrototype) {
- $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
- }
- $this->writeLine($rootOpenTag, $depth);
- } else {
- $this->writeLine($rootOpenTag, $depth);
- $i = 1;
- foreach ($rootAttributes as $attrName => $attrValue) {
- $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue));
- $this->writeLine($attr, $depth + 4);
- if ($attributesCount === $i++) {
- $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth);
- if ($rootIsVariablePrototype) {
- $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
- }
- }
- }
- }
-
- foreach ($rootChildren as $child) {
- $this->writeLine('');
- $this->writeNode($child, $depth + 4);
- }
-
- if (!$rootIsEmptyTag && !$rootIsVariablePrototype) {
- $this->writeLine('');
- $rootEndTag = '</'.$rootName.'>';
- $this->writeLine($rootEndTag, $depth);
- }
- }
-
- private function writeLine($text, $indent = 0)
- {
- $indent = \strlen($text) + $indent;
- $format = '%'.$indent.'s';
- $this->reference .= sprintf($format, $text).PHP_EOL;
- }
-
- private function writeValue($value)
- {
- if ('%%%%not_defined%%%%' === $value) {
- return '';
- }
- if (\is_string($value) || is_numeric($value)) {
- return $value;
- }
- if (false === $value) {
- return 'false';
- }
- if (true === $value) {
- return 'true';
- }
- if (null === $value) {
- return 'null';
- }
- if (empty($value)) {
- return '';
- }
- if (\is_array($value)) {
- return implode(',', $value);
- }
- }
- }
|