diff --git a/src/Bootloader/EventsBootloader.php b/src/Bootloader/EventsBootloader.php index 8d235f8..e764ce9 100644 --- a/src/Bootloader/EventsBootloader.php +++ b/src/Bootloader/EventsBootloader.php @@ -13,11 +13,11 @@ use Spiral\Bootloader\Attributes\AttributesBootloader; use Spiral\Config\ConfiguratorInterface; use Spiral\Config\Patch\Append; +use Spiral\Core\CompatiblePipelineBuilder; use Spiral\Core\Container; use Spiral\Core\Container\Autowire; use Spiral\Core\CoreInterceptorInterface; use Spiral\Core\FactoryInterface; -use Spiral\Core\InterceptorPipeline; use Spiral\Events\AutowireListenerFactory; use Spiral\Events\Config\EventsConfig; use Spiral\Events\EventDispatcher; @@ -107,15 +107,13 @@ private function initEventDispatcher( Container $container, FactoryInterface $factory ): void { - $pipeline = (new InterceptorPipeline())->withCore($core); - + $builder = new CompatiblePipelineBuilder(); + $list = []; foreach ($config->getInterceptors() as $interceptor) { - $interceptor = $this->autowire($interceptor, $container, $factory); - - \assert($interceptor instanceof CoreInterceptorInterface || $interceptor instanceof InterceptorInterface); - $pipeline->addInterceptor($interceptor); + $list[] = $this->autowire($interceptor, $container, $factory); } + $pipeline = $builder->withInterceptors(...$list)->build($core); $container->removeBinding(EventDispatcherInterface::class); $container->bindSingleton(EventDispatcherInterface::class, new EventDispatcher($pipeline)); } diff --git a/src/EventDispatcher.php b/src/EventDispatcher.php index 47e5b22..fbb3e7f 100644 --- a/src/EventDispatcher.php +++ b/src/EventDispatcher.php @@ -14,7 +14,7 @@ final class EventDispatcher implements EventDispatcherInterface { private readonly bool $isLegacy; public function __construct( - private readonly HandlerInterface|CoreInterface $core + private readonly HandlerInterface|CoreInterface $core, ) { $this->isLegacy = !$core instanceof HandlerInterface; } @@ -22,9 +22,9 @@ public function __construct( public function dispatch(object $event): object { return $this->isLegacy - ? $this->core->callAction($event::class, 'dispatch', ['event' => $event]) + ? $this->core->callAction(EventDispatcherInterface::class, 'dispatch', ['event' => $event]) : $this->core->handle(new CallContext( - Target::fromPair($event, 'dispatch'), + Target::fromPair(EventDispatcherInterface::class, 'dispatch'), ['event' => $event], )); } diff --git a/src/Interceptor/Core.php b/src/Interceptor/Core.php index cf6757c..a65d921 100644 --- a/src/Interceptor/Core.php +++ b/src/Interceptor/Core.php @@ -6,11 +6,13 @@ use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Core\CoreInterface; +use Spiral\Interceptors\Context\CallContext; +use Spiral\Interceptors\HandlerInterface; /** * @psalm-type TParameters = array{event: object} */ -final class Core implements CoreInterface +final class Core implements CoreInterface, HandlerInterface { public function __construct( private readonly EventDispatcherInterface $dispatcher @@ -27,4 +29,9 @@ public function callAction(string $controller, string $action, array $parameters return $this->dispatcher->dispatch($parameters['event']); } + + public function handle(CallContext $context): mixed + { + return $this->dispatcher->dispatch($context->getArguments()['event']); + } } diff --git a/tests/EventDispatcherTest.php b/tests/EventDispatcherTest.php index b3c6814..136e38b 100644 --- a/tests/EventDispatcherTest.php +++ b/tests/EventDispatcherTest.php @@ -6,6 +6,7 @@ use Mockery as m; use PHPUnit\Framework\TestCase; +use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Core\CoreInterface; use Spiral\Events\EventDispatcher; @@ -19,7 +20,7 @@ public function testDispatch(): void $core ->shouldReceive('callAction') ->once() - ->with($event::class, 'dispatch', ['event' => $event]) + ->with(EventDispatcherInterface::class, 'dispatch', ['event' => $event]) ->andReturn($event); $dispatcher = new EventDispatcher($core);