PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/milton2913/myBlog
PHP | 543 lines | 214 code | 70 blank | 259 comment | 8 complexity | 7bcc8255cc92b8bfc88cb8bd42b40a43 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. * Determine if the given argument is present.
  175. *
  176. * @param string|int $name
  177. * @return bool
  178. */
  179. public function hasArgument($name)
  180. {
  181. return $this->input->hasArgument($name);
  182. }
  183. /**
  184. * Get the value of a command argument.
  185. *
  186. * @param string $key
  187. * @return string|array
  188. */
  189. public function argument($key = null)
  190. {
  191. if (is_null($key)) {
  192. return $this->input->getArguments();
  193. }
  194. return $this->input->getArgument($key);
  195. }
  196. /**
  197. * Get all of the arguments passed to the command.
  198. *
  199. * @return array
  200. */
  201. public function arguments()
  202. {
  203. return $this->argument();
  204. }
  205. /**
  206. * Determine if the given option is present.
  207. *
  208. * @param string $name
  209. * @return bool
  210. */
  211. public function hasOption($name)
  212. {
  213. return $this->input->hasOption($name);
  214. }
  215. /**
  216. * Get the value of a command option.
  217. *
  218. * @param string $key
  219. * @return string|array
  220. */
  221. public function option($key = null)
  222. {
  223. if (is_null($key)) {
  224. return $this->input->getOptions();
  225. }
  226. return $this->input->getOption($key);
  227. }
  228. /**
  229. * Get all of the options passed to the command.
  230. *
  231. * @return array
  232. */
  233. public function options()
  234. {
  235. return $this->option();
  236. }
  237. /**
  238. * Confirm a question with the user.
  239. *
  240. * @param string $question
  241. * @param bool $default
  242. * @return bool
  243. */
  244. public function confirm($question, $default = false)
  245. {
  246. return $this->output->confirm($question, $default);
  247. }
  248. /**
  249. * Prompt the user for input.
  250. *
  251. * @param string $question
  252. * @param string $default
  253. * @return string
  254. */
  255. public function ask($question, $default = null)
  256. {
  257. return $this->output->ask($question, $default);
  258. }
  259. /**
  260. * Prompt the user for input with auto completion.
  261. *
  262. * @param string $question
  263. * @param array $choices
  264. * @param string $default
  265. * @return string
  266. */
  267. public function anticipate($question, array $choices, $default = null)
  268. {
  269. return $this->askWithCompletion($question, $choices, $default);
  270. }
  271. /**
  272. * Prompt the user for input with auto completion.
  273. *
  274. * @param string $question
  275. * @param array $choices
  276. * @param string $default
  277. * @return string
  278. */
  279. public function askWithCompletion($question, array $choices, $default = null)
  280. {
  281. $question = new Question($question, $default);
  282. $question->setAutocompleterValues($choices);
  283. return $this->output->askQuestion($question);
  284. }
  285. /**
  286. * Prompt the user for input but hide the answer from the console.
  287. *
  288. * @param string $question
  289. * @param bool $fallback
  290. * @return string
  291. */
  292. public function secret($question, $fallback = true)
  293. {
  294. $question = new Question($question);
  295. $question->setHidden(true)->setHiddenFallback($fallback);
  296. return $this->output->askQuestion($question);
  297. }
  298. /**
  299. * Give the user a single choice from an array of answers.
  300. *
  301. * @param string $question
  302. * @param array $choices
  303. * @param string $default
  304. * @param mixed $attempts
  305. * @param bool $multiple
  306. * @return string
  307. */
  308. public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null)
  309. {
  310. $question = new ChoiceQuestion($question, $choices, $default);
  311. $question->setMaxAttempts($attempts)->setMultiselect($multiple);
  312. return $this->output->askQuestion($question);
  313. }
  314. /**
  315. * Format input to textual table.
  316. *
  317. * @param array $headers
  318. * @param \Illuminate\Contracts\Support\Arrayable|array $rows
  319. * @param string $style
  320. * @return void
  321. */
  322. public function table(array $headers, $rows, $style = 'default')
  323. {
  324. $table = new Table($this->output);
  325. if ($rows instanceof Arrayable) {
  326. $rows = $rows->toArray();
  327. }
  328. $table->setHeaders($headers)->setRows($rows)->setStyle($style)->render();
  329. }
  330. /**
  331. * Write a string as information output.
  332. *
  333. * @param string $string
  334. * @param null|int|string $verbosity
  335. * @return void
  336. */
  337. public function info($string, $verbosity = null)
  338. {
  339. $this->line($string, 'info', $verbosity);
  340. }
  341. /**
  342. * Write a string as standard output.
  343. *
  344. * @param string $string
  345. * @param string $style
  346. * @param null|int|string $verbosity
  347. * @return void
  348. */
  349. public function line($string, $style = null, $verbosity = null)
  350. {
  351. $styled = $style ? "<$style>$string</$style>" : $string;
  352. $this->output->writeln($styled, $this->parseVerbosity($verbosity));
  353. }
  354. /**
  355. * Write a string as comment output.
  356. *
  357. * @param string $string
  358. * @param null|int|string $verbosity
  359. * @return void
  360. */
  361. public function comment($string, $verbosity = null)
  362. {
  363. $this->line($string, 'comment', $verbosity);
  364. }
  365. /**
  366. * Write a string as question output.
  367. *
  368. * @param string $string
  369. * @param null|int|string $verbosity
  370. * @return void
  371. */
  372. public function question($string, $verbosity = null)
  373. {
  374. $this->line($string, 'question', $verbosity);
  375. }
  376. /**
  377. * Write a string as error output.
  378. *
  379. * @param string $string
  380. * @param null|int|string $verbosity
  381. * @return void
  382. */
  383. public function error($string, $verbosity = null)
  384. {
  385. $this->line($string, 'error', $verbosity);
  386. }
  387. /**
  388. * Write a string as warning output.
  389. *
  390. * @param string $string
  391. * @param null|int|string $verbosity
  392. * @return void
  393. */
  394. public function warn($string, $verbosity = null)
  395. {
  396. if (! $this->output->getFormatter()->hasStyle('warning')) {
  397. $style = new OutputFormatterStyle('yellow');
  398. $this->output->getFormatter()->setStyle('warning', $style);
  399. }
  400. $this->line($string, 'warning', $verbosity);
  401. }
  402. /**
  403. * Get the verbosity level in terms of Symfony's OutputInterface level.
  404. *
  405. * @param string|int $level
  406. * @return int
  407. */
  408. protected function parseVerbosity($level = null)
  409. {
  410. if (isset($this->verbosityMap[$level])) {
  411. $level = $this->verbosityMap[$level];
  412. } elseif (! is_int($level)) {
  413. $level = $this->verbosity;
  414. }
  415. return $level;
  416. }
  417. /**
  418. * Set the verbosity level.
  419. *
  420. * @param string|int $level
  421. * @return void
  422. */
  423. protected function setVerbosity($level)
  424. {
  425. $this->verbosity = $this->parseVerbosity($level);
  426. }
  427. /**
  428. * Get the console command arguments.
  429. *
  430. * @return array
  431. */
  432. protected function getArguments()
  433. {
  434. return [];
  435. }
  436. /**
  437. * Get the console command options.
  438. *
  439. * @return array
  440. */
  441. protected function getOptions()
  442. {
  443. return [];
  444. }
  445. /**
  446. * Get the output implementation.
  447. *
  448. * @return \Symfony\Component\Console\Output\OutputInterface
  449. */
  450. public function getOutput()
  451. {
  452. return $this->output;
  453. }
  454. /**
  455. * Get the Laravel application instance.
  456. *
  457. * @return \Illuminate\Contracts\Foundation\Application
  458. */
  459. public function getLaravel()
  460. {
  461. return $this->laravel;
  462. }
  463. /**
  464. * Set the Laravel application instance.
  465. *
  466. * @param \Illuminate\Contracts\Container\Container $laravel
  467. * @return void
  468. */
  469. public function setLaravel($laravel)
  470. {
  471. $this->laravel = $laravel;
  472. }
  473. }