-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor event bus & drop symfony event bus
- Loading branch information
1 parent
65b5875
commit 00a3299
Showing
34 changed files
with
525 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\EventBus; | ||
|
||
use Patchlevel\EventSourcing\Attribute\Subscribe; | ||
use ReflectionClass; | ||
|
||
final class AttributeListenerProvider implements ListenerProvider | ||
{ | ||
/** @var array<string, list<ListenerDescriptor>>|null */ | ||
private array|null $subscribeMethods = null; | ||
|
||
/** @param iterable<object> $listeners */ | ||
public function __construct( | ||
private readonly iterable $listeners, | ||
) { | ||
} | ||
|
||
/** @return iterable<ListenerDescriptor> */ | ||
public function listenersForEvent(object $event): iterable | ||
{ | ||
if ($this->subscribeMethods !== null) { | ||
return $this->subscribeMethods[$event::class] ?? []; | ||
} | ||
|
||
$this->subscribeMethods = []; | ||
|
||
foreach ($this->listeners as $listener) { | ||
$reflection = new ReflectionClass($listener); | ||
$methods = $reflection->getMethods(); | ||
|
||
foreach ($methods as $method) { | ||
$attributes = $method->getAttributes(Subscribe::class); | ||
|
||
foreach ($attributes as $attribute) { | ||
$instance = $attribute->newInstance(); | ||
$eventClass = $instance->eventClass; | ||
|
||
$this->subscribeMethods[$eventClass][] = new ListenerDescriptor( | ||
$listener->{$method->getName()}(...), | ||
Check failure on line 42 in src/EventBus/AttributeListenerProvider.php GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)MixedMethodCall
|
||
); | ||
} | ||
} | ||
} | ||
|
||
return $this->subscribeMethods[$event::class] ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\EventBus; | ||
|
||
use Closure; | ||
use ReflectionFunction; | ||
|
||
final class ListenerDescriptor | ||
{ | ||
private readonly Closure $callable; | ||
private readonly string $name; | ||
|
||
public function __construct(callable $callable) | ||
{ | ||
$callable = $callable(...); | ||
|
||
$this->callable = $callable; | ||
|
||
$r = new ReflectionFunction($callable); | ||
|
||
if ($r->isAnonymous()) { | ||
$this->name = 'Closure'; | ||
} elseif (!$callable = $r->getClosureThis()) { | ||
$class = $r->getClosureCalledClass(); | ||
|
||
$this->name = ($class ? $class->name . '::' : '') . $r->name; | ||
} else { | ||
$this->name = $callable::class . '::' . $r->name; | ||
} | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function callable(): callable | ||
{ | ||
return $this->callable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\EventBus; | ||
|
||
interface ListenerProvider | ||
{ | ||
/** @return iterable<ListenerDescriptor> */ | ||
public function listenersForEvent(object $event): iterable; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.