PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Console/Command.php

https://gitlab.com/ealexis.t/trends
PHP | 501 lines | 198 code | 66 blank | 237 comment | 8 complexity | d35879c8566c44d644798257165f266e MD5 | raw file
  1. <?php
  2. namespace Illuminate\Console;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Symfony\Component\Console\Helper\Table;
  5. use Symfony\Component\Console\Input\ArrayInput;
  6. use Symfony\Component\Console\Output\NullOutput;
  7. use Symfony\Component\Console\Question\Question;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Question\ChoiceQuestion;
  11. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  12. use Symfony\Component\Console\Command\Command as SymfonyCommand;
  13. class Command extends SymfonyCommand
  14. {
  15. /**
  16. * The Laravel application instance.
  17. *
  18. * @var \Illuminate\Contracts\Foundation\Application
  19. */
  20. protected $laravel;
  21. /**
  22. * The input interface implementation.
  23. *
  24. * @var \Symfony\Component\Console\Input\InputInterface
  25. */
  26. protected $input;
  27. /**
  28. * The output interface implementation.
  29. *
  30. * @var \Illuminate\Console\OutputStyle
  31. */
  32. protected $output;
  33. /**
  34. * The name and signature of the console command.
  35. *
  36. * @var string
  37. */
  38. protected $signature;
  39. /**
  40. * The console command name.
  41. *
  42. * @var string
  43. */
  44. protected $name;
  45. /**
  46. * The console command description.
  47. *
  48. * @var string
  49. */
  50. protected $description;
  51. /**
  52. * The default verbosity of output commands.
  53. *
  54. * @var int
  55. */
  56. protected $verbosity = OutputInterface::VERBOSITY_NORMAL;
  57. /**
  58. * The mapping between human readable verbosity levels and Symfony's OutputInterface.
  59. *
  60. * @var array
  61. */
  62. protected $verbosityMap = [
  63. 'v' => OutputInterface::VERBOSITY_VERBOSE,
  64. 'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE,
  65. 'vvv' => OutputInterface::VERBOSITY_DEBUG,
  66. 'quiet' => OutputInterface::VERBOSITY_QUIET,
  67. 'normal' => OutputInterface::VERBOSITY_NORMAL,
  68. ];
  69. /**
  70. * Create a new console command instance.
  71. *
  72. * @return void
  73. */
  74. public function __construct()
  75. {
  76. // We will go ahead and set the name, description, and parameters on console
  77. // commands just to make things a little easier on the developer. This is
  78. // so they don't have to all be manually specified in the constructors.
  79. if (isset($this->signature)) {
  80. $this->configureUsingFluentDefinition();
  81. } else {
  82. parent::__construct($this->name);
  83. }
  84. $this->setDescription($this->description);
  85. if (! isset($this->signature)) {
  86. $this->specifyParameters();
  87. }
  88. }
  89. /**
  90. * Configure the console command using a fluent definition.
  91. *
  92. * @return void
  93. */
  94. protected function configureUsingFluentDefinition()
  95. {
  96. list($name, $arguments, $options) = Parser::parse($this->signature);
  97. parent::__construct($name);
  98. foreach ($arguments as $argument) {
  99. $this->getDefinition()->addArgument($argument);
  100. }
  101. foreach ($options as $option) {
  102. $this->getDefinition()->addOption($option);
  103. }
  104. }
  105. /**
  106. * Specify the arguments and options on the command.
  107. *
  108. * @return void
  109. */
  110. protected function specifyParameters()
  111. {
  112. // We will loop through all of the arguments and options for the command and
  113. // set them all on the base command instance. This specifies what can get
  114. // passed into these commands as "parameters" to control the execution.
  115. foreach ($this->getArguments() as $arguments) {
  116. call_user_func_array([$this, 'addArgument'], $arguments);
  117. }
  118. foreach ($this->getOptions() as $options) {
  119. call_user_func_array([$this, 'addOption'], $options);
  120. }
  121. }
  122. /**
  123. * Run the console command.
  124. *
  125. * @param \Symfony\Component\Console\Input\InputInterface $input
  126. * @param \Symfony\Component\Console\Output\OutputInterface $output
  127. * @return int
  128. */
  129. public function run(InputInterface $input, OutputInterface $output)
  130. {
  131. $this->input = $input;
  132. $this->output = new OutputStyle($input, $output);
  133. return parent::run($input, $output);
  134. }
  135. /**
  136. * Execute the console command.
  137. *
  138. * @param \Symfony\Component\Console\Input\InputInterface $input
  139. * @param \Symfony\Component\Console\Output\OutputInterface $output
  140. * @return mixed
  141. */
  142. protected function execute(InputInterface $input, OutputInterface $output)
  143. {
  144. $method = method_exists($this, 'handle') ? 'handle' : 'fire';
  145. return $this->laravel->call([$this, $method]);
  146. }
  147. /**
  148. * Call another console command.
  149. *
  150. * @param string $command
  151. * @param array $arguments
  152. * @return int
  153. */
  154. public function call($command, array $arguments = [])
  155. {
  156. $instance = $this->getApplication()->find($command);
  157. $arguments['command'] = $command;
  158. return $instance->run(new ArrayInput($arguments), $this->output);
  159. }
  160. /**
  161. * Call another console command silently.
  162. *
  163. * @param string $command
  164. * @param array $arguments
  165. * @return int
  166. */
  167. public function callSilent($command, array $arguments = [])
  168. {
  169. $instance = $this->getApplication()->find($command);
  170. $arguments['command'] = $command;
  171. return $instance->run(new ArrayInput($arguments), new NullOutput);
  172. }
  173. /**
  174. * Get the value of a command argument.
  175. *
  176. * @param string $key
  177. * @return string|array
  178. */
  179. public function argument($key = null)
  180. {
  181. if (is_null($key)) {
  182. return $this->input->getArguments();
  183. }
  184. return $this->input->getArgument($key);
  185. }
  186. /**
  187. * Get the value of a command option.
  188. *
  189. * @param string $key
  190. * @return string|array
  191. */
  192. public function option($key = null)
  193. {
  194. if (is_null($key)) {
  195. return $this->input->getOptions();
  196. }
  197. return $this->input->getOption($key);
  198. }
  199. /**
  200. * Confirm a question with the user.
  201. *
  202. * @param string $question
  203. * @param bool $default
  204. * @return bool
  205. */
  206. public function confirm($question, $default = false)
  207. {
  208. return $this->output->confirm($question, $default);
  209. }
  210. /**
  211. * Prompt the user for input.
  212. *
  213. * @param string $question
  214. * @param string $default
  215. * @return string
  216. */
  217. public function ask($question, $default = null)
  218. {
  219. return $this->output->ask($question, $default);
  220. }
  221. /**
  222. * Prompt the user for input with auto completion.
  223. *
  224. * @param string $question
  225. * @param array $choices
  226. * @param string $default
  227. * @return string
  228. */
  229. public function anticipate($question, array $choices, $default = null)
  230. {
  231. return $this->askWithCompletion($question, $choices, $default);
  232. }
  233. /**
  234. * Prompt the user for input with auto completion.
  235. *
  236. * @param string $question
  237. * @param array $choices
  238. * @param string $default
  239. * @return string
  240. */
  241. public function askWithCompletion($question, array $choices, $default = null)
  242. {
  243. $question = new Question($question, $default);
  244. $question->setAutocompleterValues($choices);
  245. return $this->output->askQuestion($question);
  246. }
  247. /**
  248. * Prompt the user for input but hide the answer from the console.
  249. *
  250. * @param string $question
  251. * @param bool $fallback
  252. * @return string
  253. */
  254. public function secret($question, $fallback = true)
  255. {
  256. $question = new Question($question);
  257. $question->setHidden(true)->setHiddenFallback($fallback);
  258. return $this->output->askQuestion($question);
  259. }
  260. /**
  261. * Give the user a single choice from an array of answers.
  262. *
  263. * @param string $question
  264. * @param array $choices
  265. * @param string $default
  266. * @param mixed $attempts
  267. * @param bool $multiple
  268. * @return string
  269. */
  270. public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null)
  271. {
  272. $question = new ChoiceQuestion($question, $choices, $default);
  273. $question->setMaxAttempts($attempts)->setMultiselect($multiple);
  274. return $this->output->askQuestion($question);
  275. }
  276. /**
  277. * Format input to textual table.
  278. *
  279. * @param array $headers
  280. * @param \Illuminate\Contracts\Support\Arrayable|array $rows
  281. * @param string $style
  282. * @return void
  283. */
  284. public function table(array $headers, $rows, $style = 'default')
  285. {
  286. $table = new Table($this->output);
  287. if ($rows instanceof Arrayable) {
  288. $rows = $rows->toArray();
  289. }
  290. $table->setHeaders($headers)->setRows($rows)->setStyle($style)->render();
  291. }
  292. /**
  293. * Write a string as information output.
  294. *
  295. * @param string $string
  296. * @param null|int|string $verbosity
  297. * @return void
  298. */
  299. public function info($string, $verbosity = null)
  300. {
  301. $this->line($string, 'info', $verbosity);
  302. }
  303. /**
  304. * Write a string as standard output.
  305. *
  306. * @param string $string
  307. * @param string $style
  308. * @param null|int|string $verbosity
  309. * @return void
  310. */
  311. public function line($string, $style = null, $verbosity = null)
  312. {
  313. $styled = $style ? "<$style>$string</$style>" : $string;
  314. $this->output->writeln($styled, $this->parseVerbosity($verbosity));
  315. }
  316. /**
  317. * Write a string as comment output.
  318. *
  319. * @param string $string
  320. * @param null|int|string $verbosity
  321. * @return void
  322. */
  323. public function comment($string, $verbosity = null)
  324. {
  325. $this->line($string, 'comment', $verbosity);
  326. }
  327. /**
  328. * Write a string as question output.
  329. *
  330. * @param string $string
  331. * @param null|int|string $verbosity
  332. * @return void
  333. */
  334. public function question($string, $verbosity = null)
  335. {
  336. $this->line($string, 'question', $verbosity);
  337. }
  338. /**
  339. * Write a string as error output.
  340. *
  341. * @param string $string
  342. * @param null|int|string $verbosity
  343. * @return void
  344. */
  345. public function error($string, $verbosity = null)
  346. {
  347. $this->line($string, 'error', $verbosity);
  348. }
  349. /**
  350. * Write a string as warning output.
  351. *
  352. * @param string $string
  353. * @param null|int|string $verbosity
  354. * @return void
  355. */
  356. public function warn($string, $verbosity = null)
  357. {
  358. if (! $this->output->getFormatter()->hasStyle('warning')) {
  359. $style = new OutputFormatterStyle('yellow');
  360. $this->output->getFormatter()->setStyle('warning', $style);
  361. }
  362. $this->line($string, 'warning', $verbosity);
  363. }
  364. /**
  365. * Get the verbosity level in terms of Symfony's OutputInterface level.
  366. *
  367. * @param string|int $level
  368. * @return int
  369. */
  370. protected function parseVerbosity($level = null)
  371. {
  372. if (isset($this->verbosityMap[$level])) {
  373. $level = $this->verbosityMap[$level];
  374. } elseif (! is_int($level)) {
  375. $level = $this->verbosity;
  376. }
  377. return $level;
  378. }
  379. /**
  380. * Set the verbosity level.
  381. *
  382. * @param string|int $level
  383. * @return void
  384. */
  385. protected function setVerbosity($level)
  386. {
  387. $this->verbosity = $this->parseVerbosity($level);
  388. }
  389. /**
  390. * Get the console command arguments.
  391. *
  392. * @return array
  393. */
  394. protected function getArguments()
  395. {
  396. return [];
  397. }
  398. /**
  399. * Get the console command options.
  400. *
  401. * @return array
  402. */
  403. protected function getOptions()
  404. {
  405. return [];
  406. }
  407. /**
  408. * Get the output implementation.
  409. *
  410. * @return \Symfony\Component\Console\Output\OutputInterface
  411. */
  412. public function getOutput()
  413. {
  414. return $this->output;
  415. }
  416. /**
  417. * Get the Laravel application instance.
  418. *
  419. * @return \Illuminate\Contracts\Foundation\Application
  420. */
  421. public function getLaravel()
  422. {
  423. return $this->laravel;
  424. }
  425. /**
  426. * Set the Laravel application instance.
  427. *
  428. * @param \Illuminate\Contracts\Container\Container $laravel
  429. * @return void
  430. */
  431. public function setLaravel($laravel)
  432. {
  433. $this->laravel = $laravel;
  434. }
  435. }