PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/console/Descriptor/XmlDescriptor.php

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