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

https://gitlab.com/techniconline/kmc · PHP · 386 lines · 144 code · 58 blank · 184 comment · 2 complexity · 06d9f9e8863e7d0e6323f859b737c5d5 MD5 · raw file

  1. <?php namespace Illuminate\Console;
  2. use Symfony\Component\Console\Helper\Table;
  3. use Symfony\Component\Console\Input\ArrayInput;
  4. use Symfony\Component\Console\Output\NullOutput;
  5. use Symfony\Component\Console\Question\Question;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Question\ChoiceQuestion;
  9. use Symfony\Component\Console\Question\ConfirmationQuestion;
  10. use Illuminate\Contracts\Foundation\Application as LaravelApplication;
  11. class Command extends \Symfony\Component\Console\Command\Command
  12. {
  13. /**
  14. * The Laravel application instance.
  15. *
  16. * @var \Illuminate\Contracts\Foundation\Application
  17. */
  18. protected $laravel;
  19. /**
  20. * The input interface implementation.
  21. *
  22. * @var \Symfony\Component\Console\Input\InputInterface
  23. */
  24. protected $input;
  25. /**
  26. * The output interface implementation.
  27. *
  28. * @var \Symfony\Component\Console\Output\OutputInterface
  29. */
  30. protected $output;
  31. /**
  32. * The console command name.
  33. *
  34. * @var string
  35. */
  36. protected $name;
  37. /**
  38. * The console command description.
  39. *
  40. * @var string
  41. */
  42. protected $description;
  43. /**
  44. * Create a new console command instance.
  45. *
  46. * @return void
  47. */
  48. public function __construct()
  49. {
  50. parent::__construct($this->name);
  51. // We will go ahead and set the name, description, and parameters on console
  52. // commands just to make things a little easier on the developer. This is
  53. // so they don't have to all be manually specified in the constructors.
  54. $this->setDescription($this->description);
  55. $this->specifyParameters();
  56. }
  57. /**
  58. * Specify the arguments and options on the command.
  59. *
  60. * @return void
  61. */
  62. protected function specifyParameters()
  63. {
  64. // We will loop through all of the arguments and options for the command and
  65. // set them all on the base command instance. This specifies what can get
  66. // passed into these commands as "parameters" to control the execution.
  67. foreach ($this->getArguments() as $arguments) {
  68. call_user_func_array(array($this, 'addArgument'), $arguments);
  69. }
  70. foreach ($this->getOptions() as $options) {
  71. call_user_func_array(array($this, 'addOption'), $options);
  72. }
  73. }
  74. /**
  75. * Run the console command.
  76. *
  77. * @param \Symfony\Component\Console\Input\InputInterface $input
  78. * @param \Symfony\Component\Console\Output\OutputInterface $output
  79. * @return int
  80. */
  81. public function run(InputInterface $input, OutputInterface $output)
  82. {
  83. $this->input = $input;
  84. $this->output = $output;
  85. return parent::run($input, $output);
  86. }
  87. /**
  88. * Execute the console command.
  89. *
  90. * @param \Symfony\Component\Console\Input\InputInterface $input
  91. * @param \Symfony\Component\Console\Output\OutputInterface $output
  92. * @return mixed
  93. */
  94. protected function execute(InputInterface $input, OutputInterface $output)
  95. {
  96. $method = method_exists($this, 'handle') ? 'handle' : 'fire';
  97. return $this->laravel->call([$this, $method]);
  98. }
  99. /**
  100. * Call another console command.
  101. *
  102. * @param string $command
  103. * @param array $arguments
  104. * @return int
  105. */
  106. public function call($command, array $arguments = array())
  107. {
  108. $instance = $this->getApplication()->find($command);
  109. $arguments['command'] = $command;
  110. return $instance->run(new ArrayInput($arguments), $this->output);
  111. }
  112. /**
  113. * Call another console command silently.
  114. *
  115. * @param string $command
  116. * @param array $arguments
  117. * @return int
  118. */
  119. public function callSilent($command, array $arguments = array())
  120. {
  121. $instance = $this->getApplication()->find($command);
  122. $arguments['command'] = $command;
  123. return $instance->run(new ArrayInput($arguments), new NullOutput);
  124. }
  125. /**
  126. * Get the value of a command argument.
  127. *
  128. * @param string $key
  129. * @return string|array
  130. */
  131. public function argument($key = null)
  132. {
  133. if (is_null($key)) return $this->input->getArguments();
  134. return $this->input->getArgument($key);
  135. }
  136. /**
  137. * Get the value of a command option.
  138. *
  139. * @param string $key
  140. * @return string|array
  141. */
  142. public function option($key = null)
  143. {
  144. if (is_null($key)) return $this->input->getOptions();
  145. return $this->input->getOption($key);
  146. }
  147. /**
  148. * Confirm a question with the user.
  149. *
  150. * @param string $question
  151. * @param bool $default
  152. * @return bool
  153. */
  154. public function confirm($question, $default = false)
  155. {
  156. $helper = $this->getHelperSet()->get('question');
  157. $question = new ConfirmationQuestion("<question>{$question}</question> ", $default);
  158. return $helper->ask($this->input, $this->output, $question);
  159. }
  160. /**
  161. * Prompt the user for input.
  162. *
  163. * @param string $question
  164. * @param string $default
  165. * @return string
  166. */
  167. public function ask($question, $default = null)
  168. {
  169. $helper = $this->getHelperSet()->get('question');
  170. $question = new Question("<question>$question</question> ", $default);
  171. return $helper->ask($this->input, $this->output, $question);
  172. }
  173. /**
  174. * Prompt the user for input with auto completion.
  175. *
  176. * @param string $question
  177. * @param array $choices
  178. * @param string $default
  179. * @return string
  180. */
  181. public function askWithCompletion($question, array $choices, $default = null)
  182. {
  183. $helper = $this->getHelperSet()->get('question');
  184. $question = new Question("<question>$question</question> ", $default);
  185. $question->setAutocompleterValues($choices);
  186. return $helper->ask($this->input, $this->output, $question);
  187. }
  188. /**
  189. * Prompt the user for input but hide the answer from the console.
  190. *
  191. * @param string $question
  192. * @param bool $fallback
  193. * @return string
  194. */
  195. public function secret($question, $fallback = true)
  196. {
  197. $helper = $this->getHelperSet()->get('question');
  198. $question = new Question("<question>$question</question> ");
  199. $question->setHidden(true)->setHiddenFallback($fallback);
  200. return $helper->ask($this->input, $this->output, $question);
  201. }
  202. /**
  203. * Give the user a single choice from an array of answers.
  204. *
  205. * @param string $question
  206. * @param array $choices
  207. * @param string $default
  208. * @param mixed $attempts
  209. * @param bool $multiple
  210. * @return bool
  211. */
  212. public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null)
  213. {
  214. $helper = $this->getHelperSet()->get('question');
  215. $question = new ChoiceQuestion("<question>$question</question> ", $choices, $default);
  216. $question->setMaxAttempts($attempts)->setMultiselect($multiple);
  217. return $helper->ask($this->input, $this->output, $question);
  218. }
  219. /**
  220. * Format input to textual table.
  221. *
  222. * @param array $headers
  223. * @param array $rows
  224. * @param string $style
  225. * @return void
  226. */
  227. public function table(array $headers, array $rows, $style = 'default')
  228. {
  229. $table = new Table($this->output);
  230. $table->setHeaders($headers)->setRows($rows)->setStyle($style)->render();
  231. }
  232. /**
  233. * Write a string as information output.
  234. *
  235. * @param string $string
  236. * @return void
  237. */
  238. public function info($string)
  239. {
  240. $this->output->writeln("<info>$string</info>");
  241. }
  242. /**
  243. * Write a string as standard output.
  244. *
  245. * @param string $string
  246. * @return void
  247. */
  248. public function line($string)
  249. {
  250. $this->output->writeln($string);
  251. }
  252. /**
  253. * Write a string as comment output.
  254. *
  255. * @param string $string
  256. * @return void
  257. */
  258. public function comment($string)
  259. {
  260. $this->output->writeln("<comment>$string</comment>");
  261. }
  262. /**
  263. * Write a string as question output.
  264. *
  265. * @param string $string
  266. * @return void
  267. */
  268. public function question($string)
  269. {
  270. $this->output->writeln("<question>$string</question>");
  271. }
  272. /**
  273. * Write a string as error output.
  274. *
  275. * @param string $string
  276. * @return void
  277. */
  278. public function error($string)
  279. {
  280. $this->output->writeln("<error>$string</error>");
  281. }
  282. /**
  283. * Get the console command arguments.
  284. *
  285. * @return array
  286. */
  287. protected function getArguments()
  288. {
  289. return array();
  290. }
  291. /**
  292. * Get the console command options.
  293. *
  294. * @return array
  295. */
  296. protected function getOptions()
  297. {
  298. return array();
  299. }
  300. /**
  301. * Get the output implementation.
  302. *
  303. * @return \Symfony\Component\Console\Output\OutputInterface
  304. */
  305. public function getOutput()
  306. {
  307. return $this->output;
  308. }
  309. /**
  310. * Get the Laravel application instance.
  311. *
  312. * @return \Illuminate\Contracts\Foundation\Application
  313. */
  314. public function getLaravel()
  315. {
  316. return $this->laravel;
  317. }
  318. /**
  319. * Set the Laravel application instance.
  320. *
  321. * @param \Illuminate\Contracts\Foundation\Application $laravel
  322. * @return void
  323. */
  324. public function setLaravel(LaravelApplication $laravel)
  325. {
  326. $this->laravel = $laravel;
  327. }
  328. }