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\Console\Descriptor;
14 use Symfony\Component\Console\Application;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputDefinition;
18 use Symfony\Component\Console\Input\InputOption;
23 * @author Jean-François Simon <contact@jfsimon.fr>
27 class XmlDescriptor extends Descriptor
30 * @return \DOMDocument
32 public function getInputDefinitionDocument(InputDefinition $definition)
34 $dom = new \DOMDocument('1.0', 'UTF-8');
35 $dom->appendChild($definitionXML = $dom->createElement('definition'));
37 $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
38 foreach ($definition->getArguments() as $argument) {
39 $this->appendDocument($argumentsXML, $this->getInputArgumentDocument($argument));
42 $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
43 foreach ($definition->getOptions() as $option) {
44 $this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
51 * @return \DOMDocument
53 public function getCommandDocument(Command $command)
55 $dom = new \DOMDocument('1.0', 'UTF-8');
56 $dom->appendChild($commandXML = $dom->createElement('command'));
58 $command->getSynopsis();
59 $command->mergeApplicationDefinition(false);
61 $commandXML->setAttribute('id', $command->getName());
62 $commandXML->setAttribute('name', $command->getName());
63 $commandXML->setAttribute('hidden', $command->isHidden() ? 1 : 0);
65 $commandXML->appendChild($usagesXML = $dom->createElement('usages'));
67 foreach (array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()) as $usage) {
68 $usagesXML->appendChild($dom->createElement('usage', $usage));
71 $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
72 $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
74 $commandXML->appendChild($helpXML = $dom->createElement('help'));
75 $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
77 $definitionXML = $this->getInputDefinitionDocument($command->getNativeDefinition());
78 $this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
84 * @param Application $application
85 * @param string|null $namespace
87 * @return \DOMDocument
89 public function getApplicationDocument(Application $application, $namespace = null)
91 $dom = new \DOMDocument('1.0', 'UTF-8');
92 $dom->appendChild($rootXml = $dom->createElement('symfony'));
94 if ('UNKNOWN' !== $application->getName()) {
95 $rootXml->setAttribute('name', $application->getName());
96 if ('UNKNOWN' !== $application->getVersion()) {
97 $rootXml->setAttribute('version', $application->getVersion());
101 $rootXml->appendChild($commandsXML = $dom->createElement('commands'));
103 $description = new ApplicationDescription($application, $namespace, true);
106 $commandsXML->setAttribute('namespace', $namespace);
109 foreach ($description->getCommands() as $command) {
110 $this->appendDocument($commandsXML, $this->getCommandDocument($command));
114 $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));
116 foreach ($description->getNamespaces() as $namespaceDescription) {
117 $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
118 $namespaceArrayXML->setAttribute('id', $namespaceDescription['id']);
120 foreach ($namespaceDescription['commands'] as $name) {
121 $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
122 $commandXML->appendChild($dom->createTextNode($name));
133 protected function describeInputArgument(InputArgument $argument, array $options = array())
135 $this->writeDocument($this->getInputArgumentDocument($argument));
141 protected function describeInputOption(InputOption $option, array $options = array())
143 $this->writeDocument($this->getInputOptionDocument($option));
149 protected function describeInputDefinition(InputDefinition $definition, array $options = array())
151 $this->writeDocument($this->getInputDefinitionDocument($definition));
157 protected function describeCommand(Command $command, array $options = array())
159 $this->writeDocument($this->getCommandDocument($command));
165 protected function describeApplication(Application $application, array $options = array())
167 $this->writeDocument($this->getApplicationDocument($application, isset($options['namespace']) ? $options['namespace'] : null));
171 * Appends document children to parent node.
173 private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
175 foreach ($importedParent->childNodes as $childNode) {
176 $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
181 * Writes DOM document.
183 * @return \DOMDocument|string
185 private function writeDocument(\DOMDocument $dom)
187 $dom->formatOutput = true;
188 $this->write($dom->saveXML());
192 * @return \DOMDocument
194 private function getInputArgumentDocument(InputArgument $argument)
196 $dom = new \DOMDocument('1.0', 'UTF-8');
198 $dom->appendChild($objectXML = $dom->createElement('argument'));
199 $objectXML->setAttribute('name', $argument->getName());
200 $objectXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
201 $objectXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
202 $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
203 $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
205 $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
206 $defaults = \is_array($argument->getDefault()) ? $argument->getDefault() : (\is_bool($argument->getDefault()) ? array(var_export($argument->getDefault(), true)) : ($argument->getDefault() ? array($argument->getDefault()) : array()));
207 foreach ($defaults as $default) {
208 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
209 $defaultXML->appendChild($dom->createTextNode($default));
216 * @return \DOMDocument
218 private function getInputOptionDocument(InputOption $option)
220 $dom = new \DOMDocument('1.0', 'UTF-8');
222 $dom->appendChild($objectXML = $dom->createElement('option'));
223 $objectXML->setAttribute('name', '--'.$option->getName());
224 $pos = strpos($option->getShortcut(), '|');
225 if (false !== $pos) {
226 $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
227 $objectXML->setAttribute('shortcuts', '-'.str_replace('|', '|-', $option->getShortcut()));
229 $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
231 $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
232 $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
233 $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
234 $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
235 $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
237 if ($option->acceptValue()) {
238 $defaults = \is_array($option->getDefault()) ? $option->getDefault() : (\is_bool($option->getDefault()) ? array(var_export($option->getDefault(), true)) : ($option->getDefault() ? array($option->getDefault()) : array()));
239 $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
241 if (!empty($defaults)) {
242 foreach ($defaults as $default) {
243 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
244 $defaultXML->appendChild($dom->createTextNode($default));