diff --git a/src/Bootloader/EventsBootloader.php b/src/Bootloader/EventsBootloader.php index 8d235f8..bfaacce 100644 --- a/src/Bootloader/EventsBootloader.php +++ b/src/Bootloader/EventsBootloader.php @@ -4,7 +4,6 @@ namespace Spiral\Events\Bootloader; -use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Boot\AbstractKernel; @@ -17,7 +16,7 @@ use Spiral\Core\Container\Autowire; use Spiral\Core\CoreInterceptorInterface; use Spiral\Core\FactoryInterface; -use Spiral\Core\InterceptorPipeline; +use Spiral\Core\InterceptableCore; use Spiral\Events\AutowireListenerFactory; use Spiral\Events\Config\EventsConfig; use Spiral\Events\EventDispatcher; @@ -28,7 +27,6 @@ use Spiral\Events\Processor\AttributeProcessor; use Spiral\Events\Processor\ConfigProcessor; use Spiral\Events\Processor\ProcessorInterface; -use Spiral\Interceptors\InterceptorInterface; use Spiral\Tokenizer\Bootloader\TokenizerListenerBootloader; /** @@ -95,9 +93,8 @@ public function boot( /** * @param TInterceptor $interceptor */ - public function addInterceptor( - string|InterceptorInterface|CoreInterceptorInterface|Container\Autowire $interceptor, - ): void { + public function addInterceptor(string|CoreInterceptorInterface|Container\Autowire $interceptor): void + { $this->configs->modify(EventsConfig::CONFIG, new Append('interceptors', null, $interceptor)); } @@ -107,34 +104,27 @@ private function initEventDispatcher( Container $container, FactoryInterface $factory ): void { - $pipeline = (new InterceptorPipeline())->withCore($core); + $core = new InterceptableCore($core); foreach ($config->getInterceptors() as $interceptor) { $interceptor = $this->autowire($interceptor, $container, $factory); - \assert($interceptor instanceof CoreInterceptorInterface || $interceptor instanceof InterceptorInterface); - $pipeline->addInterceptor($interceptor); + \assert($interceptor instanceof CoreInterceptorInterface); + $core->addInterceptor($interceptor); } $container->removeBinding(EventDispatcherInterface::class); - $container->bindSingleton(EventDispatcherInterface::class, new EventDispatcher($pipeline)); + $container->bindSingleton(EventDispatcherInterface::class, new EventDispatcher($core)); } - /** - * @template T of object - * - * @param class-string|Autowire|T $id - * - * @return T - * - * @throws ContainerExceptionInterface - */ private function autowire(string|object $id, ContainerInterface $container, FactoryInterface $factory): object { - return match (true) { - \is_string($id) => $container->get($id), - $id instanceof Autowire => $id->resolve($factory), - default => $id, - }; + if (\is_string($id)) { + $id = $container->get($id); + } elseif ($id instanceof Autowire) { + $id = $id->resolve($factory); + } + + return $id; } } diff --git a/src/Config/EventsConfig.php b/src/Config/EventsConfig.php index 0e196f2..897fcbc 100644 --- a/src/Config/EventsConfig.php +++ b/src/Config/EventsConfig.php @@ -8,14 +8,11 @@ use Spiral\Core\CoreInterceptorInterface; use Spiral\Core\InjectableConfig; use Spiral\Events\Processor\ProcessorInterface; -use Spiral\Interceptors\InterceptorInterface; /** * @psalm-type TProcessor = ProcessorInterface|class-string|Autowire * @psalm-type TListener = class-string|EventListener - * @psalm-type TLegacyInterceptor = class-string|CoreInterceptorInterface|Autowire - * @psalm-type TNewInterceptor = class-string|InterceptorInterface|Autowire - * @psalm-type TInterceptor = TLegacyInterceptor|TNewInterceptor + * @psalm-type TInterceptor = class-string|CoreInterceptorInterface|Autowire * @property array{ * processors: TProcessor[], * listeners: array, diff --git a/src/EventDispatcher.php b/src/EventDispatcher.php index 6768096..03ea261 100644 --- a/src/EventDispatcher.php +++ b/src/EventDispatcher.php @@ -6,26 +6,16 @@ use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Core\CoreInterface; -use Spiral\Interceptors\Context\CallContext; -use Spiral\Interceptors\Context\Target; -use Spiral\Interceptors\HandlerInterface; final class EventDispatcher implements EventDispatcherInterface { - private readonly bool $isLegacy; public function __construct( - private readonly HandlerInterface|CoreInterface $core + private readonly CoreInterface $core ) { - $this->isLegacy = !$core instanceof HandlerInterface; } public function dispatch(object $event): object { - return $this->isLegacy - ? $this->core->callAction($event::class, 'dispatch', ['event' => $event]) - : $this->core->handle(new CallContext( - Target::fromReflection(new \ReflectionMethod($event::class, 'dispatch')), - ['event' => $event], - )); + return $this->core->callAction($event::class, 'dispatch', ['event' => $event]); } }