|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Doppar\Queue\Commands; |
| 4 | + |
| 5 | +use Phaseolies\Console\Schedule\Command; |
| 6 | + |
| 7 | +class MakeJobCommand extends Command |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The name and signature of the console command. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $name = 'make:job {name}'; |
| 15 | + |
| 16 | + /** |
| 17 | + * The description of the console command. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $description = 'Create a new Job class'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Execute the console command. |
| 25 | + * |
| 26 | + * @return int |
| 27 | + */ |
| 28 | + protected function handle(): int |
| 29 | + { |
| 30 | + return $this->executeWithTiming(function () { |
| 31 | + $name = $this->argument('name'); |
| 32 | + $parts = explode('/', $name); |
| 33 | + $className = array_pop($parts); |
| 34 | + |
| 35 | + // Ensure class name ends with Job |
| 36 | + if (!str_ends_with($className, 'Job')) { |
| 37 | + $className .= 'Job'; |
| 38 | + } |
| 39 | + |
| 40 | + $namespace = 'App\\Jobs' . (count($parts) > 0 ? '\\' . implode('\\', $parts) : ''); |
| 41 | + $filePath = base_path('app/Jobs/' . str_replace('/', DIRECTORY_SEPARATOR, $name) . '.php'); |
| 42 | + |
| 43 | + // Check if Job already exists |
| 44 | + if (file_exists($filePath)) { |
| 45 | + $this->displayError('Job already exists at:'); |
| 46 | + $this->line('<fg=white>' . str_replace(base_path(), '', $filePath) . '</>'); |
| 47 | + return Command::FAILURE; |
| 48 | + } |
| 49 | + |
| 50 | + // Create directory if needed |
| 51 | + $directoryPath = dirname($filePath); |
| 52 | + if (!is_dir($directoryPath)) { |
| 53 | + mkdir($directoryPath, 0755, true); |
| 54 | + } |
| 55 | + |
| 56 | + // Generate and save Job class |
| 57 | + $content = $this->generateJobContent($namespace, $className); |
| 58 | + file_put_contents($filePath, $content); |
| 59 | + |
| 60 | + $this->displaySuccess('Job created successfully'); |
| 61 | + $this->line('<fg=yellow>📦 File:</> <fg=white>' . str_replace(base_path(), '', $filePath) . '</>'); |
| 62 | + $this->newLine(); |
| 63 | + $this->line('<fg=yellow>⚙️ Class:</> <fg=white>' . $className . '</>'); |
| 64 | + |
| 65 | + return Command::SUCCESS; |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Generate Job class content. |
| 71 | + */ |
| 72 | + protected function generateJobContent(string $namespace, string $className): string |
| 73 | + { |
| 74 | + return <<<EOT |
| 75 | +<?php |
| 76 | +
|
| 77 | +namespace {$namespace}; |
| 78 | +
|
| 79 | +use Doppar\Queue\Job; |
| 80 | +
|
| 81 | +class {$className} extends Job |
| 82 | +{ |
| 83 | + /** |
| 84 | + * Execute the job. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function handle(): void |
| 89 | + { |
| 90 | + // |
| 91 | + } |
| 92 | +
|
| 93 | + /** |
| 94 | + * Handle a job failure. |
| 95 | + * |
| 96 | + * @param \\Throwable \$exception |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + public function failed(\\Throwable \$exception): void |
| 100 | + { |
| 101 | + // |
| 102 | + } |
| 103 | +} |
| 104 | +
|
| 105 | +EOT; |
| 106 | + } |
| 107 | +} |
0 commit comments