Skip to content

Commit

Permalink
Merge commit b3d9511b716c7b8631b59796b83c60767596a6ba into new-master
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed May 15, 2024
1 parent 41feb1a commit 59eac97
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
38 changes: 14 additions & 24 deletions src/Bootloader/EventsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Spiral\Events\Bootloader;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Boot\AbstractKernel;
Expand All @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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));
}

Expand All @@ -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<T>|Autowire<T>|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;
}
}
5 changes: 1 addition & 4 deletions src/Config/EventsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessorInterface>|Autowire<ProcessorInterface>
* @psalm-type TListener = class-string|EventListener
* @psalm-type TLegacyInterceptor = class-string<CoreInterceptorInterface>|CoreInterceptorInterface|Autowire<CoreInterceptorInterface>
* @psalm-type TNewInterceptor = class-string<InterceptorInterface>|InterceptorInterface|Autowire<InterceptorInterface>
* @psalm-type TInterceptor = TLegacyInterceptor|TNewInterceptor
* @psalm-type TInterceptor = class-string<CoreInterceptorInterface>|CoreInterceptorInterface|Autowire<CoreInterceptorInterface>
* @property array{
* processors: TProcessor[],
* listeners: array<class-string, TListener[]>,
Expand Down
14 changes: 2 additions & 12 deletions src/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit 59eac97

Please sign in to comment.