PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/psy/psysh/src/Psy/Command/Command.php

https://gitlab.com/btkm/correct_fb
PHP | 282 lines | 162 code | 44 blank | 76 comment | 21 complexity | 8592b1ff84d12db785bbbcb7a33e6339 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2015 Justin Hileman
  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 Psy\Command;
  11. use Psy\Shell;
  12. use Symfony\Component\Console\Application;
  13. use Symfony\Component\Console\Command\Command as BaseCommand;
  14. use Symfony\Component\Console\Helper\Table;
  15. use Symfony\Component\Console\Helper\TableHelper;
  16. use Symfony\Component\Console\Helper\TableStyle;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * The Psy Shell base command.
  20. */
  21. abstract class Command extends BaseCommand
  22. {
  23. /**
  24. * Sets the application instance for this command.
  25. *
  26. * @param Application $application An Application instance
  27. *
  28. * @api
  29. */
  30. public function setApplication(Application $application = null)
  31. {
  32. if ($application !== null && !$application instanceof Shell) {
  33. throw new \InvalidArgumentException('PsySH Commands require an instance of Psy\Shell.');
  34. }
  35. return parent::setApplication($application);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function asText()
  41. {
  42. $messages = array(
  43. '<comment>Usage:</comment>',
  44. ' ' . $this->getSynopsis(),
  45. '',
  46. );
  47. if ($this->getAliases()) {
  48. $messages[] = $this->aliasesAsText();
  49. }
  50. if ($this->getArguments()) {
  51. $messages[] = $this->argumentsAsText();
  52. }
  53. if ($this->getOptions()) {
  54. $messages[] = $this->optionsAsText();
  55. }
  56. if ($help = $this->getProcessedHelp()) {
  57. $messages[] = '<comment>Help:</comment>';
  58. $messages[] = ' ' . str_replace("\n", "\n ", $help) . "\n";
  59. }
  60. return implode("\n", $messages);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. private function getArguments()
  66. {
  67. $hidden = $this->getHiddenArguments();
  68. return array_filter($this->getNativeDefinition()->getArguments(), function ($argument) use ($hidden) {
  69. return !in_array($argument->getName(), $hidden);
  70. });
  71. }
  72. /**
  73. * These arguments will be excluded from help output.
  74. *
  75. * @return array
  76. */
  77. protected function getHiddenArguments()
  78. {
  79. return array('command');
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. private function getOptions()
  85. {
  86. $hidden = $this->getHiddenOptions();
  87. return array_filter($this->getNativeDefinition()->getOptions(), function ($option) use ($hidden) {
  88. return !in_array($option->getName(), $hidden);
  89. });
  90. }
  91. /**
  92. * These options will be excluded from help output.
  93. *
  94. * @return array
  95. */
  96. protected function getHiddenOptions()
  97. {
  98. return array('verbose');
  99. }
  100. /**
  101. * Format command aliases as text..
  102. *
  103. * @return string
  104. */
  105. private function aliasesAsText()
  106. {
  107. return '<comment>Aliases:</comment> <info>' . implode(', ', $this->getAliases()) . '</info>' . PHP_EOL;
  108. }
  109. /**
  110. * Format command arguments as text.
  111. *
  112. * @return string
  113. */
  114. private function argumentsAsText()
  115. {
  116. $max = $this->getMaxWidth();
  117. $messages = array();
  118. $arguments = $this->getArguments();
  119. if (!empty($arguments)) {
  120. $messages[] = '<comment>Arguments:</comment>';
  121. foreach ($arguments as $argument) {
  122. if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
  123. $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($argument->getDefault()));
  124. } else {
  125. $default = '';
  126. }
  127. $description = str_replace("\n", "\n" . str_pad('', $max + 2, ' '), $argument->getDescription());
  128. $messages[] = sprintf(" <info>%-${max}s</info> %s%s", $argument->getName(), $description, $default);
  129. }
  130. $messages[] = '';
  131. }
  132. return implode(PHP_EOL, $messages);
  133. }
  134. /**
  135. * Format options as text.
  136. *
  137. * @return string
  138. */
  139. private function optionsAsText()
  140. {
  141. $max = $this->getMaxWidth();
  142. $messages = array();
  143. $options = $this->getOptions();
  144. if ($options) {
  145. $messages[] = '<comment>Options:</comment>';
  146. foreach ($options as $option) {
  147. if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
  148. $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($option->getDefault()));
  149. } else {
  150. $default = '';
  151. }
  152. $multiple = $option->isArray() ? '<comment> (multiple values allowed)</comment>' : '';
  153. $description = str_replace("\n", "\n" . str_pad('', $max + 2, ' '), $option->getDescription());
  154. $optionMax = $max - strlen($option->getName()) - 2;
  155. $messages[] = sprintf(
  156. " <info>%s</info> %-${optionMax}s%s%s%s",
  157. '--' . $option->getName(),
  158. $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '',
  159. $description,
  160. $default,
  161. $multiple
  162. );
  163. }
  164. $messages[] = '';
  165. }
  166. return implode(PHP_EOL, $messages);
  167. }
  168. /**
  169. * Calculate the maximum padding width for a set of lines.
  170. *
  171. * @return int
  172. */
  173. private function getMaxWidth()
  174. {
  175. $max = 0;
  176. foreach ($this->getOptions() as $option) {
  177. $nameLength = strlen($option->getName()) + 2;
  178. if ($option->getShortcut()) {
  179. $nameLength += strlen($option->getShortcut()) + 3;
  180. }
  181. $max = max($max, $nameLength);
  182. }
  183. foreach ($this->getArguments() as $argument) {
  184. $max = max($max, strlen($argument->getName()));
  185. }
  186. return ++$max;
  187. }
  188. /**
  189. * Format an option default as text.
  190. *
  191. * @param mixed $default
  192. *
  193. * @return string
  194. */
  195. private function formatDefaultValue($default)
  196. {
  197. if (is_array($default) && $default === array_values($default)) {
  198. return sprintf("array('%s')", implode("', '", $default));
  199. }
  200. return str_replace("\n", '', var_export($default, true));
  201. }
  202. /**
  203. * Get a Table instance.
  204. *
  205. * Falls back to legacy TableHelper.
  206. *
  207. * @return Table|TableHelper
  208. */
  209. protected function getTable(OutputInterface $output)
  210. {
  211. if (!class_exists('Symfony\Component\Console\Helper\Table')) {
  212. return $this->getTableHelper();
  213. }
  214. $style = new TableStyle();
  215. $style
  216. ->setVerticalBorderChar(' ')
  217. ->setHorizontalBorderChar('')
  218. ->setCrossingChar('');
  219. $table = new Table($output);
  220. return $table
  221. ->setRows(array())
  222. ->setStyle($style);
  223. }
  224. /**
  225. * Legacy fallback for getTable.
  226. *
  227. * @return TableHelper
  228. */
  229. protected function getTableHelper()
  230. {
  231. $table = $this->getApplication()->getHelperSet()->get('table');
  232. return $table
  233. ->setRows(array())
  234. ->setLayout(TableHelper::LAYOUT_BORDERLESS)
  235. ->setHorizontalBorderChar('')
  236. ->setCrossingChar('');
  237. }
  238. }