Skip to content

Commit

Permalink
Make it possible for queued handlers to specify their queue and conne…
Browse files Browse the repository at this point in the history
…ction.

Previously it was impossible for a queued handler to be queued on
anything other than the default connection and queue. This makes that
configurable. The Queueable trait is also applied to the listener stub.
Even though it isn’t explicitly necessary I think it provides
communication to the user that these options may be customized by
overriding the properties.
  • Loading branch information
taylorotwell committed Dec 15, 2016
1 parent c54b09b commit fedd4cd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
42 changes: 27 additions & 15 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,46 +405,58 @@ protected function handlerShouldBeQueued($class)
protected function createQueuedHandlerCallable($class, $method)
{
return function () use ($class, $method) {
$arguments = $this->cloneArgumentsForQueueing(func_get_args());
$arguments = array_map(function ($a) {
return is_object($a) ? clone $a : $a;
}, func_get_args());

if (method_exists($class, 'queue')) {
$this->callQueueMethodOnHandler($class, $method, $arguments);
} else {
$this->resolveQueue()->push('Illuminate\Events\CallQueuedHandler@call', [
'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);
$this->queueHandler($class, $method, $arguments);
}
};
}

/**
* Clone the given arguments for queueing.
* Call the queue method on the handler class.
*
* @param string $class
* @param string $method
* @param array $arguments
* @return array
* @return void
*/
protected function cloneArgumentsForQueueing(array $arguments)
protected function callQueueMethodOnHandler($class, $method, $arguments)
{
return array_map(function ($a) {
return is_object($a) ? clone $a : $a;
}, $arguments);
$handler = (new ReflectionClass($class))->newInstanceWithoutConstructor();

$handler->queue($this->resolveQueue(), 'Illuminate\Events\CallQueuedHandler@call', [
'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);
}

/**
* Call the queue method on the handler class.
* Queue the handler class.
*
* @param string $class
* @param string $method
* @param array $arguments
* @return void
*/
protected function callQueueMethodOnHandler($class, $method, $arguments)
protected function queueHandler($class, $method, $arguments)
{
$handler = (new ReflectionClass($class))->newInstanceWithoutConstructor();

$handler->queue($this->resolveQueue(), 'Illuminate\Events\CallQueuedHandler@call', [
'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);
$connection = isset($handler->connection) ? $handler->connection : null;

$queue = isset($handler->queue) ? $handler->queue : null;

$this->resolveQueue()
->connection($connection)
->pushOn($queue, 'Illuminate\Events\CallQueuedHandler@call', [
'class' => $class,
'method' => $method,
'data' => serialize($arguments),
]);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/listener.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace DummyNamespace;

use DummyFullEvent;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DummyClass
{
use Queueable;

/**
* Create the event listener.
*
Expand Down
6 changes: 5 additions & 1 deletion tests/Events/EventsDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,15 @@ public function testQueuedEventHandlersAreQueued()
{
$d = new Dispatcher;
$queue = m::mock('Illuminate\Contracts\Queue\Queue');
$queue->shouldReceive('push')->once()->with('Illuminate\Events\CallQueuedHandler@call', [

$queue->shouldReceive('connection')->once()->with(null)->andReturnSelf();

$queue->shouldReceive('pushOn')->once()->with(null, 'Illuminate\Events\CallQueuedHandler@call', [
'class' => 'TestDispatcherQueuedHandler',
'method' => 'someMethod',
'data' => serialize(['foo', 'bar']),
]);

$d->setQueueResolver(function () use ($queue) {
return $queue;
});
Expand Down

0 comments on commit fedd4cd

Please sign in to comment.