Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Add HandlerArgumentsStamp #45418

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

Merged
merged 1 commit into from
Aug 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Messenger\Stamp\AckStamp;
use Symfony\Component\Messenger\Stamp\FlushBatchHandlersStamp;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
use Symfony\Component\Messenger\Stamp\NoAutoAckStamp;

/**
Expand Down Expand Up @@ -78,7 +79,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$ackStamp->ack($envelope, $e);
});

$result = $handler($message, $ack);
$result = $this->callHandler($handler, $message, $ack, $envelope->last(HandlerArgumentsStamp::class));

if (!\is_int($result) || 0 > $result) {
throw new LogicException(sprintf('A handler implementing BatchHandlerInterface must return the size of the current batch as a positive integer, "%s" returned from "%s".', \is_int($result) ? $result : get_debug_type($result), get_debug_type($batchHandler)));
Expand All @@ -92,7 +93,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$result = $ack->getResult();
}
} else {
$result = $handler($message);
$result = $this->callHandler($handler, $message, null, $envelope->last(HandlerArgumentsStamp::class));
}

$handledStamp = HandledStamp::fromDescriptor($handlerDescriptor, $result);
Expand Down Expand Up @@ -142,4 +143,17 @@ private function messageHasAlreadyBeenHandled(Envelope $envelope, HandlerDescrip

return false;
}

private function callHandler(callable $handler, object $message, ?Acknowledger $ack, ?HandlerArgumentsStamp $handlerArgumentsStamp): mixed
{
$arguments = [$message];
if (null !== $ack) {
$arguments[] = $ack;
}
if (null !== $handlerArgumentsStamp) {
$arguments = [...$arguments, ...$handlerArgumentsStamp->getAdditionalArguments()];
}

return $handler(...$arguments);
}
}
28 changes: 28 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/HandlerArgumentsStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Stamp;

/**
* @author Jáchym Toušek <[email protected]>
*/
final class HandlerArgumentsStamp implements NonSendableStampInterface
{
public function __construct(
private array $additionalArguments,
) {
}

public function getAdditionalArguments()
{
return $this->additionalArguments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Messenger\Middleware\StackMiddleware;
use Symfony\Component\Messenger\Stamp\AckStamp;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
use Symfony\Component\Messenger\Stamp\NoAutoAckStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
Expand Down Expand Up @@ -261,6 +262,40 @@ private function process(array $jobs): void

$this->assertSame([$message], $handler->processedMessages);
}

public function testHandlerArgumentsStamp()
{
$message = new DummyMessage('Hey');
$envelope = new Envelope($message);
$envelope = $envelope->with(new HandlerArgumentsStamp(['additional argument']));

$handler = $this->createPartialMock(HandleMessageMiddlewareTestCallable::class, ['__invoke']);

$middleware = new HandleMessageMiddleware(new HandlersLocator([
DummyMessage::class => [$handler],
]));

$handler->expects($this->once())->method('__invoke')->with($message, 'additional argument');

$middleware->handle($envelope, $this->getStackMock());
}

public function testHandlerArgumentsStampNamedArgument()
{
$message = new DummyMessage('Hey');
$envelope = new Envelope($message);
$envelope = $envelope->with(new HandlerArgumentsStamp(['namedArgument' => 'additional named argument']));

$handler = $this->createPartialMock(HandleMessageMiddlewareNamedArgumentTestCallable::class, ['__invoke']);

$middleware = new HandleMessageMiddleware(new HandlersLocator([
DummyMessage::class => [$handler],
]));

$handler->expects($this->once())->method('__invoke')->with($message, 'additional named argument');

$middleware->handle($envelope, $this->getStackMock());
}
}

class HandleMessageMiddlewareTestCallable
Expand All @@ -269,3 +304,10 @@ public function __invoke()
{
}
}

class HandleMessageMiddlewareNamedArgumentTestCallable
{
public function __invoke(object $message, $namedArgument)
{
}
}