XmlDescriptor.php 9.3 KB

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