XmlDescriptor.php 9.4 KB

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