josegonzalez/php-queuesadilla

Add support for passing constructor parameters for job callables

cleptric opened this issue · 4 comments

Add support for passing constructor parameters for job callables

Even better, use psr/container to get jobs:

$job = $this->container->get($item['class']);

What? Can you explain what exactly you mean by this?

Instead of requiring jobs to be callables, require that jobs are container identifiers that point to a callable. That way the job name can be anything, so long as it resolves into a callable. For instance:

class HelloJob
{
    public function __invoke($job)
    {
        echo "Hello ", $job->data()['name'] ?? 'world';
    }
}

$queue->push(HelloJob::class, ['name' => 'Jose']);

In this way, the HelloJob can have whatever constructor arguments it wants, as the container will instantiate it when $container->get(HelloJob::class) is called.

As a means of progressive enhancement, you could even do:

if (!is_callable($item['class'])) {
    // assume that the job class is actually a container identifier
    $item['class'] = $this->container->get($item['class']);
}

I still have no idea what this means, so pull requests welcome? :D

EDIT: I don't really use containers for anything since I personally don't find utility in them and feel they tend to result in spaghetti code, but if this is a non-required change in usage/setup, then I'm on board with it.