123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- 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;
- /**
- * Dumps an XML reference configuration for the given configuration/node instance.
- *
- * @author Wouter J <waldio.webdesign@gmail.com>
- */
- class XmlReferenceDumper
- {
- private $reference;
- public function dump(ConfigurationInterface $configuration, string $namespace = null)
- {
- return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace);
- }
- public function dumpNode(NodeInterface $node, string $namespace = null)
- {
- $this->reference = '';
- $this->writeNode($node, 0, true, $namespace);
- $ref = $this->reference;
- $this->reference = null;
- return $ref;
- }
- private function writeNode(NodeInterface $node, int $depth = 0, bool $root = false, string $namespace = null)
- {
- $rootName = ($root ? 'config' : $node->getName());
- $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null));
- // xml remapping
- if ($node->getParent()) {
- $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) {
- return $rootName === $mapping[1];
- });
- if (\count($remapping)) {
- [$singular] = current($remapping);
- $rootName = $singular;
- }
- }
- $rootName = str_replace('_', '-', $rootName);
- $rootAttributes = [];
- $rootAttributeComments = [];
- $rootChildren = [];
- $rootComments = [];
- if ($node instanceof ArrayNode) {
- $children = $node->getChildren();
- // comments about the root node
- if ($rootInfo = $node->getInfo()) {
- $rootComments[] = $rootInfo;
- }
- if ($rootNamespace) {
- $rootComments[] = 'Namespace: '.$rootNamespace;
- }
- // render prototyped nodes
- 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 PrototypedArrayNode) {
- $prototype->setName($key ?? '');
- $children = [$key => $prototype];
- } elseif ($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';
- }
- }
- }
- }
- // get attributes and elements
- foreach ($children as $child) {
- if (!$child instanceof ArrayNode) {
- // get attributes
- // metadata
- $name = str_replace('_', '-', $child->getName());
- $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
- // comments
- $comments = [];
- if ($info = $child->getInfo()) {
- $comments[] = $info;
- }
- if ($example = $child->getExample()) {
- $comments[] = 'Example: '.$example;
- }
- if ($child->isRequired()) {
- $comments[] = 'Required';
- }
- if ($child->isDeprecated()) {
- $deprecation = $child->getDeprecation($child->getName(), $node->getPath());
- $comments[] = sprintf('Deprecated (%s)', ($deprecation['package'] || $deprecation['version'] ? "Since {$deprecation['package']} {$deprecation['version']}: " : '').$deprecation['message']);
- }
- if ($child instanceof EnumNode) {
- $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
- }
- if (\count($comments)) {
- $rootAttributeComments[$name] = implode(";\n", $comments);
- }
- // default values
- if ($child->hasDefaultValue()) {
- $value = $child->getDefaultValue();
- }
- // append attribute
- $rootAttributes[$name] = $value;
- } else {
- // get elements
- $rootChildren[] = $child;
- }
- }
- }
- // render comments
- // root node comment
- if (\count($rootComments)) {
- foreach ($rootComments as $comment) {
- $this->writeLine('<!-- '.$comment.' -->', $depth);
- }
- }
- // attribute comments
- 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);
- }
- }
- }
- // render start tag + attributes
- $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.'>';
- }
- }
- }
- }
- // render children tags
- foreach ($rootChildren as $child) {
- $this->writeLine('');
- $this->writeNode($child, $depth + 4);
- }
- // render end tag
- if (!$rootIsEmptyTag && !$rootIsVariablePrototype) {
- $this->writeLine('');
- $rootEndTag = '</'.$rootName.'>';
- $this->writeLine($rootEndTag, $depth);
- }
- }
- /**
- * Outputs a single config reference line.
- */
- private function writeLine(string $text, int $indent = 0)
- {
- $indent = \strlen($text) + $indent;
- $format = '%'.$indent.'s';
- $this->reference .= sprintf($format, $text).\PHP_EOL;
- }
- /**
- * Renders the string conversion of the value.
- *
- * @param mixed $value
- */
- private function writeValue($value): string
- {
- 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);
- }
- return '';
- }
- }
|