XmlDescriptor.php 9.0 KB

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