XmlDescriptor.php 9.1KB

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