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
12namespace Symfony\Component\Console\Descriptor;
13
14use Symfony\Component\Console\Application;
15use Symfony\Component\Console\Command\Command;
16use Symfony\Component\Console\Input\InputArgument;
17use Symfony\Component\Console\Input\InputDefinition;
18use Symfony\Component\Console\Input\InputOption;
19
20/**
21 * XML descriptor.
22 *
23 * @author Jean-François Simon <contact@jfsimon.fr>
24 *
25 * @internal
26 */
27class XmlDescriptor extends Descriptor
28{
29    /**
30     * @return \DOMDocument
31     */
32    public function getInputDefinitionDocument(InputDefinition $definition)
33    {
34        $dom = new \DOMDocument('1.0', 'UTF-8');
35        $dom->appendChild($definitionXML = $dom->createElement('definition'));
36
37        $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
38        foreach ($definition->getArguments() as $argument) {
39            $this->appendDocument($argumentsXML, $this->getInputArgumentDocument($argument));
40        }
41
42        $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
43        foreach ($definition->getOptions() as $option) {
44            $this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
45        }
46
47        return $dom;
48    }
49
50    /**
51     * @return \DOMDocument
52     */
53    public function getCommandDocument(Command $command)
54    {
55        $dom = new \DOMDocument('1.0', 'UTF-8');
56        $dom->appendChild($commandXML = $dom->createElement('command'));
57
58        $command->getSynopsis();
59        $command->mergeApplicationDefinition(false);
60
61        $commandXML->setAttribute('id', $command->getName());
62        $commandXML->setAttribute('name', $command->getName());
63        $commandXML->setAttribute('hidden', $command->isHidden() ? 1 : 0);
64
65        $commandXML->appendChild($usagesXML = $dom->createElement('usages'));
66
67        foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
68            $usagesXML->appendChild($dom->createElement('usage', $usage));
69        }
70
71        $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
72        $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
73
74        $commandXML->appendChild($helpXML = $dom->createElement('help'));
75        $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
76
77        $definitionXML = $this->getInputDefinitionDocument($command->getNativeDefinition());
78        $this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
79
80        return $dom;
81    }
82
83    /**
84     * @param string|null $namespace
85     *
86     * @return \DOMDocument
87     */
88    public function getApplicationDocument(Application $application, $namespace = null)
89    {
90        $dom = new \DOMDocument('1.0', 'UTF-8');
91        $dom->appendChild($rootXml = $dom->createElement('symfony'));
92
93        if ('UNKNOWN' !== $application->getName()) {
94            $rootXml->setAttribute('name', $application->getName());
95            if ('UNKNOWN' !== $application->getVersion()) {
96                $rootXml->setAttribute('version', $application->getVersion());
97            }
98        }
99
100        $rootXml->appendChild($commandsXML = $dom->createElement('commands'));
101
102        $description = new ApplicationDescription($application, $namespace, true);
103
104        if ($namespace) {
105            $commandsXML->setAttribute('namespace', $namespace);
106        }
107
108        foreach ($description->getCommands() as $command) {
109            $this->appendDocument($commandsXML, $this->getCommandDocument($command));
110        }
111
112        if (!$namespace) {
113            $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));
114
115            foreach ($description->getNamespaces() as $namespaceDescription) {
116                $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
117                $namespaceArrayXML->setAttribute('id', $namespaceDescription['id']);
118
119                foreach ($namespaceDescription['commands'] as $name) {
120                    $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
121                    $commandXML->appendChild($dom->createTextNode($name));
122                }
123            }
124        }
125
126        return $dom;
127    }
128
129    /**
130     * {@inheritdoc}
131     */
132    protected function describeInputArgument(InputArgument $argument, array $options = [])
133    {
134        $this->writeDocument($this->getInputArgumentDocument($argument));
135    }
136
137    /**
138     * {@inheritdoc}
139     */
140    protected function describeInputOption(InputOption $option, array $options = [])
141    {
142        $this->writeDocument($this->getInputOptionDocument($option));
143    }
144
145    /**
146     * {@inheritdoc}
147     */
148    protected function describeInputDefinition(InputDefinition $definition, array $options = [])
149    {
150        $this->writeDocument($this->getInputDefinitionDocument($definition));
151    }
152
153    /**
154     * {@inheritdoc}
155     */
156    protected function describeCommand(Command $command, array $options = [])
157    {
158        $this->writeDocument($this->getCommandDocument($command));
159    }
160
161    /**
162     * {@inheritdoc}
163     */
164    protected function describeApplication(Application $application, array $options = [])
165    {
166        $this->writeDocument($this->getApplicationDocument($application, isset($options['namespace']) ? $options['namespace'] : null));
167    }
168
169    /**
170     * Appends document children to parent node.
171     */
172    private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
173    {
174        foreach ($importedParent->childNodes as $childNode) {
175            $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
176        }
177    }
178
179    /**
180     * Writes DOM document.
181     */
182    private function writeDocument(\DOMDocument $dom)
183    {
184        $dom->formatOutput = true;
185        $this->write($dom->saveXML());
186    }
187
188    /**
189     * @return \DOMDocument
190     */
191    private function getInputArgumentDocument(InputArgument $argument)
192    {
193        $dom = new \DOMDocument('1.0', 'UTF-8');
194
195        $dom->appendChild($objectXML = $dom->createElement('argument'));
196        $objectXML->setAttribute('name', $argument->getName());
197        $objectXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
198        $objectXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
199        $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
200        $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
201
202        $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
203        $defaults = \is_array($argument->getDefault()) ? $argument->getDefault() : (\is_bool($argument->getDefault()) ? [var_export($argument->getDefault(), true)] : ($argument->getDefault() ? [$argument->getDefault()] : []));
204        foreach ($defaults as $default) {
205            $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
206            $defaultXML->appendChild($dom->createTextNode($default));
207        }
208
209        return $dom;
210    }
211
212    /**
213     * @return \DOMDocument
214     */
215    private function getInputOptionDocument(InputOption $option)
216    {
217        $dom = new \DOMDocument('1.0', 'UTF-8');
218
219        $dom->appendChild($objectXML = $dom->createElement('option'));
220        $objectXML->setAttribute('name', '--'.$option->getName());
221        $pos = strpos($option->getShortcut(), '|');
222        if (false !== $pos) {
223            $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
224            $objectXML->setAttribute('shortcuts', '-'.str_replace('|', '|-', $option->getShortcut()));
225        } else {
226            $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
227        }
228        $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
229        $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
230        $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
231        $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
232        $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
233
234        if ($option->acceptValue()) {
235            $defaults = \is_array($option->getDefault()) ? $option->getDefault() : (\is_bool($option->getDefault()) ? [var_export($option->getDefault(), true)] : ($option->getDefault() ? [$option->getDefault()] : []));
236            $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
237
238            if (!empty($defaults)) {
239                foreach ($defaults as $default) {
240                    $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
241                    $defaultXML->appendChild($dom->createTextNode($default));
242                }
243            }
244        }
245
246        return $dom;
247    }
248}
249