-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakeJobCommand.php
More file actions
107 lines (89 loc) · 2.61 KB
/
MakeJobCommand.php
File metadata and controls
107 lines (89 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Doppar\Queue\Commands;
use Phaseolies\Console\Schedule\Command;
class MakeJobCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:job {name}';
/**
* The description of the console command.
*
* @var string
*/
protected $description = 'Create a new Job class';
/**
* Execute the console command.
*
* @return int
*/
protected function handle(): int
{
return $this->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('<fg=white>' . 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('<fg=yellow>📦 File:</> <fg=white>' . str_replace(base_path(), '', $filePath) . '</>');
$this->newLine();
$this->line('<fg=yellow>⚙️ Class:</> <fg=white>' . $className . '</>');
return Command::SUCCESS;
});
}
/**
* Generate Job class content.
*/
protected function generateJobContent(string $namespace, string $className): string
{
return <<<EOT
<?php
namespace {$namespace};
use Doppar\Queue\Job;
class {$className} extends Job
{
/**
* Execute the job.
*
* @return void
*/
public function handle(): void
{
//
}
/**
* Handle a job failure.
*
* @param \\Throwable \$exception
* @return void
*/
public function failed(\\Throwable \$exception): void
{
//
}
}
EOT;
}
}