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 * @param InputDefinition $definition
32 * @return \DOMDocument
34 public function getInputDefinitionDocument(InputDefinition $definition)
36 $dom = new \DOMDocument('1.0', 'UTF-8');
37 $dom->appendChild($definitionXML = $dom->createElement('definition'));
39 $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
40 foreach ($definition->getArguments() as $argument) {
41 $this->appendDocument($argumentsXML, $this->getInputArgumentDocument($argument));
44 $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
45 foreach ($definition->getOptions() as $option) {
46 $this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
53 * @param Command $command
55 * @return \DOMDocument
57 public function getCommandDocument(Command $command)
59 $dom = new \DOMDocument('1.0', 'UTF-8');
60 $dom->appendChild($commandXML = $dom->createElement('command'));
62 $command->getSynopsis();
63 $command->mergeApplicationDefinition(false);
65 $commandXML->setAttribute('id', $command->getName());
66 $commandXML->setAttribute('name', $command->getName());
68 $commandXML->appendChild($usagesXML = $dom->createElement('usages'));
70 foreach (array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()) as $usage) {
71 $usagesXML->appendChild($dom->createElement('usage', $usage));
74 $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
75 $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
77 $commandXML->appendChild($helpXML = $dom->createElement('help'));
78 $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
80 $definitionXML = $this->getInputDefinitionDocument($command->getNativeDefinition());
81 $this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
87 * @param Application $application
88 * @param string|null $namespace
90 * @return \DOMDocument
92 public function getApplicationDocument(Application $application, $namespace = null)
94 $dom = new \DOMDocument('1.0', 'UTF-8');
95 $dom->appendChild($rootXml = $dom->createElement('symfony'));
97 if ($application->getName() !== 'UNKNOWN') {
98 $rootXml->setAttribute('name', $application->getName());
99 if ($application->getVersion() !== 'UNKNOWN') {
100 $rootXml->setAttribute('version', $application->getVersion());
104 $rootXml->appendChild($commandsXML = $dom->createElement('commands'));
106 $description = new ApplicationDescription($application, $namespace);
109 $commandsXML->setAttribute('namespace', $namespace);
112 foreach ($description->getCommands() as $command) {
113 $this->appendDocument($commandsXML, $this->getCommandDocument($command));
117 $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));
119 foreach ($description->getNamespaces() as $namespaceDescription) {
120 $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
121 $namespaceArrayXML->setAttribute('id', $namespaceDescription['id']);
123 foreach ($namespaceDescription['commands'] as $name) {
124 $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
125 $commandXML->appendChild($dom->createTextNode($name));
136 protected function describeInputArgument(InputArgument $argument, array $options = array())
138 $this->writeDocument($this->getInputArgumentDocument($argument));
144 protected function describeInputOption(InputOption $option, array $options = array())
146 $this->writeDocument($this->getInputOptionDocument($option));
152 protected function describeInputDefinition(InputDefinition $definition, array $options = array())
154 $this->writeDocument($this->getInputDefinitionDocument($definition));
160 protected function describeCommand(Command $command, array $options = array())
162 $this->writeDocument($this->getCommandDocument($command));
168 protected function describeApplication(Application $application, array $options = array())
170 $this->writeDocument($this->getApplicationDocument($application, isset($options['namespace']) ? $options['namespace'] : null));
174 * Appends document children to parent node.
176 * @param \DOMNode $parentNode
177 * @param \DOMNode $importedParent
179 private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
181 foreach ($importedParent->childNodes as $childNode) {
182 $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
187 * Writes DOM document.
189 * @param \DOMDocument $dom
191 * @return \DOMDocument|string
193 private function writeDocument(\DOMDocument $dom)
195 $dom->formatOutput = true;
196 $this->write($dom->saveXML());
200 * @param InputArgument $argument
202 * @return \DOMDocument
204 private function getInputArgumentDocument(InputArgument $argument)
206 $dom = new \DOMDocument('1.0', 'UTF-8');
208 $dom->appendChild($objectXML = $dom->createElement('argument'));
209 $objectXML->setAttribute('name', $argument->getName());
210 $objectXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
211 $objectXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
212 $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
213 $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
215 $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
216 $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : (is_bool($argument->getDefault()) ? array(var_export($argument->getDefault(), true)) : ($argument->getDefault() ? array($argument->getDefault()) : array()));
217 foreach ($defaults as $default) {
218 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
219 $defaultXML->appendChild($dom->createTextNode($default));
226 * @param InputOption $option
228 * @return \DOMDocument
230 private function getInputOptionDocument(InputOption $option)
232 $dom = new \DOMDocument('1.0', 'UTF-8');
234 $dom->appendChild($objectXML = $dom->createElement('option'));
235 $objectXML->setAttribute('name', '--'.$option->getName());
236 $pos = strpos($option->getShortcut(), '|');
237 if (false !== $pos) {
238 $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
239 $objectXML->setAttribute('shortcuts', '-'.implode('|-', explode('|', $option->getShortcut())));
241 $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
243 $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
244 $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
245 $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
246 $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
247 $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
249 if ($option->acceptValue()) {
250 $defaults = is_array($option->getDefault()) ? $option->getDefault() : (is_bool($option->getDefault()) ? array(var_export($option->getDefault(), true)) : ($option->getDefault() ? array($option->getDefault()) : array()));
251 $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
253 if (!empty($defaults)) {
254 foreach ($defaults as $default) {
255 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
256 $defaultXML->appendChild($dom->createTextNode($default));