PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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