From fedd4cd4d900656071d44fc1ee9c83e6de986fa8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 15 Dec 2016 13:23:15 -0600 Subject: [PATCH] Make it possible for queued handlers to specify their queue and connection. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/Illuminate/Events/Dispatcher.php | 42 ++++++++++++------- .../Foundation/Console/stubs/listener.stub | 3 ++ tests/Events/EventsDispatcherTest.php | 6 ++- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index b663d6b0b468..a8b008b1819f 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -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), + ]); } /** diff --git a/src/Illuminate/Foundation/Console/stubs/listener.stub b/src/Illuminate/Foundation/Console/stubs/listener.stub index 8ea1d2b18c85..ef70f8ad3cef 100644 --- a/src/Illuminate/Foundation/Console/stubs/listener.stub +++ b/src/Illuminate/Foundation/Console/stubs/listener.stub @@ -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. * diff --git a/tests/Events/EventsDispatcherTest.php b/tests/Events/EventsDispatcherTest.php index d8e2a7bdf634..373cf2c290ef 100755 --- a/tests/Events/EventsDispatcherTest.php +++ b/tests/Events/EventsDispatcherTest.php @@ -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; });