File tree Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,13 @@ class PendingBatch
47
47
*/
48
48
public $ options = [];
49
49
50
+ /**
51
+ * Jobs that have been verified to contain the Batchable trait.
52
+ *
53
+ * @var array<class-string, bool>
54
+ */
55
+ protected static $ batchableClasses = [];
56
+
50
57
/**
51
58
* Create a new pending batch instance.
52
59
*
@@ -92,14 +99,16 @@ protected function ensureJobIsBatchable(object|array $job): void
92
99
{
93
100
foreach (Arr::wrap ($ job ) as $ job ) {
94
101
if ($ job instanceof PendingBatch) {
95
- $ this ->ensureJobIsBatchable ($ job ->jobs ->all ());
96
-
97
102
return ;
98
103
}
99
104
100
- if (! in_array (Batchable::class, class_uses_recursive ($ job ))) {
105
+ if (! (static ::$ batchableClasses [$ job ::class] ?? false ) && ! in_array (Batchable::class, class_uses_recursive ($ job ))) {
106
+ static ::$ batchableClasses [$ job ::class] = false ;
107
+
101
108
throw new RuntimeException (sprintf ('Attempted to batch job [%s], but it does not use the Batchable trait. ' , $ job ::class));
102
109
}
110
+
111
+ static ::$ batchableClasses [$ job ::class] = true ;
103
112
}
104
113
}
105
114
Original file line number Diff line number Diff line change @@ -231,4 +231,22 @@ public function test_it_throws_exception_if_batched_job_is_not_batchable(): void
231
231
232
232
new PendingBatch (new Container , new Collection ([$ nonBatchableJob ]));
233
233
}
234
+
235
+ public function test_it_throws_an_exception_if_batched_job_contains_batch_with_nonbatchable_job (): void
236
+ {
237
+ $ this ->expectException (RuntimeException::class);
238
+
239
+ $ container = new Container ;
240
+ new PendingBatch (
241
+ $ container ,
242
+ new Collection (
243
+ [new PendingBatch ($ container , new Collection ([new BatchableJob , new class {}]))]
244
+ )
245
+ );
246
+ }
247
+ }
248
+
249
+ class BatchableJob
250
+ {
251
+ use Batchable;
234
252
}
You can’t perform that action at this time.
0 commit comments