PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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