Skip to content

Commit 09d54ca

Browse files
committed
add lazy create helper method
1 parent 3c47d3a commit 09d54ca

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/Subscription/StatefulSubscriber/StatefulSubscriber.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
namespace Patchlevel\EventSourcing\Subscription\StatefulSubscriber;
66

7+
use LogicException;
78
use Patchlevel\EventSourcing\Subscription\Subscriber\BatchableSubscriber;
89
use Patchlevel\Hydrator\Attribute\Ignore;
10+
use ReflectionClass;
11+
12+
use const PHP_VERSION_ID;
913

1014
abstract class StatefulSubscriber implements BatchableSubscriber
1115
{
@@ -35,4 +39,17 @@ public function forceCommit(): bool
3539
{
3640
return false;
3741
}
42+
43+
public static function createLazy(StatefulSubscriberStore $store): static
44+
{
45+
if (PHP_VERSION_ID < 80400) {
46+
throw new LogicException('Lazy subscriber is only supported on PHP 8.4 or higher');
47+
}
48+
49+
$reflection = new ReflectionClass(static::class);
50+
51+
return $reflection->newLazyGhost(static function ($object) use ($store): void {
52+
$object->__construct($store);
53+
});
54+
}
3855
}

tests/Integration/Subscription/SubscriptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ public function testStatefulSubscriber(): void
16701670
$schemaDirector->create();
16711671

16721672
$subscriberRepository = new MetadataSubscriberAccessorRepository([
1673-
new ProfileInlineStatefulSubscriber(
1673+
ProfileInlineStatefulSubscriber::createLazy(
16741674
$stateStore,
16751675
),
16761676
]);

0 commit comments

Comments
 (0)