Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
37.50% |
3 / 8 |
CRAP | |
59.62% |
31 / 52 |
| Kernel | |
0.00% |
0 / 1 |
|
37.50% |
3 / 8 |
39.34 | |
59.62% |
31 / 52 |
| defineConsoleSchedule | |
0.00% |
0 / 1 |
1.12 | |
50.00% |
3 / 6 |
|||
| schedule | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 6 |
|||
| noServiceNameSchedule | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 6 |
|||
| commands | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| mapBaseCommands | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| mapServicesCommands | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| mapServiceCommands | |
0.00% |
0 / 1 |
2.06 | |
75.00% |
3 / 4 |
|||
| loadService | |
0.00% |
0 / 1 |
7.77 | |
75.00% |
15 / 20 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\MicroService\Console; |
| 4 | |
| 5 | use Illuminate\Console\Scheduling\Schedule; |
| 6 | use Qmp\Laravel\CommandsLaravel\Console\Scheduling\Schedule as ScheduleCustom; |
| 7 | use Illuminate\Foundation\Console\Kernel as ConsoleKernel; |
| 8 | |
| 9 | use ReflectionClass; |
| 10 | use Illuminate\Support\Arr; |
| 11 | use Illuminate\Support\Str; |
| 12 | use Illuminate\Console\Command; |
| 13 | use Symfony\Component\Finder\Finder; |
| 14 | use Illuminate\Console\Application as Artisan; |
| 15 | |
| 16 | use Illuminate\Support\Facades\File; |
| 17 | |
| 18 | class Kernel extends ConsoleKernel |
| 19 | { |
| 20 | |
| 21 | /** |
| 22 | * Define the application's command schedule. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | protected function defineConsoleSchedule() |
| 27 | { |
| 28 | $this->app->singleton(Schedule::class, function ($app) { |
| 29 | return tap(new ScheduleCustom($this->scheduleTimezone()), function ($schedule) { |
| 30 | $this->schedule($schedule->useCache($this->scheduleCache())); |
| 31 | }); |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Define the application's command schedule. |
| 37 | * |
| 38 | * @param \Illuminate\Console\Scheduling\Schedule $schedule |
| 39 | * @return void |
| 40 | */ |
| 41 | protected function schedule(Schedule $schedule) |
| 42 | { |
| 43 | $serviceName = config('micro-services.name'); |
| 44 | $schedulerPath = base_path('vendor/qwamplify/' . $serviceName . '/src/routes/scheduler.php'); |
| 45 | |
| 46 | if (!empty($serviceName) && file_exists($schedulerPath)) { |
| 47 | require $schedulerPath; |
| 48 | } else { |
| 49 | $this->noServiceNameSchedule($schedule); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | protected function noServiceNameSchedule(Schedule $schedule) |
| 54 | { |
| 55 | $schedule->command('command:purge-monitoring') |
| 56 | ->daily() |
| 57 | ->onOneServer() |
| 58 | ->runInBackground() |
| 59 | ->withCronsTable(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Register the commands for the application. |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | protected function commands() |
| 68 | { |
| 69 | $this->mapBaseCommands(); |
| 70 | $this->mapServicesCommands(); |
| 71 | } |
| 72 | |
| 73 | protected function mapBaseCommands() |
| 74 | { |
| 75 | $this->load(app_path('Console/Commands')); |
| 76 | require base_path('routes/console.php'); |
| 77 | } |
| 78 | |
| 79 | protected function mapServicesCommands() { |
| 80 | $fields = File::directories(base_path('vendor/qwamplify')); |
| 81 | foreach ($fields as $folder) { |
| 82 | $this->mapServiceCommands($folder); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | protected function mapServiceCommands(string $path) |
| 87 | { |
| 88 | $this->loadService($path . '/src/Console'); |
| 89 | if (file_exists($path . 'routes/console.php')) { |
| 90 | require $path . 'routes/console.php'; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | protected function loadService($paths) |
| 95 | { |
| 96 | $paths = array_unique(Arr::wrap($paths)); |
| 97 | |
| 98 | $paths = array_filter($paths, function ($path) { |
| 99 | return is_dir($path); |
| 100 | }); |
| 101 | |
| 102 | if (empty($paths)) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | $namespace = $this->app->getNamespace(); |
| 107 | |
| 108 | foreach ((new Finder)->in($paths)->files() as $command) { |
| 109 | // Get good namespace instead of default "\App\Console\Commands" |
| 110 | if (preg_match_all('/namespace (.+)/', $command->getContents(), $matchNamespaces, PREG_SET_ORDER) && isset($matchNamespaces[0][1])) { |
| 111 | $command = str_replace(';', '\\', $matchNamespaces[0][1]) . str_replace('.php', '', $command->getFilename()); |
| 112 | // Get default namespace |
| 113 | } else { |
| 114 | $command = $namespace.str_replace( |
| 115 | ['/', '.php'], |
| 116 | ['\\', ''], |
| 117 | Str::after($command->getPathname(), realpath(app_path()).DIRECTORY_SEPARATOR) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | if (is_subclass_of($command, Command::class) && |
| 122 | ! (new ReflectionClass($command))->isAbstract()) { |
| 123 | Artisan::starting(function ($artisan) use ($command) { |
| 124 | $artisan->resolve($command); |
| 125 | }); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |