Skip to content

Commit

Permalink
1.9.11 - add run method to QueueJob
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozerich committed Jun 29, 2023
1 parent 169c640 commit 8a91adf
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Jobs/QueueJob.php
Expand Up @@ -11,4 +11,41 @@
class QueueJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


protected static function getParameterValueForCommand($command, \ReflectionParameter $parameter, array $extras = [])
{
if (array_key_exists($parameter->name, $extras)) {
return $extras[$parameter->name];
}

if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}

throw new \Exception("Unable to map parameter [{$parameter->name}] to command [{$command}]");
}

protected static function marshal($command, array $extras = [])
{
$injected = [];

$reflection = new \ReflectionClass($command);
if ($constructor = $reflection->getConstructor()) {
$injected = array_map(function ($parameter) use ($command, $extras) {
return self::getParameterValueForCommand($command, $parameter, $extras);
}, $constructor->getParameters());
}

return $reflection->newInstanceArgs($injected);
}

public function runNow($job, array $arguments = [])
{
if (!is_object($job)) {
$job = $this->marshal($job, $arguments);
}

return dispatch_sync($job);
}
}

0 comments on commit 8a91adf

Please sign in to comment.