Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/Commands/MakeJobCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
1 change: 1 addition & 0 deletions src/Commands/QueueRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function __construct(QueueManager $manager)

/**
* Execute the console command.
* Example: php pool queue:run --queue=reports --sleep=10 --memory=1024 --timeout=3600
*
* @return int
*/
Expand Down
4 changes: 3 additions & 1 deletion src/QueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doppar\Queue;

use Doppar\Queue\Commands\MakeJobCommand;
use Phaseolies\Providers\ServiceProvider;
use Doppar\Queue\QueueManager;
use Doppar\Queue\Commands\QueueRunCommand;
Expand Down Expand Up @@ -34,7 +35,8 @@ public function boot(): void

$this->commands([
QueueRunCommand::class,
QueueRetryCommand::class
QueueRetryCommand::class,
MakeJobCommand::class
]);
}
}
Loading