JsonDescriptor.php 5.1 KB

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