Skip to content

Commit

Permalink
Merge pull request #1111: [Interceptors] Add PipelineBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jun 26, 2024
1 parent 375e553 commit 286876d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
12 changes: 5 additions & 7 deletions src/Bootloader/EventsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions src/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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;
}

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],
));
}
Expand Down
9 changes: 8 additions & 1 deletion src/Interceptor/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']);
}
}
3 changes: 2 additions & 1 deletion tests/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Core\CoreInterface;
use Spiral\Events\EventDispatcher;

Expand All @@ -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);
Expand Down

0 comments on commit 286876d

Please sign in to comment.