Skip to content

Commit 3ed70e9

Browse files
committed
Pass listener arguments to middleware method
1 parent 6c0d272 commit 3ed70e9

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Illuminate/Events/Dispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ protected function createListenerAndJob($class, $method, $arguments)
612612
* Propagate listener options to the job.
613613
*
614614
* @param mixed $listener
615-
* @param mixed $job
615+
* @param \Illuminate\Events\CallQueuedListener $job
616616
* @return mixed
617617
*/
618618
protected function propagateListenerOptions($listener, $job)
@@ -627,7 +627,7 @@ protected function propagateListenerOptions($listener, $job)
627627
$job->tries = $listener->tries ?? null;
628628

629629
$job->through(array_merge(
630-
method_exists($listener, 'middleware') ? $listener->middleware() : [],
630+
method_exists($listener, 'middleware') ? $listener->middleware(...$job->data) : [],
631631
$listener->middleware ?? []
632632
));
633633
});

tests/Events/QueuedEventsTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ public function testQueuePropagateMiddleware()
116116
$d->dispatch('some.event', ['foo', 'bar']);
117117

118118
$fakeQueue->assertPushed(CallQueuedListener::class, function ($job) {
119-
return count($job->middleware) === 1 && $job->middleware[0] instanceof TestMiddleware;
119+
return count($job->middleware) === 1
120+
&& $job->middleware[0] instanceof TestMiddleware
121+
&& $job->middleware[0]->a === 'foo'
122+
&& $job->middleware[0]->b === 'bar';
120123
});
121124
}
122125
}
@@ -190,19 +193,28 @@ public function handle()
190193

191194
class TestDispatcherMiddleware implements ShouldQueue
192195
{
193-
public function middleware()
196+
public function middleware($a, $b)
194197
{
195-
return [new TestMiddleware()];
198+
return [new TestMiddleware($a, $b)];
196199
}
197200

198-
public function handle()
201+
public function handle($a, $b)
199202
{
200203
//
201204
}
202205
}
203206

204207
class TestMiddleware
205208
{
209+
public $a;
210+
public $b;
211+
212+
public function __construct($a, $b)
213+
{
214+
$this->a = $a;
215+
$this->b = $b;
216+
}
217+
206218
public function handle($job, $next)
207219
{
208220
$next($job);

0 commit comments

Comments
 (0)