From 207b18815482da2dc66d70585ee59e4e8c1ccce8 Mon Sep 17 00:00:00 2001 From: GuntherDebrauwer Date: Sun, 25 Jul 2021 16:02:26 +0200 Subject: [PATCH 1/2] Support job middleware on event listeners --- src/Illuminate/Events/Dispatcher.php | 7 +++++ tests/Events/QueuedEventsTest.php | 39 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 3454172eed07..4198276c99a7 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -557,6 +557,13 @@ protected function queueHandler($class, $method, $arguments) ? $listener->viaQueue() : $listener->queue ?? null; + $job->through( + array_merge( + method_exists($listener, 'middleware') ? $listener->middleware() : [], + $listener->middleware ?? [] + ) + ); + isset($listener->delay) ? $connection->laterOn($queue, $listener->delay, $job) : $connection->pushOn($queue, $job); diff --git a/tests/Events/QueuedEventsTest.php b/tests/Events/QueuedEventsTest.php index 1a6da8ef98c4..4c5ea4b66b6d 100644 --- a/tests/Events/QueuedEventsTest.php +++ b/tests/Events/QueuedEventsTest.php @@ -101,6 +101,24 @@ public function testQueuePropagateRetryUntilAndMaxExceptions() return $job->maxExceptions === 1 && $job->retryUntil !== null; }); } + + public function testQueuePropagateMiddleware() + { + $d = new Dispatcher; + + $fakeQueue = new QueueFake(new Container); + + $d->setQueueResolver(function () use ($fakeQueue) { + return $fakeQueue; + }); + + $d->listen('some.event', TestDispatcherMiddleware::class.'@handle'); + $d->dispatch('some.event', ['foo', 'bar']); + + $fakeQueue->assertPushed(CallQueuedListener::class, function ($job) { + return count($job->middleware) === 1 && $job->middleware[0] instanceof TestMiddleware; + }); + } } class TestDispatcherQueuedHandler implements ShouldQueue @@ -169,3 +187,24 @@ public function handle() // } } + +class TestDispatcherMiddleware implements ShouldQueue +{ + public function middleware() + { + return [new TestMiddleware()]; + } + + public function handle() + { + // + } +} + +class TestMiddleware +{ + public function handle($job, $next) + { + $next($job); + } +} From c01df37d75afc566ec550c27f59b49d0b8ba1b60 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 26 Jul 2021 08:46:59 -0500 Subject: [PATCH 2/2] formatting --- src/Illuminate/Events/Dispatcher.php | 29 +++++++++------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 4198276c99a7..b5bccfa58a6d 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -557,13 +557,6 @@ protected function queueHandler($class, $method, $arguments) ? $listener->viaQueue() : $listener->queue ?? null; - $job->through( - array_merge( - method_exists($listener, 'middleware') ? $listener->middleware() : [], - $listener->middleware ?? [] - ) - ); - isset($listener->delay) ? $connection->laterOn($queue, $listener->delay, $job) : $connection->pushOn($queue, $job); @@ -596,22 +589,18 @@ protected function createListenerAndJob($class, $method, $arguments) protected function propagateListenerOptions($listener, $job) { return tap($job, function ($job) use ($listener) { - $job->tries = $listener->tries ?? null; - + $job->afterCommit = property_exists($listener, 'afterCommit') ? $listener->afterCommit : null; + $job->backoff = method_exists($listener, 'backoff') ? $listener->backoff() : ($listener->backoff ?? null); $job->maxExceptions = $listener->maxExceptions ?? null; - - $job->backoff = method_exists($listener, 'backoff') - ? $listener->backoff() : ($listener->backoff ?? null); - + $job->retryUntil = method_exists($listener, 'retryUntil') ? $listener->retryUntil() : null; + $job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted; $job->timeout = $listener->timeout ?? null; + $job->tries = $listener->tries ?? null; - $job->afterCommit = property_exists($listener, 'afterCommit') - ? $listener->afterCommit : null; - - $job->retryUntil = method_exists($listener, 'retryUntil') - ? $listener->retryUntil() : null; - - $job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted; + $job->through(array_merge( + method_exists($listener, 'middleware') ? $listener->middleware() : [], + $listener->middleware ?? [] + )); }); }