Skip to content

Commit fafeb61

Browse files
authored
Merge pull request #3 from doppar/commands
make:job command for queue
2 parents a1f668d + 3d07c2e commit fafeb61

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

src/Commands/MakeJobCommand.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}

src/Commands/QueueRunCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct(QueueManager $manager)
5050

5151
/**
5252
* Execute the console command.
53+
* Example: php pool queue:run --queue=reports --sleep=10 --memory=1024 --timeout=3600
5354
*
5455
* @return int
5556
*/

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;
@@ -34,7 +35,8 @@ public function boot(): void
3435

3536
$this->commands([
3637
QueueRunCommand::class,
37-
QueueRetryCommand::class
38+
QueueRetryCommand::class,
39+
MakeJobCommand::class
3840
]);
3941
}
4042
}

0 commit comments

Comments
 (0)