Any way to send a closure in laravel 5?

In laravel 4 I can push to the close in the queue with queue::push(function...)

, but that doesn't work anymore in laravel 5. Instead, it seems that I have to create my own Job class for each function I want to push to the queue.

Since the functions I want to click are just a few lines and they are only used in one place, it actually seems like a waste of time and space to write a complete class for each case.

The best "solutions" I can think of right now are to either have a helper function that uses PHP reflection methods to dynamically generate a new class when called, or have a general assignment that takes a closure as a parameter, ie. dispatch(new ClosureJob(function(){...}));

They seem less perfect to me. Is there any other way to do this? Or will I have to implement one of them?

+3


source to share


1 answer


I did this by relying on the OpisClosure library . Extend the class like this:

class QueueableClosure extends SerializableClosure
{
    public function handle() {
        call_user_func_array($this->closure, func_get_args());
    }
}

      

Then use it like this:



Queue::push(new QueueableClosure(function(){
    Log::debug("this is the QueueableClosure in action.");
}));

      

NB See comment from @Quezler for possible restrictions!

+2


source







All Articles