Skip to content

[Messenger] Introduce HandlerDescriptorInterface #50998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Symfony\Component\Messenger\EventListener\StopWorkerOnCustomStopExceptionListener;
use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
use Symfony\Component\Messenger\EventListener\StopWorkerOnSignalsListener;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlersLocator;
use Symfony\Component\Messenger\Handler\RedispatchMessageHandler;
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware;
Expand All @@ -46,6 +48,9 @@
use Symfony\Component\Messenger\Transport\TransportFactory;

return static function (ContainerConfigurator $container) {
$container->parameters()
->set('.messenger.handler_descriptor_class', HandlerDescriptor::class);

$container->services()
->alias(SerializerInterface::class, 'messenger.default_serializer')

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Introduce `HandlerDescriptorInterface`

6.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlerDescriptorInterface;
use Symfony\Component\Messenger\Handler\HandlersLocator;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
Expand Down Expand Up @@ -63,6 +64,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds): v
$definitions = [];
$handlersByBusAndMessage = [];
$handlerToOriginalServiceIdMapping = [];
$handlerDescriptorClass = $container->getParameter('.messenger.handler_descriptor_class') ?? HandlerDescriptor::class;

foreach ($container->findTaggedServiceIds('messenger.message_handler', true) as $serviceId => $tags) {
foreach ($tags as $tag) {
Expand Down Expand Up @@ -164,7 +166,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds): v
foreach ($handlersByMessage as $message => $handlers) {
$handlerDescriptors = [];
foreach ($handlers as $handler) {
$definitions[$definitionId = '.messenger.handler_descriptor.'.ContainerBuilder::hash($bus.':'.$message.':'.$handler[0])] = (new Definition(HandlerDescriptor::class))->setArguments([new Reference($handler[0]), $handler[1]]);
$definitions[$definitionId = '.messenger.handler_descriptor.'.ContainerBuilder::hash($bus.':'.$message.':'.$handler[0])] = (new Definition($handlerDescriptorClass))->setArguments([new Reference($handler[0]), $handler[1]]);
$handlerDescriptors[] = new Reference($definitionId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
namespace Symfony\Component\Messenger\Handler;

/**
* Describes a handler and the possible associated options, such as `from_transport`, `bus`, etc.
*
* @author Samuel Roze <samuel.roze@gmail.com>
*/
final class HandlerDescriptor
final class HandlerDescriptor implements HandlerDescriptorInterface
{
private \Closure $handler;
private string $name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\Messenger\Handler;

/**
* Describes a handler and the possible associated options, such as `from_transport`, `bus`, etc.
*
* @author Samuel Roze <samuel.roze@gmail.com>
* @author Ruud Kamphuis <ruud@ticketswap.com>
*/
interface HandlerDescriptorInterface
{
public function getHandler() : callable;

public function getName() : string;

public function getBatchHandler() : ?BatchHandlerInterface;

public function getOption(string $option) : mixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface HandlersLocatorInterface
/**
* Returns the handlers for the given message name.
*
* @return iterable<int, HandlerDescriptor>
* @return iterable<int, HandlerDescriptorInterface>
*/
public function getHandlers(Envelope $envelope): iterable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
use Symfony\Component\Messenger\Handler\Acknowledger;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlerDescriptorInterface;
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface;
use Symfony\Component\Messenger\Stamp\AckStamp;
use Symfony\Component\Messenger\Stamp\FlushBatchHandlersStamp;
Expand Down Expand Up @@ -132,7 +132,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
return $stack->next()->handle($envelope, $stack);
}

private function messageHasAlreadyBeenHandled(Envelope $envelope, HandlerDescriptor $handlerDescriptor): bool
private function messageHasAlreadyBeenHandled(Envelope $envelope, HandlerDescriptorInterface $handlerDescriptor): bool
{
/** @var HandledStamp $stamp */
foreach ($envelope->all(HandledStamp::class) as $stamp) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Messenger/Stamp/HandledStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Messenger\Stamp;

use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlerDescriptorInterface;

/**
* Stamp identifying a message handled by the `HandleMessageMiddleware` middleware
Expand All @@ -36,7 +36,7 @@ public function __construct(mixed $result, string $handlerName)
$this->handlerName = $handlerName;
}

public static function fromDescriptor(HandlerDescriptor $handler, mixed $result): self
public static function fromDescriptor(HandlerDescriptorInterface $handler, mixed $result): self
{
return new self($result, $handler->getName());
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Messenger/Stamp/NoAutoAckStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Messenger\Stamp;

use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlerDescriptorInterface;

/**
* Marker telling that ack should not be done automatically for this message.
Expand All @@ -20,12 +20,12 @@ final class NoAutoAckStamp implements NonSendableStampInterface
{
private $handlerDescriptor;

public function __construct(HandlerDescriptor $handlerDescriptor)
public function __construct(HandlerDescriptorInterface $handlerDescriptor)
{
$this->handlerDescriptor = $handlerDescriptor;
}

public function getHandlerDescriptor(): HandlerDescriptor
public function getHandlerDescriptor(): HandlerDescriptorInterface
{
return $this->handlerDescriptor;
}
Expand Down