From 3d07c2e4ff7fd369b66a81b682bd76209aeed406 Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Thu, 13 Nov 2025 22:33:53 +0600 Subject: [PATCH] make:job command for queue --- src/Commands/MakeJobCommand.php | 107 +++++++++++++++++++++++++++++++ src/Commands/QueueRunCommand.php | 1 + src/QueueServiceProvider.php | 4 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/Commands/MakeJobCommand.php diff --git a/src/Commands/MakeJobCommand.php b/src/Commands/MakeJobCommand.php new file mode 100644 index 0000000..00e8de8 --- /dev/null +++ b/src/Commands/MakeJobCommand.php @@ -0,0 +1,107 @@ +executeWithTiming(function () { + $name = $this->argument('name'); + $parts = explode('/', $name); + $className = array_pop($parts); + + // Ensure class name ends with Job + if (!str_ends_with($className, 'Job')) { + $className .= 'Job'; + } + + $namespace = 'App\\Jobs' . (count($parts) > 0 ? '\\' . implode('\\', $parts) : ''); + $filePath = base_path('app/Jobs/' . str_replace('/', DIRECTORY_SEPARATOR, $name) . '.php'); + + // Check if Job already exists + if (file_exists($filePath)) { + $this->displayError('Job already exists at:'); + $this->line('' . str_replace(base_path(), '', $filePath) . ''); + return Command::FAILURE; + } + + // Create directory if needed + $directoryPath = dirname($filePath); + if (!is_dir($directoryPath)) { + mkdir($directoryPath, 0755, true); + } + + // Generate and save Job class + $content = $this->generateJobContent($namespace, $className); + file_put_contents($filePath, $content); + + $this->displaySuccess('Job created successfully'); + $this->line('📦 File: ' . str_replace(base_path(), '', $filePath) . ''); + $this->newLine(); + $this->line('⚙️ Class: ' . $className . ''); + + return Command::SUCCESS; + }); + } + + /** + * Generate Job class content. + */ + protected function generateJobContent(string $namespace, string $className): string + { + return <<commands([ QueueRunCommand::class, - QueueRetryCommand::class + QueueRetryCommand::class, + MakeJobCommand::class ]); } }