Skip to content

Commit c68038e

Browse files
authored
Merge pull request #14 from techmahedy/techmahedy-queue
make:job command added in doppar queue
2 parents 480bebe + e5caf97 commit c68038e

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

src/Commands/MakeJobCommand.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
use Doppar\Queue\Dispatchable;
81+
// use Doppar\Queue\Attributes\Queueable;
82+
83+
// #[Queueable(tries: 3, retryAfter: 60, delayFor: 300, onQueue: 'default')]
84+
class {$className} extends Job
85+
{
86+
use Dispatchable;
87+
88+
/**
89+
* Create a new job instance.
90+
*/
91+
public function __construct(){}
92+
93+
/**
94+
* Execute the job.
95+
*
96+
* @return void
97+
*/
98+
public function handle(): void
99+
{
100+
//
101+
}
102+
103+
/**
104+
* Handle a job failure.
105+
*
106+
* @param \\Throwable \$exception
107+
* @return void
108+
*/
109+
public function failed(\\Throwable \$exception): void
110+
{
111+
//
112+
}
113+
}
114+
115+
EOT;
116+
}
117+
}

src/QueueServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Doppar\Queue;
44

5+
use Doppar\Queue\Commands\MakeJobCommand;
56
use Phaseolies\Providers\ServiceProvider;
67
use Doppar\Queue\QueueManager;
78
use Doppar\Queue\Commands\QueueRunCommand;
@@ -40,7 +41,8 @@ public function boot(): void
4041
QueueRetryCommand::class,
4142
QueueFlushCommand::class,
4243
QueueFailedCommand::class,
43-
QueueMonitorCommand::class
44+
QueueMonitorCommand::class,
45+
MakeJobCommand::class
4446
]);
4547
}
4648
}

0 commit comments

Comments
 (0)