From de1717893a825ac2746a4f7fbfd95d8bbbea0a06 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Thu, 10 Aug 2023 09:14:05 +0100 Subject: [PATCH] Filter out abstract event handlers --- src/Support/DiscoverEventHandlers.php | 2 ++ .../AutoDiscoverEventHandlers/AbstractProjector.php | 9 +++++++++ .../AutoDiscoverEventHandlers/AbstractReactor.php | 9 +++++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/TestClasses/AutoDiscoverEventHandlers/AbstractProjector.php create mode 100644 tests/TestClasses/AutoDiscoverEventHandlers/AbstractReactor.php diff --git a/src/Support/DiscoverEventHandlers.php b/src/Support/DiscoverEventHandlers.php index 151de93b..7a713da4 100644 --- a/src/Support/DiscoverEventHandlers.php +++ b/src/Support/DiscoverEventHandlers.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; +use ReflectionClass; use Spatie\EventSourcing\EventHandlers\EventHandler; use Spatie\EventSourcing\Projectionist; use SplFileInfo; @@ -64,6 +65,7 @@ public function addToProjectionist(Projectionist $projectionist) ->reject(fn (SplFileInfo $file) => in_array($file->getPathname(), $this->ignoredFiles)) ->map(fn (SplFileInfo $file) => $this->fullQualifiedClassNameFromFile($file)) ->filter(fn (string $eventHandlerClass) => is_subclass_of($eventHandlerClass, EventHandler::class)) + ->filter(fn (string $eventHandlerClass) => (new ReflectionClass($eventHandlerClass))->isInstantiable()) ->pipe(function (Collection $eventHandlers) use ($projectionist) { $projectionist->addEventHandlers($eventHandlers->toArray()); }); diff --git a/tests/TestClasses/AutoDiscoverEventHandlers/AbstractProjector.php b/tests/TestClasses/AutoDiscoverEventHandlers/AbstractProjector.php new file mode 100644 index 00000000..e33c57c1 --- /dev/null +++ b/tests/TestClasses/AutoDiscoverEventHandlers/AbstractProjector.php @@ -0,0 +1,9 @@ +