Skip to content

Commit 39567bb

Browse files
committed
simplify PendingBatch@ensureJobIsBatchable; memoize the classes which are batchable
1 parent 1eaf24b commit 39567bb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Illuminate/Bus/PendingBatch.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class PendingBatch
4747
*/
4848
public $options = [];
4949

50+
/**
51+
* Batches that have been checked to contain the Batchable trait.
52+
*
53+
* @var array<class-string, bool>
54+
*/
55+
protected static $batchableClasses = [];
56+
5057
/**
5158
* Create a new pending batch instance.
5259
*
@@ -97,9 +104,13 @@ protected function ensureJobIsBatchable(object|array $job): void
97104
return;
98105
}
99106

100-
if (! in_array(Batchable::class, class_uses_recursive($job))) {
107+
if (! (static::$batchableClasses[$job::class] ?? false) && ! in_array(Batchable::class, class_uses_recursive($job))) {
108+
static::$batchableClasses[$job::class] = false;
109+
101110
throw new RuntimeException(sprintf('Attempted to batch job [%s], but it does not use the Batchable trait.', $job::class));
102111
}
112+
113+
static::$batchableClasses[$job::class] = true;
103114
}
104115
}
105116

tests/Bus/BusPendingBatchTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,22 @@ public function test_it_throws_exception_if_batched_job_is_not_batchable(): void
231231

232232
new PendingBatch(new Container, new Collection([$nonBatchableJob]));
233233
}
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;
234252
}

0 commit comments

Comments
 (0)