Drupal investigation

JsonDescriptor.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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);
  60. $commands = array();
  61. foreach ($description->getCommands() as $command) {
  62. $commands[] = $this->getCommandData($command);
  63. }
  64. $data = $describedNamespace
  65. ? array('commands' => $commands, 'namespace' => $describedNamespace)
  66. : array('commands' => $commands, 'namespaces' => array_values($description->getNamespaces()));
  67. $this->writeData($data, $options);
  68. }
  69. /**
  70. * Writes data as json.
  71. *
  72. * @param array $data
  73. * @param array $options
  74. *
  75. * @return array|string
  76. */
  77. private function writeData(array $data, array $options)
  78. {
  79. $this->write(json_encode($data, isset($options['json_encoding']) ? $options['json_encoding'] : 0));
  80. }
  81. /**
  82. * @param InputArgument $argument
  83. *
  84. * @return array
  85. */
  86. private function getInputArgumentData(InputArgument $argument)
  87. {
  88. return array(
  89. 'name' => $argument->getName(),
  90. 'is_required' => $argument->isRequired(),
  91. 'is_array' => $argument->isArray(),
  92. 'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $argument->getDescription()),
  93. 'default' => $argument->getDefault(),
  94. );
  95. }
  96. /**
  97. * @param InputOption $option
  98. *
  99. * @return array
  100. */
  101. private function getInputOptionData(InputOption $option)
  102. {
  103. return array(
  104. 'name' => '--'.$option->getName(),
  105. 'shortcut' => $option->getShortcut() ? '-'.implode('|-', explode('|', $option->getShortcut())) : '',
  106. 'accept_value' => $option->acceptValue(),
  107. 'is_value_required' => $option->isValueRequired(),
  108. 'is_multiple' => $option->isArray(),
  109. 'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
  110. 'default' => $option->getDefault(),
  111. );
  112. }
  113. /**
  114. * @param InputDefinition $definition
  115. *
  116. * @return array
  117. */
  118. private function getInputDefinitionData(InputDefinition $definition)
  119. {
  120. $inputArguments = array();
  121. foreach ($definition->getArguments() as $name => $argument) {
  122. $inputArguments[$name] = $this->getInputArgumentData($argument);
  123. }
  124. $inputOptions = array();
  125. foreach ($definition->getOptions() as $name => $option) {
  126. $inputOptions[$name] = $this->getInputOptionData($option);
  127. }
  128. return array('arguments' => $inputArguments, 'options' => $inputOptions);
  129. }
  130. /**
  131. * @param Command $command
  132. *
  133. * @return array
  134. */
  135. private function getCommandData(Command $command)
  136. {
  137. $command->getSynopsis();
  138. $command->mergeApplicationDefinition(false);
  139. return array(
  140. 'name' => $command->getName(),
  141. 'usage' => array_merge(array($command->getSynopsis()), $command->getUsages(), $command->getAliases()),
  142. 'description' => $command->getDescription(),
  143. 'help' => $command->getProcessedHelp(),
  144. 'definition' => $this->getInputDefinitionData($command->getNativeDefinition()),
  145. );
  146. }
  147. }