diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 307b14c435bf..afbe21c8272f 100644 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -610,22 +610,18 @@ protected function createListenerAndJob($class, $method, $arguments) protected function propagateListenerOptions($listener, $job) { return tap($job, function ($job) use ($listener) { - $job->tries = isset($listener->tries) ? $listener->tries : null; - + $job->afterCommit = property_exists($listener, 'afterCommit') ? $listener->afterCommit : null; + $job->backoff = method_exists($listener, 'backoff') ? $listener->backoff() : (isset($listener->backoff) ? $listener->backoff : null); $job->maxExceptions = isset($listener->maxExceptions) ? $listener->maxExceptions : null; - - $job->backoff = method_exists($listener, 'backoff') - ? $listener->backoff() : (isset($listener->backoff) ? $listener->backoff : null); - + $job->retryUntil = method_exists($listener, 'retryUntil') ? $listener->retryUntil() : null; + $job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted; $job->timeout = isset($listener->timeout) ? $listener->timeout : null; + $job->tries = isset($listener->tries) ? $listener->tries : null; - $job->retryUntil = method_exists($listener, 'retryUntil') - ? $listener->retryUntil() : null; - - $job->afterCommit = property_exists($listener, 'afterCommit') - ? $listener->afterCommit : null; - - $job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted; + $job->through(array_merge( + method_exists($listener, 'middleware') ? $listener->middleware() : [], + $listener->middleware ?? [] + )); }); } diff --git a/tests/Events/QueuedEventsTest.php b/tests/Events/QueuedEventsTest.php index 43e1f180a909..c4d96aa804d0 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); + } +}