|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RobersonFaria\DatabaseSchedule\Http\Services; |
| 4 | + |
| 5 | +use Illuminate\Console\Scheduling\Schedule; |
| 6 | +use Illuminate\Contracts\Console\Kernel; |
| 7 | +use Illuminate\Support\Facades\App; |
| 8 | +use ReflectionClass; |
| 9 | +use ReflectionMethod; |
| 10 | + |
| 11 | +class KernelExtractorService |
| 12 | +{ |
| 13 | + |
| 14 | + public function extract(): array |
| 15 | + { |
| 16 | + $kernel = App::make(Kernel::class); |
| 17 | + $schedule = App::make(Schedule::class); |
| 18 | + |
| 19 | + $method = new ReflectionMethod($kernel, 'schedule'); |
| 20 | + $method->setAccessible(true); |
| 21 | + $method->invoke($kernel, $schedule); |
| 22 | + |
| 23 | + $extractedSchedules = []; |
| 24 | + |
| 25 | + foreach ($schedule->events() as $event) { |
| 26 | + $extractedSchedules[] = $this->parseEvent($event); |
| 27 | + } |
| 28 | + |
| 29 | + return $extractedSchedules; |
| 30 | + } |
| 31 | + |
| 32 | + private function parseEvent($event): array |
| 33 | + { |
| 34 | + $command = $this->getCommand($event); |
| 35 | + |
| 36 | + // Get cron expression |
| 37 | + $expression = $event->getExpression(); |
| 38 | + |
| 39 | + // Get configurations |
| 40 | + $withoutOverlapping = $this->getProperty($event, 'withoutOverlapping'); |
| 41 | + $onOneServer = $this->getProperty($event, 'onOneServer'); |
| 42 | + $environments = $this->getEnvironments($event); |
| 43 | + |
| 44 | + return [ |
| 45 | + 'command' => $command['command'], |
| 46 | + 'params' => $command['params'], |
| 47 | + 'expression' => $expression, |
| 48 | + 'without_overlapping' => $withoutOverlapping, |
| 49 | + 'on_one_server' => $onOneServer, |
| 50 | + 'environments' => $environments, |
| 51 | + 'status' => true, |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + private function getCommand($event): array |
| 56 | + { |
| 57 | + if (!empty($event->description)) { |
| 58 | + return [ |
| 59 | + 'command' => $event->description, |
| 60 | + 'params' => [] |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + $fullCommand = $event->command ?? ''; |
| 65 | + |
| 66 | + $fullCommand = str_replace(["'/usr/local/bin/php'", "'artisan'"], '', $fullCommand); |
| 67 | + $fullCommand = trim(preg_replace('/\s+/', ' ', $fullCommand)); |
| 68 | + |
| 69 | + $parts = explode(' ', $fullCommand); |
| 70 | + $command = $parts[0] ?? ''; |
| 71 | + $params = array_slice($parts, 1); |
| 72 | + |
| 73 | + return [ |
| 74 | + 'command' => $command, |
| 75 | + 'params' => $this->formatParams($params), |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + private function formatParams(array $params): array |
| 80 | + { |
| 81 | + $formatted = []; |
| 82 | + foreach ($params as $index => $param) { |
| 83 | + $formatted["arg{$index}"] = ['value' => $param]; |
| 84 | + } |
| 85 | + return $formatted; |
| 86 | + } |
| 87 | + |
| 88 | + private function getProperty($event, string $property) |
| 89 | + { |
| 90 | + try { |
| 91 | + $reflection = new ReflectionClass($event); |
| 92 | + $prop = $reflection->getProperty($property); |
| 93 | + $prop->setAccessible(true); |
| 94 | + |
| 95 | + return $prop->getValue($event); |
| 96 | + } catch (\Exception $e) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private function getEnvironments($event): ?string |
| 102 | + { |
| 103 | + $environments = $this->getProperty($event, 'environments'); |
| 104 | + return $environments ? implode(',', $environments) : null; |
| 105 | + } |
| 106 | +} |
0 commit comments