/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php

https://gitlab.com/vincetang/mtweb · PHP · 249 lines · 109 code · 32 blank · 108 comment · 3 complexity · 5c1566a42ae9d48508602d41f54916f2 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Foundation\Console;
  3. use Exception;
  4. use Illuminate\Contracts\Events\Dispatcher;
  5. use Illuminate\Console\Scheduling\Schedule;
  6. use Illuminate\Console\Application as Artisan;
  7. use Illuminate\Contracts\Foundation\Application;
  8. use Illuminate\Contracts\Console\Kernel as KernelContract;
  9. class Kernel implements KernelContract
  10. {
  11. /**
  12. * The application implementation.
  13. *
  14. * @var \Illuminate\Contracts\Foundation\Application
  15. */
  16. protected $app;
  17. /**
  18. * The event dispatcher implementation.
  19. *
  20. * @var \Illuminate\Contracts\Events\Dispatcher
  21. */
  22. protected $events;
  23. /**
  24. * The Artisan application instance.
  25. *
  26. * @var \Illuminate\Console\Application
  27. */
  28. protected $artisan;
  29. /**
  30. * The bootstrap classes for the application.
  31. *
  32. * @var array
  33. */
  34. protected $bootstrappers = [
  35. 'Illuminate\Foundation\Bootstrap\DetectEnvironment',
  36. 'Illuminate\Foundation\Bootstrap\LoadConfiguration',
  37. 'Illuminate\Foundation\Bootstrap\ConfigureLogging',
  38. 'Illuminate\Foundation\Bootstrap\HandleExceptions',
  39. 'Illuminate\Foundation\Bootstrap\RegisterFacades',
  40. 'Illuminate\Foundation\Bootstrap\SetRequestForConsole',
  41. 'Illuminate\Foundation\Bootstrap\RegisterProviders',
  42. 'Illuminate\Foundation\Bootstrap\BootProviders',
  43. ];
  44. /**
  45. * Create a new console kernel instance.
  46. *
  47. * @param \Illuminate\Contracts\Foundation\Application $app
  48. * @param \Illuminate\Contracts\Events\Dispatcher $events
  49. * @return void
  50. */
  51. public function __construct(Application $app, Dispatcher $events)
  52. {
  53. if (! defined('ARTISAN_BINARY')) {
  54. define('ARTISAN_BINARY', 'artisan');
  55. }
  56. $this->app = $app;
  57. $this->events = $events;
  58. $this->app->booted(function () {
  59. $this->defineConsoleSchedule();
  60. });
  61. }
  62. /**
  63. * Define the application's command schedule.
  64. *
  65. * @return void
  66. */
  67. protected function defineConsoleSchedule()
  68. {
  69. $this->app->instance(
  70. 'Illuminate\Console\Scheduling\Schedule', $schedule = new Schedule
  71. );
  72. $this->schedule($schedule);
  73. }
  74. /**
  75. * Run the console application.
  76. *
  77. * @param \Symfony\Component\Console\Input\InputInterface $input
  78. * @param \Symfony\Component\Console\Output\OutputInterface $output
  79. * @return int
  80. */
  81. public function handle($input, $output = null)
  82. {
  83. try {
  84. $this->bootstrap();
  85. return $this->getArtisan()->run($input, $output);
  86. } catch (Exception $e) {
  87. $this->reportException($e);
  88. $this->renderException($output, $e);
  89. return 1;
  90. }
  91. }
  92. /**
  93. * Terminate the application.
  94. *
  95. * @param \Symfony\Component\Console\Input\InputInterface $input
  96. * @param int $status
  97. * @return void
  98. */
  99. public function terminate($input, $status)
  100. {
  101. $this->app->terminate();
  102. }
  103. /**
  104. * Define the application's command schedule.
  105. *
  106. * @param \Illuminate\Console\Scheduling\Schedule $schedule
  107. * @return void
  108. */
  109. protected function schedule(Schedule $schedule)
  110. {
  111. //
  112. }
  113. /**
  114. * Run an Artisan console command by name.
  115. *
  116. * @param string $command
  117. * @param array $parameters
  118. * @return int
  119. */
  120. public function call($command, array $parameters = [])
  121. {
  122. $this->bootstrap();
  123. // If we are calling a arbitary command from within the application, we will load
  124. // all of the available deferred providers which will make all of the commands
  125. // available to an application. Otherwise the command will not be available.
  126. $this->app->loadDeferredProviders();
  127. return $this->getArtisan()->call($command, $parameters);
  128. }
  129. /**
  130. * Queue the given console command.
  131. *
  132. * @param string $command
  133. * @param array $parameters
  134. * @return void
  135. */
  136. public function queue($command, array $parameters = [])
  137. {
  138. $this->app['Illuminate\Contracts\Queue\Queue']->push(
  139. 'Illuminate\Foundation\Console\QueuedJob', func_get_args()
  140. );
  141. }
  142. /**
  143. * Get all of the commands registered with the console.
  144. *
  145. * @return array
  146. */
  147. public function all()
  148. {
  149. $this->bootstrap();
  150. return $this->getArtisan()->all();
  151. }
  152. /**
  153. * Get the output for the last run command.
  154. *
  155. * @return string
  156. */
  157. public function output()
  158. {
  159. $this->bootstrap();
  160. return $this->getArtisan()->output();
  161. }
  162. /**
  163. * Bootstrap the application for HTTP requests.
  164. *
  165. * @return void
  166. */
  167. public function bootstrap()
  168. {
  169. if (!$this->app->hasBeenBootstrapped()) {
  170. $this->app->bootstrapWith($this->bootstrappers());
  171. }
  172. $this->app->loadDeferredProviders();
  173. }
  174. /**
  175. * Get the Artisan application instance.
  176. *
  177. * @return \Illuminate\Console\Application
  178. */
  179. protected function getArtisan()
  180. {
  181. if (is_null($this->artisan)) {
  182. return $this->artisan = (new Artisan($this->app, $this->events, $this->app->version()))
  183. ->resolveCommands($this->commands);
  184. }
  185. return $this->artisan;
  186. }
  187. /**
  188. * Get the bootstrap classes for the application.
  189. *
  190. * @return array
  191. */
  192. protected function bootstrappers()
  193. {
  194. return $this->bootstrappers;
  195. }
  196. /**
  197. * Report the exception to the exception handler.
  198. *
  199. * @param \Exception $e
  200. * @return void
  201. */
  202. protected function reportException(Exception $e)
  203. {
  204. $this->app['Illuminate\Contracts\Debug\ExceptionHandler']->report($e);
  205. }
  206. /**
  207. * Report the exception to the exception handler.
  208. *
  209. * @param \Symfony\Component\Console\Output\OutputInterface $output
  210. * @param \Exception $e
  211. * @return void
  212. */
  213. protected function renderException($output, Exception $e)
  214. {
  215. $this->app['Illuminate\Contracts\Debug\ExceptionHandler']->renderForConsole($output, $e);
  216. }
  217. }