XmlDescriptor.php 9.7 KB

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