Skip to content

Commit 6ecff0f

Browse files
committed
Add defer method to PendingDispatch
1 parent 45cb5bb commit 6ecff0f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Illuminate/Foundation/Bus/PendingDispatch.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class PendingDispatch
2727
*/
2828
protected $afterResponse = false;
2929

30+
/**
31+
* Indicates if the job should be dispatched to the queue after the current request.
32+
*
33+
* @var bool
34+
*/
35+
protected $defer = true;
36+
3037
/**
3138
* Create a new pending job dispatch.
3239
*
@@ -163,6 +170,18 @@ public function afterResponse()
163170
return $this;
164171
}
165172

173+
/**
174+
* Indicate that the job should be dispatched to the queue after the current request.
175+
*
176+
* @return $this
177+
*/
178+
public function defer(): static
179+
{
180+
$this->defer = true;
181+
182+
return $this;
183+
}
184+
166185
/**
167186
* Determine if the job should be dispatched.
168187
*
@@ -217,6 +236,8 @@ public function __destruct()
217236
return;
218237
} elseif ($this->afterResponse) {
219238
app(Dispatcher::class)->dispatchAfterResponse($this->job);
239+
} elseif ($this->defer) {
240+
defer(fn () => app(Dispatcher::class)->dispatch($this->job));
220241
} else {
221242
app(Dispatcher::class)->dispatch($this->job);
222243
}

tests/Bus/BusPendingDispatchTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ public function testAfterResponse()
103103
);
104104
}
105105

106+
public function testDefer()
107+
{
108+
$this->pendingDispatch->defer();
109+
$this->assertTrue(
110+
(new ReflectionClass($this->pendingDispatch))->getProperty('defer')->getValue($this->pendingDispatch)
111+
);
112+
}
113+
106114
public function testGetJob()
107115
{
108116
$this->assertSame($this->job, $this->pendingDispatch->getJob());

0 commit comments

Comments
 (0)