PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jjpa2018/dashboard
PHP | 380 lines | 258 code | 30 blank | 92 comment | 2 complexity | bad4723fbb0f7153fdbc1e7b392700cb MD5 | raw file
  1. <?php
  2. namespace Illuminate\Foundation\Console;
  3. use Closure;
  4. use Illuminate\Console\Application as Artisan;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Console\Scheduling\Schedule;
  7. use Illuminate\Contracts\Console\Kernel as KernelContract;
  8. use Illuminate\Contracts\Debug\ExceptionHandler;
  9. use Illuminate\Contracts\Events\Dispatcher;
  10. use Illuminate\Contracts\Foundation\Application;
  11. use Illuminate\Support\Arr;
  12. use Illuminate\Support\Env;
  13. use Illuminate\Support\Str;
  14. use ReflectionClass;
  15. use Symfony\Component\Finder\Finder;
  16. use Throwable;
  17. class Kernel implements KernelContract
  18. {
  19. /**
  20. * The application implementation.
  21. *
  22. * @var \Illuminate\Contracts\Foundation\Application
  23. */
  24. protected $app;
  25. /**
  26. * The event dispatcher implementation.
  27. *
  28. * @var \Illuminate\Contracts\Events\Dispatcher
  29. */
  30. protected $events;
  31. /**
  32. * The Artisan application instance.
  33. *
  34. * @var \Illuminate\Console\Application|null
  35. */
  36. protected $artisan;
  37. /**
  38. * The Artisan commands provided by the application.
  39. *
  40. * @var array
  41. */
  42. protected $commands = [];
  43. /**
  44. * Indicates if the Closure commands have been loaded.
  45. *
  46. * @var bool
  47. */
  48. protected $commandsLoaded = false;
  49. /**
  50. * The bootstrap classes for the application.
  51. *
  52. * @var string[]
  53. */
  54. protected $bootstrappers = [
  55. \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
  56. \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
  57. \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
  58. \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
  59. \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class,
  60. \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
  61. \Illuminate\Foundation\Bootstrap\BootProviders::class,
  62. ];
  63. /**
  64. * Create a new console kernel instance.
  65. *
  66. * @param \Illuminate\Contracts\Foundation\Application $app
  67. * @param \Illuminate\Contracts\Events\Dispatcher $events
  68. * @return void
  69. */
  70. public function __construct(Application $app, Dispatcher $events)
  71. {
  72. if (! defined('ARTISAN_BINARY')) {
  73. define('ARTISAN_BINARY', 'artisan');
  74. }
  75. $this->app = $app;
  76. $this->events = $events;
  77. $this->app->booted(function () {
  78. $this->defineConsoleSchedule();
  79. });
  80. }
  81. /**
  82. * Define the application's command schedule.
  83. *
  84. * @return void
  85. */
  86. protected function defineConsoleSchedule()
  87. {
  88. $this->app->singleton(Schedule::class, function ($app) {
  89. return tap(new Schedule($this->scheduleTimezone()), function ($schedule) {
  90. $this->schedule($schedule->useCache($this->scheduleCache()));
  91. });
  92. });
  93. }
  94. /**
  95. * Get the name of the cache store that should manage scheduling mutexes.
  96. *
  97. * @return string
  98. */
  99. protected function scheduleCache()
  100. {
  101. return $this->app['config']->get('cache.schedule_store', Env::get('SCHEDULE_CACHE_DRIVER'));
  102. }
  103. /**
  104. * Run the console application.
  105. *
  106. * @param \Symfony\Component\Console\Input\InputInterface $input
  107. * @param \Symfony\Component\Console\Output\OutputInterface|null $output
  108. * @return int
  109. */
  110. public function handle($input, $output = null)
  111. {
  112. try {
  113. $this->bootstrap();
  114. return $this->getArtisan()->run($input, $output);
  115. } catch (Throwable $e) {
  116. $this->reportException($e);
  117. $this->renderException($output, $e);
  118. return 1;
  119. }
  120. }
  121. /**
  122. * Terminate the application.
  123. *
  124. * @param \Symfony\Component\Console\Input\InputInterface $input
  125. * @param int $status
  126. * @return void
  127. */
  128. public function terminate($input, $status)
  129. {
  130. $this->app->terminate();
  131. }
  132. /**
  133. * Define the application's command schedule.
  134. *
  135. * @param \Illuminate\Console\Scheduling\Schedule $schedule
  136. * @return void
  137. */
  138. protected function schedule(Schedule $schedule)
  139. {
  140. //
  141. }
  142. /**
  143. * Get the timezone that should be used by default for scheduled events.
  144. *
  145. * @return \DateTimeZone|string|null
  146. */
  147. protected function scheduleTimezone()
  148. {
  149. $config = $this->app['config'];
  150. return $config->get('app.schedule_timezone', $config->get('app.timezone'));
  151. }
  152. /**
  153. * Register the Closure based commands for the application.
  154. *
  155. * @return void
  156. */
  157. protected function commands()
  158. {
  159. //
  160. }
  161. /**
  162. * Register a Closure based command with the application.
  163. *
  164. * @param string $signature
  165. * @param \Closure $callback
  166. * @return \Illuminate\Foundation\Console\ClosureCommand
  167. */
  168. public function command($signature, Closure $callback)
  169. {
  170. $command = new ClosureCommand($signature, $callback);
  171. Artisan::starting(function ($artisan) use ($command) {
  172. $artisan->add($command);
  173. });
  174. return $command;
  175. }
  176. /**
  177. * Register all of the commands in the given directory.
  178. *
  179. * @param array|string $paths
  180. * @return void
  181. */
  182. protected function load($paths)
  183. {
  184. $paths = array_unique(Arr::wrap($paths));
  185. $paths = array_filter($paths, function ($path) {
  186. return is_dir($path);
  187. });
  188. if (empty($paths)) {
  189. return;
  190. }
  191. $namespace = $this->app->getNamespace();
  192. foreach ((new Finder)->in($paths)->files() as $command) {
  193. $command = $namespace.str_replace(
  194. ['/', '.php'],
  195. ['\\', ''],
  196. Str::after($command->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
  197. );
  198. if (is_subclass_of($command, Command::class) &&
  199. ! (new ReflectionClass($command))->isAbstract()) {
  200. Artisan::starting(function ($artisan) use ($command) {
  201. $artisan->resolve($command);
  202. });
  203. }
  204. }
  205. }
  206. /**
  207. * Register the given command with the console application.
  208. *
  209. * @param \Symfony\Component\Console\Command\Command $command
  210. * @return void
  211. */
  212. public function registerCommand($command)
  213. {
  214. $this->getArtisan()->add($command);
  215. }
  216. /**
  217. * Run an Artisan console command by name.
  218. *
  219. * @param string $command
  220. * @param array $parameters
  221. * @param \Symfony\Component\Console\Output\OutputInterface|null $outputBuffer
  222. * @return int
  223. *
  224. * @throws \Symfony\Component\Console\Exception\CommandNotFoundException
  225. */
  226. public function call($command, array $parameters = [], $outputBuffer = null)
  227. {
  228. $this->bootstrap();
  229. return $this->getArtisan()->call($command, $parameters, $outputBuffer);
  230. }
  231. /**
  232. * Queue the given console command.
  233. *
  234. * @param string $command
  235. * @param array $parameters
  236. * @return \Illuminate\Foundation\Bus\PendingDispatch
  237. */
  238. public function queue($command, array $parameters = [])
  239. {
  240. return QueuedCommand::dispatch(func_get_args());
  241. }
  242. /**
  243. * Get all of the commands registered with the console.
  244. *
  245. * @return array
  246. */
  247. public function all()
  248. {
  249. $this->bootstrap();
  250. return $this->getArtisan()->all();
  251. }
  252. /**
  253. * Get the output for the last run command.
  254. *
  255. * @return string
  256. */
  257. public function output()
  258. {
  259. $this->bootstrap();
  260. return $this->getArtisan()->output();
  261. }
  262. /**
  263. * Bootstrap the application for artisan commands.
  264. *
  265. * @return void
  266. */
  267. public function bootstrap()
  268. {
  269. if (! $this->app->hasBeenBootstrapped()) {
  270. $this->app->bootstrapWith($this->bootstrappers());
  271. }
  272. $this->app->loadDeferredProviders();
  273. if (! $this->commandsLoaded) {
  274. $this->commands();
  275. $this->commandsLoaded = true;
  276. }
  277. }
  278. /**
  279. * Get the Artisan application instance.
  280. *
  281. * @return \Illuminate\Console\Application
  282. */
  283. protected function getArtisan()
  284. {
  285. if (is_null($this->artisan)) {
  286. return $this->artisan = (new Artisan($this->app, $this->events, $this->app->version()))
  287. ->resolveCommands($this->commands);
  288. }
  289. return $this->artisan;
  290. }
  291. /**
  292. * Set the Artisan application instance.
  293. *
  294. * @param \Illuminate\Console\Application $artisan
  295. * @return void
  296. */
  297. public function setArtisan($artisan)
  298. {
  299. $this->artisan = $artisan;
  300. }
  301. /**
  302. * Get the bootstrap classes for the application.
  303. *
  304. * @return array
  305. */
  306. protected function bootstrappers()
  307. {
  308. return $this->bootstrappers;
  309. }
  310. /**
  311. * Report the exception to the exception handler.
  312. *
  313. * @param \Throwable $e
  314. * @return void
  315. */
  316. protected function reportException(Throwable $e)
  317. {
  318. $this->app[ExceptionHandler::class]->report($e);
  319. }
  320. /**
  321. * Render the given exception.
  322. *
  323. * @param \Symfony\Component\Console\Output\OutputInterface $output
  324. * @param \Throwable $e
  325. * @return void
  326. */
  327. protected function renderException($output, Throwable $e)
  328. {
  329. $this->app[ExceptionHandler::class]->renderForConsole($output, $e);
  330. }
  331. }