File tree Expand file tree Collapse file tree 7 files changed +107
-0
lines changed
Expand file tree Collapse file tree 7 files changed +107
-0
lines changed Original file line number Diff line number Diff line change 99use Tempest \Support \Str ;
1010use UnitEnum ;
1111
12+ use function Tempest \reflect ;
13+
1214final readonly class GenericEventBus implements EventBus
1315{
1416 public function __construct (
@@ -55,13 +57,18 @@ private function resolveHandlers(string|object $event): array
5557 return $ handlers ;
5658 }
5759
60+ /** @param \Tempest\EventBus\CallableEventHandler[] $eventHandlers */
5861 private function getCallable (array $ eventHandlers ): EventBusMiddlewareCallable
5962 {
6063 $ callable = new EventBusMiddlewareCallable (function (string |object $ event ) use ($ eventHandlers ): void {
6164 foreach ($ eventHandlers as $ eventHandler ) {
6265 $ callable = $ eventHandler ->normalizeCallable ($ this ->container );
6366
6467 $ callable ($ event );
68+
69+ if (is_object ($ event ) && reflect ($ event )->hasAttribute (WithoutPropagation::class) || $ eventHandler ->handler ->handler ->hasAttribute (WithoutPropagation::class)) {
70+ break ;
71+ }
6572 }
6673 });
6774
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tempest \EventBus ;
4+
5+ use Attribute ;
6+
7+ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD )]
8+ final readonly class WithoutPropagation
9+ {
10+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Tempest \Fixtures \Events ;
4+
5+ final readonly class EventForListenerWithoutPropagation
6+ {
7+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Tempest \Fixtures \Events ;
4+
5+ use Tempest \EventBus \WithoutPropagation ;
6+
7+ #[WithoutPropagation]
8+ final readonly class EventWithoutPropagation
9+ {
10+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Tempest \Fixtures \Events ;
4+
5+ use Tempest \Container \Singleton ;
6+ use Tempest \EventBus \EventHandler ;
7+ use Tempest \EventBus \WithoutPropagation ;
8+
9+ #[Singleton]
10+ final class HandlersForEventWithListenerWithoutPropagation
11+ {
12+ public int $ count = 0 ;
13+
14+ #[EventHandler, WithoutPropagation]
15+ public function a (EventForListenerWithoutPropagation $ event ): void
16+ {
17+ $ this ->count ++;
18+ }
19+
20+ #[EventHandler]
21+ public function b (EventForListenerWithoutPropagation $ event ): void
22+ {
23+ $ this ->count ++;
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Tempest \Fixtures \Events ;
4+
5+ use Tempest \Container \Singleton ;
6+ use Tempest \EventBus \EventHandler ;
7+
8+ #[Singleton]
9+ final class HandlersForEventWithoutPropagation
10+ {
11+ public int $ count = 0 ;
12+
13+ #[EventHandler]
14+ public function a (EventWithoutPropagation $ event ): void
15+ {
16+ $ this ->count ++;
17+ }
18+
19+ #[EventHandler]
20+ public function b (EventWithoutPropagation $ event ): void
21+ {
22+ $ this ->count ++;
23+ }
24+ }
Original file line number Diff line number Diff line change 66
77use Tests \Tempest \Fixtures \Events \DiscoveredEventBusMiddleware ;
88use Tests \Tempest \Fixtures \Events \EnumEvent ;
9+ use Tests \Tempest \Fixtures \Events \EventForListenerWithoutPropagation ;
910use Tests \Tempest \Fixtures \Events \EventInterfaceImplementation ;
11+ use Tests \Tempest \Fixtures \Events \EventWithoutPropagation ;
12+ use Tests \Tempest \Fixtures \Events \HandlersForEventWithListenerWithoutPropagation ;
13+ use Tests \Tempest \Fixtures \Events \HandlersForEventWithoutPropagation ;
1014use Tests \Tempest \Fixtures \Events \OtherEnumEvent ;
1115use Tests \Tempest \Fixtures \Events \TestEventHandler ;
1216use Tests \Tempest \Fixtures \Handlers \EventInterfaceHandler ;
@@ -63,4 +67,24 @@ public function test_discovered_middleware(): void
6367
6468 $ this ->assertTrue (DiscoveredEventBusMiddleware::$ hit );
6569 }
70+
71+ public function test_event_without_propagation (): void
72+ {
73+ $ handler = $ this ->get (HandlersForEventWithoutPropagation::class);
74+ $ handler ->count = 0 ;
75+
76+ event (new EventWithoutPropagation ());
77+
78+ $ this ->assertSame (1 , $ handler ->count );
79+ }
80+
81+ public function test_listener_without_propagation (): void
82+ {
83+ $ handler = $ this ->get (HandlersForEventWithListenerWithoutPropagation::class);
84+ $ handler ->count = 0 ;
85+
86+ event (new EventForListenerWithoutPropagation ());
87+
88+ $ this ->assertSame (1 , $ handler ->count );
89+ }
6690}
You can’t perform that action at this time.
0 commit comments