Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Messenger/Kernel/AutowiredHandlerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,35 @@

namespace WonderNetwork\SlimKernel\Messenger\Kernel;

use Psr\Container\ContainerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface;
use function WonderNetwork\SlimKernel\Collection\collection;

final readonly class AutowiredHandlerLocator implements HandlersLocatorInterface {
/**
* @var array<class-string, callable>
* @var array<class-string, class-string>
*/
private array $handlers;

/**
* @param array<callable> $handlers
*/
public function __construct(array $handlers) {
public function __construct(private ContainerInterface $container, array $handlers) {
$this->handlers = collection($handlers)->indexBy(new HandlerToMessageMapping())->toArray();
}

public function getHandlers(Envelope $envelope): iterable {
$class = $envelope->getMessage()::class;
$handler = $this->handlers[$class] ?? null;
$handlerClass = $this->handlers[$class] ?? null;

if ($handler) {
yield new HandlerDescriptor($handler);
if ($handlerClass) {
$handler = $this->container->get($handlerClass);

if (is_callable($handler)) {
yield new HandlerDescriptor($handler);
}
}
}
}
16 changes: 8 additions & 8 deletions src/Messenger/Kernel/HandlerToMessageMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace WonderNetwork\SlimKernel\Messenger\Kernel;

use ReflectionClass;
use ReflectionException;
use ReflectionIntersectionType;
use ReflectionNamedType;
use ReflectionObject;
use ReflectionUnionType;
use RuntimeException;

final readonly class HandlerToMessageMapping {
/**
* @throws ReflectionException
*/
public function __invoke(mixed $handler): string {
if (false === is_object($handler)) {
public function __invoke(string $handler): string {
if (false === class_exists($handler)) {
throw new RuntimeException(
sprintf(
'Handler must be an object, %s given',
Expand All @@ -25,13 +25,13 @@ public function __invoke(mixed $handler): string {
);
}

$reflectionObject = new ReflectionObject($handler);
$reflectionObject = new ReflectionClass($handler);

if (false === $reflectionObject->hasMethod('__invoke')) {
throw new RuntimeException(
sprintf(
'Handler %s does not have an __invoke method',
$handler::class,
$handler,
),
);
}
Expand All @@ -42,7 +42,7 @@ public function __invoke(mixed $handler): string {
throw new RuntimeException(
sprintf(
'Handler %s::__invoke() is expected to have exactly one parameter, actual: %d',
$handler::class,
$handler,
$reflectionMethod->getNumberOfParameters(),
),
);
Expand All @@ -52,11 +52,11 @@ public function __invoke(mixed $handler): string {

return match (true) {
$type instanceof ReflectionNamedType => $type->getName(),
$type instanceof ReflectionUnionType => $this->handleUnionTypes($handler::class, ...$type->getTypes()),
$type instanceof ReflectionUnionType => $this->handleUnionTypes($handler, ...$type->getTypes()),
default => throw new RuntimeException(
sprintf(
'Handler %s::__invoke($message) is not properly typehinted',
$handler::class,
$handler,
),
),
};
Expand Down
8 changes: 4 additions & 4 deletions src/Messenger/Kernel/MessengerServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function __invoke(ServicesBuilder $builder): iterable {
yield from $commands = $builder->autowire()->glob($this->commandPath);
yield from $queries = $builder->autowire()->glob($this->queryPath);
yield AutowiredHandlerLocator::class => autowire()
->constructor([
...collection($commands)->keys()->map(static fn (string $className) => get($className)),
...collection($queries)->keys()->map(static fn (string $className) => get($className)),
]);
->constructor(
get(ContainerInterface::class),
collection($commands)->concat($queries)->keys()->toArray(),
);
yield HandlersLocatorInterface::class => get(AutowiredHandlerLocator::class);
yield HandleMessageMiddleware::class => autowire()->constructor(
handlersLocator: get(HandlersLocatorInterface::class),
Expand Down
23 changes: 19 additions & 4 deletions tests/Messenger/MessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace WonderNetwork\SlimKernel\Messenger;

use Acme\SideEffectsCommand;
use Acme\StateQuery;
use Acme\Sample\SideEffectsCommand;
use Acme\Sample\StateQuery;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
Expand All @@ -32,8 +32,8 @@ public function testMessenger(): void {
->useCache(__DIR__.'/../../.cache/')
->register(
new MessengerServiceFactory(
commandPath: 'src/*AsyncHandler.php',
queryPath: 'src/*QueryHandler.php',
commandPath: 'src/Sample/*AsyncHandler.php',
queryPath: 'src/Sample/*QueryHandler.php',
transports: TransportLocatorBuilder::start()
->withTransport(
name: $transportName,
Expand Down Expand Up @@ -66,4 +66,19 @@ public function testMessenger(): void {

self::assertSame($some, $queryBus->query(new StateQuery()));
}

public function testHandlersCanDependOnCommandBus(): void {
$root = realpath(__DIR__.'/../Resources/Messenger') ?: throw new RuntimeException('Oops');
$container = KernelBuilder::start($root)
->register(
new MessengerServiceFactory(
commandPath: 'src/Requeue/*Handler.php',
queryPath: 'src/Requeue/*QueryHandler.php',
),
)
->build();

$this->expectNotToPerformAssertions();
$container->get(CommandBus::class);
}
}
10 changes: 10 additions & 0 deletions tests/Resources/Messenger/src/Requeue/RetryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Acme\Requeue;

use WonderNetwork\SlimKernel\Messenger\AsyncCommand;

final readonly class RetryCommand implements AsyncCommand {
}
26 changes: 26 additions & 0 deletions tests/Resources/Messenger/src/Requeue/RetryCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Acme\Requeue;

use WonderNetwork\SlimKernel\Messenger\AsyncCommand;
use WonderNetwork\SlimKernel\Messenger\AsyncCommandHandler;
use WonderNetwork\SlimKernel\Messenger\CommandBus;
use WonderNetwork\SlimKernel\Messenger\Delay;

/**
* @implements AsyncCommandHandler<RetryCommand>
*/
final readonly class RetryCommandHandler implements AsyncCommandHandler {
public function __construct(private CommandBus $commandBus) {
}

public function __invoke(RetryCommand|AsyncCommand $command): void {
$this->commandBus->delay(
command: $command,
transport: 'some',
delay: Delay::ofHours(1),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acme;
namespace Acme\Sample;

use WonderNetwork\SlimKernel\Messenger\AsyncCommand;
use WonderNetwork\SlimKernel\Messenger\AsyncCommandHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acme;
namespace Acme\Sample;

use WonderNetwork\SlimKernel\Messenger\AsyncCommand;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acme;
namespace Acme\Sample;

use WonderNetwork\SlimKernel\Messenger\Query;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Acme;
namespace Acme\Sample;

use WonderNetwork\SlimKernel\Messenger\HoldsState;
use WonderNetwork\SlimKernel\Messenger\Query;
Expand Down