From 425db3390e4fcca3806bc72019772f3b35322a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Debrauwer?= Date: Tue, 15 Apr 2025 18:20:44 +0200 Subject: [PATCH 1/2] Option to disable/enable dispatchAfterresponse --- src/Illuminate/Bus/Dispatcher.php | 37 +++++++++++++++++++ .../Integration/Queue/JobDispatchingTest.php | 32 ++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/Illuminate/Bus/Dispatcher.php b/src/Illuminate/Bus/Dispatcher.php index 32f917d796a6..5bcd6f2c7970 100644 --- a/src/Illuminate/Bus/Dispatcher.php +++ b/src/Illuminate/Bus/Dispatcher.php @@ -51,6 +51,13 @@ class Dispatcher implements QueueingDispatcher */ protected $queueResolver; + /** + * Indicates if dispatching after response is disabled. + * + * @var bool + */ + protected $afterResponseDisabled = false; + /** * Create a new command dispatcher instance. * @@ -252,11 +259,41 @@ protected function pushCommandToQueue($queue, $command) */ public function dispatchAfterResponse($command, $handler = null) { + if ($this->afterResponseDisabled) { + $this->dispatchSync($command); + + return; + } + $this->container->terminating(function () use ($command, $handler) { $this->dispatchSync($command, $handler); }); } + /** + * Disable dispatching after response. + * + * @return $this + */ + public function disableDispatchingAfterResponse() + { + $this->afterResponseDisabled = true; + + return $this; + } + + /** + * Enable dispatching after response. + * + * @return $this + */ + public function enableDispatchingAfterResponse() + { + $this->afterResponseDisabled = false; + + return $this; + } + /** * Set the pipes through which commands should be piped before dispatching. * diff --git a/tests/Integration/Queue/JobDispatchingTest.php b/tests/Integration/Queue/JobDispatchingTest.php index ebef0f344318..f2ae861dd0a6 100644 --- a/tests/Integration/Queue/JobDispatchingTest.php +++ b/tests/Integration/Queue/JobDispatchingTest.php @@ -10,6 +10,7 @@ use Illuminate\Queue\Events\JobQueued; use Illuminate\Queue\Events\JobQueueing; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Config; use Orchestra\Testbench\Attributes\WithMigration; @@ -165,6 +166,37 @@ public function testQueueMayBeNullForJobQueueingAndJobQueuedEvent() $this->assertNull($events[3]->queue); } + public function testCanDisableDispatchingAfterResponse() + { + Job::dispatchAfterResponse('test'); + + $this->assertFalse(Job::$ran); + + $this->app->terminate(); + + $this->assertTrue(Job::$ran); + + Bus::disableDispatchingAfterResponse(); + + Job::$ran = false; + Job::dispatchAfterResponse('test'); + + $this->assertTrue(Job::$ran); + + $this->app->terminate(); + + Bus::enableDispatchingAfterResponse(); + + Job::$ran = false; + Job::dispatchAfterResponse('test'); + + $this->assertFalse(Job::$ran); + + $this->app->terminate(); + + $this->assertTrue(Job::$ran); + } + /** * Helpers. */ From d51126912ac7fa6b858423bf0daf6c6231c1b3e4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 24 Apr 2025 14:40:41 -0500 Subject: [PATCH 2/2] formatting --- src/Illuminate/Bus/Dispatcher.php | 32 +++++++++---------- .../Integration/Queue/JobDispatchingTest.php | 4 +-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Illuminate/Bus/Dispatcher.php b/src/Illuminate/Bus/Dispatcher.php index 5bcd6f2c7970..0107b9e5acd4 100644 --- a/src/Illuminate/Bus/Dispatcher.php +++ b/src/Illuminate/Bus/Dispatcher.php @@ -56,7 +56,7 @@ class Dispatcher implements QueueingDispatcher * * @var bool */ - protected $afterResponseDisabled = false; + protected $allowsDispatchingAfterResponses = true; /** * Create a new command dispatcher instance. @@ -259,7 +259,7 @@ protected function pushCommandToQueue($queue, $command) */ public function dispatchAfterResponse($command, $handler = null) { - if ($this->afterResponseDisabled) { + if (! $this->allowsDispatchingAfterResponses) { $this->dispatchSync($command); return; @@ -271,51 +271,51 @@ public function dispatchAfterResponse($command, $handler = null) } /** - * Disable dispatching after response. + * Set the pipes through which commands should be piped before dispatching. * + * @param array $pipes * @return $this */ - public function disableDispatchingAfterResponse() + public function pipeThrough(array $pipes) { - $this->afterResponseDisabled = true; + $this->pipes = $pipes; return $this; } /** - * Enable dispatching after response. + * Map a command to a handler. * + * @param array $map * @return $this */ - public function enableDispatchingAfterResponse() + public function map(array $map) { - $this->afterResponseDisabled = false; + $this->handlers = array_merge($this->handlers, $map); return $this; } /** - * Set the pipes through which commands should be piped before dispatching. + * Allow dispatching after responses. * - * @param array $pipes * @return $this */ - public function pipeThrough(array $pipes) + public function withDispatchingAfterResponses() { - $this->pipes = $pipes; + $this->allowsDispatchingAfterResponses = true; return $this; } /** - * Map a command to a handler. + * Disable dispatching after responses. * - * @param array $map * @return $this */ - public function map(array $map) + public function withoutDispatchingAfterResponses() { - $this->handlers = array_merge($this->handlers, $map); + $this->allowsDispatchingAfterResponses = false; return $this; } diff --git a/tests/Integration/Queue/JobDispatchingTest.php b/tests/Integration/Queue/JobDispatchingTest.php index f2ae861dd0a6..eddfd83c23c1 100644 --- a/tests/Integration/Queue/JobDispatchingTest.php +++ b/tests/Integration/Queue/JobDispatchingTest.php @@ -176,7 +176,7 @@ public function testCanDisableDispatchingAfterResponse() $this->assertTrue(Job::$ran); - Bus::disableDispatchingAfterResponse(); + Bus::withoutDispatchingAfterResponses(); Job::$ran = false; Job::dispatchAfterResponse('test'); @@ -185,7 +185,7 @@ public function testCanDisableDispatchingAfterResponse() $this->app->terminate(); - Bus::enableDispatchingAfterResponse(); + Bus::withDispatchingAfterResponses(); Job::$ran = false; Job::dispatchAfterResponse('test');