JsonDescriptor.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * JSON descriptor.
  18. *
  19. * @author Jean-François Simon <contact@jfsimon.fr>
  20. *
  21. * @internal
  22. */
  23. class JsonDescriptor extends Descriptor
  24. {
  25. protected function describeInputArgument(InputArgument $argument, array $options = []): void
  26. {
  27. $this->writeData($this->getInputArgumentData($argument), $options);
  28. }
  29. protected function describeInputOption(InputOption $option, array $options = []): void
  30. {
  31. $this->writeData($this->getInputOptionData($option), $options);
  32. if ($option->isNegatable()) {
  33. $this->writeData($this->getInputOptionData($option, true), $options);
  34. }
  35. }
  36. protected function describeInputDefinition(InputDefinition $definition, array $options = []): void
  37. {
  38. $this->writeData($this->getInputDefinitionData($definition), $options);
  39. }
  40. protected function describeCommand(Command $command, array $options = []): void
  41. {
  42. $this->writeData($this->getCommandData($command, $options['short'] ?? false), $options);
  43. }
  44. protected function describeApplication(Application $application, array $options = []): void
  45. {
  46. $describedNamespace = $options['namespace'] ?? null;
  47. $description = new ApplicationDescription($application, $describedNamespace, true);
  48. $commands = [];
  49. foreach ($description->getCommands() as $command) {
  50. $commands[] = $this->getCommandData($command, $options['short'] ?? false);
  51. }
  52. $data = [];
  53. if ('UNKNOWN' !== $application->getName()) {
  54. $data['application']['name'] = $application->getName();
  55. if ('UNKNOWN' !== $application->getVersion()) {
  56. $data['application']['version'] = $application->getVersion();
  57. }
  58. }
  59. $data['commands'] = $commands;
  60. if ($describedNamespace) {
  61. $data['namespace'] = $describedNamespace;
  62. } else {
  63. $data['namespaces'] = array_values($description->getNamespaces());
  64. }
  65. $this->writeData($data, $options);
  66. }
  67. /**
  68. * Writes data as json.
  69. */
  70. private function writeData(array $data, array $options): void
  71. {
  72. $flags = $options['json_encoding'] ?? 0;
  73. $this->write(json_encode($data, $flags));
  74. }
  75. private function getInputArgumentData(InputArgument $argument): array
  76. {
  77. return [
  78. 'name' => $argument->getName(),
  79. 'is_required' => $argument->isRequired(),
  80. 'is_array' => $argument->isArray(),
  81. 'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $argument->getDescription()),
  82. 'default' => \INF === $argument->getDefault() ? 'INF' : $argument->getDefault(),
  83. ];
  84. }
  85. private function getInputOptionData(InputOption $option, bool $negated = false): array
  86. {
  87. return $negated ? [
  88. 'name' => '--no-'.$option->getName(),
  89. 'shortcut' => '',
  90. 'accept_value' => false,
  91. 'is_value_required' => false,
  92. 'is_multiple' => false,
  93. 'description' => 'Negate the "--'.$option->getName().'" option',
  94. 'default' => false,
  95. ] : [
  96. 'name' => '--'.$option->getName(),
  97. 'shortcut' => $option->getShortcut() ? '-'.str_replace('|', '|-', $option->getShortcut()) : '',
  98. 'accept_value' => $option->acceptValue(),
  99. 'is_value_required' => $option->isValueRequired(),
  100. 'is_multiple' => $option->isArray(),
  101. 'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
  102. 'default' => \INF === $option->getDefault() ? 'INF' : $option->getDefault(),
  103. ];
  104. }
  105. private function getInputDefinitionData(InputDefinition $definition): array
  106. {
  107. $inputArguments = [];
  108. foreach ($definition->getArguments() as $name => $argument) {
  109. $inputArguments[$name] = $this->getInputArgumentData($argument);
  110. }
  111. $inputOptions = [];
  112. foreach ($definition->getOptions() as $name => $option) {
  113. $inputOptions[$name] = $this->getInputOptionData($option);
  114. if ($option->isNegatable()) {
  115. $inputOptions['no-'.$name] = $this->getInputOptionData($option, true);
  116. }
  117. }
  118. return ['arguments' => $inputArguments, 'options' => $inputOptions];
  119. }
  120. private function getCommandData(Command $command, bool $short = false): array
  121. {
  122. $data = [
  123. 'name' => $command->getName(),
  124. 'description' => $command->getDescription(),
  125. ];
  126. if ($short) {
  127. $data += [
  128. 'usage' => $command->getAliases(),
  129. ];
  130. } else {
  131. $command->mergeApplicationDefinition(false);
  132. $data += [
  133. 'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
  134. 'help' => $command->getProcessedHelp(),
  135. 'definition' => $this->getInputDefinitionData($command->getDefinition()),
  136. ];
  137. }
  138. $data['hidden'] = $command->isHidden();
  139. return $data;
  140. }
  141. }