Skip to content

[Messenger] made dispatch() and handle() return void #28909

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
Oct 21, 2018
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 @@ -31,7 +31,7 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
$this->entityManagerName = $entityManagerName;
}

public function handle($message, callable $next)
public function handle($message, callable $next): void
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

Expand All @@ -41,15 +41,13 @@ public function handle($message, callable $next)

$entityManager->getConnection()->beginTransaction();
try {
$result = $next($message);
$next($message);
$entityManager->flush();
$entityManager->getConnection()->commit();
} catch (\Throwable $exception) {
$entityManager->getConnection()->rollBack();

throw $exception;
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,6 @@
{% endfor %}
</td>
</tr>
<tr>
<td class="text-bold">Result</td>
<td>
{% if dispatchCall.result is defined %}
{{ profiler_dump(dispatchCall.result.seek('value'), maxDepth=2) }}
{% elseif dispatchCall.exception is defined %}
<span class="text-danger">No result as an exception occurred</span>
{% endif %}
</td>
</tr>
{% if dispatchCall.exception is defined %}
<tr>
<td class="text-bold">Exception</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
*
* {@inheritdoc}
*/
public function handle($envelope, callable $next)
public function handle($envelope, callable $next): void
{
if ($envelope->get(ReceivedStamp::class)) {
// It's a received message. Do not send it back:
return $next($envelope);
$next($envelope);

return;
}

$sender = $this->senderLocator->getSenderForMessage($envelope->getMessage());
Expand All @@ -55,7 +57,7 @@ public function handle($envelope, callable $next)
}
}

return $next($envelope);
$next($envelope);
}

private function mustSendAndHandle($message): bool
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* The component is not experimental anymore
* All the changes below are BC BREAKS
* `MessageBusInterface::dispatch()` and `MiddlewareInterface::handle()` now return `void`
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
`Serializer` as a second argument.
* `SenderLocator` has been renamed to `ContainerSenderLocator`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ private function collectMessage(string $busName, array $tracedMessage)
'caller' => $tracedMessage['caller'],
);

if (array_key_exists('result', $tracedMessage)) {
$result = $tracedMessage['result'];
$debugRepresentation['result'] = array(
'type' => \is_object($result) ? \get_class($result) : \gettype($result),
'value' => $result,
);
}

if (isset($tracedMessage['exception'])) {
$exception = $tracedMessage['exception'];

Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Messenger/Handler/ChainHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ public function __construct(array $handlers)

public function __invoke($message)
{
$results = array();

foreach ($this->handlers as $handler) {
$results[] = $handler($message);
$handler($message);
}

return $results;
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/Messenger/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function __construct(iterable $middlewareHandlers = array())
/**
* {@inheritdoc}
*/
public function dispatch($message)
public function dispatch($message): void
{
if (!\is_object($message)) {
throw new InvalidArgumentException(sprintf('Invalid type for message argument. Expected object, but got "%s".', \gettype($message)));
}

return \call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message);
\call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message);
}

private function callableForNextMiddleware(int $index, Envelope $currentEnvelope): callable
Expand All @@ -71,7 +71,7 @@ private function callableForNextMiddleware(int $index, Envelope $currentEnvelope
$message = $message->getMessage();
}

return $middleware->handle($message, $this->callableForNextMiddleware($index + 1, $currentEnvelope));
$middleware->handle($message, $this->callableForNextMiddleware($index + 1, $currentEnvelope));
};
}
}
6 changes: 1 addition & 5 deletions src/Symfony/Component/Messenger/MessageBusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ interface MessageBusInterface
/**
* Dispatches the given message.
*
* The bus can return a value coming from handlers, but is not required to do so.
*
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
*
* @return mixed
*/
public function dispatch($message);
public function dispatch($message): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/
class AllowNoHandlerMiddleware implements MiddlewareInterface
{
public function handle($message, callable $next)
public function handle($message, callable $next): void
{
try {
return $next($message);
$next($message);
} catch (NoHandlerForMessageException $e) {
// We allow not having a handler for this message.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
/**
* @param Envelope $envelope
*/
public function handle($envelope, callable $next)
public function handle($envelope, callable $next): void
{
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
return $this->inner->handle($envelope->getMessageFor($this->inner), $next);
$this->inner->handle($envelope->getMessageFor($this->inner), $next);
} else {
$next($envelope);
}

return $next($envelope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(MiddlewareInterface $inner, Stopwatch $stopwatch, st
/**
* @param Envelope $envelope
*/
public function handle($envelope, callable $next)
public function handle($envelope, callable $next): void
{
$class = \get_class($this->inner);
$eventName = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
Expand All @@ -51,19 +51,15 @@ public function handle($envelope, callable $next)
$this->stopwatch->start($eventName, $this->eventCategory);

try {
$result = $this->inner->handle($envelope->getMessageFor($this->inner), function ($message) use ($next, $eventName) {
$this->inner->handle($envelope->getMessageFor($this->inner), function ($message) use ($next, $eventName) {
$this->stopwatch->stop($eventName);
$result = $next($message);
$next($message);
$this->stopwatch->start($eventName, $this->eventCategory);

return $result;
});
} finally {
if ($this->stopwatch->isStarted($eventName)) {
$this->stopwatch->stop($eventName);
}
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ public function __construct(HandlerLocatorInterface $messageHandlerResolver)
/**
* {@inheritdoc}
*/
public function handle($message, callable $next)
public function handle($message, callable $next): void
{
$handler = $this->messageHandlerResolver->resolve($message);
$result = $handler($message);
$handler($message);

$next($message);

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function __construct(LoggerInterface $logger)
/**
* {@inheritdoc}
*/
public function handle($message, callable $next)
public function handle($message, callable $next): void
{
$this->logger->debug('Starting handling message {class}', $this->createContext($message));

try {
$result = $next($message);
$next($message);
} catch (\Throwable $e) {
$this->logger->warning('An exception occurred while handling message {class}', array_merge(
$this->createContext($message),
Expand All @@ -44,8 +44,6 @@ public function handle($message, callable $next)
}

$this->logger->debug('Finished handling message {class}', $this->createContext($message));

return $result;
}

private function createContext($message): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ interface MiddlewareInterface
{
/**
* @param object $message
*
* @return mixed
*/
public function handle($message, callable $next);
public function handle($message, callable $next): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(ValidatorInterface $validator)
/**
* @param Envelope $envelope
*/
public function handle($envelope, callable $next)
public function handle($envelope, callable $next): void
{
$message = $envelope->getMessage();
$groups = null;
Expand All @@ -46,6 +46,6 @@ public function handle($envelope, callable $next)
throw new ValidationFailedException($message, $violations);
}

return $next($envelope);
$next($envelope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ protected function setUp()
$this->dumper->setColors(false);
}

/**
* @dataProvider getHandleTestData
*/
public function testHandle($returnedValue, $expected)
public function testHandle()
{
$message = new DummyMessage('dummy message');

$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
$bus->method('dispatch')->with($message)->willReturn($returnedValue);
$bus->method('dispatch')->with($message);
$bus = new TraceableMessageBus($bus);

$collector = new MessengerDataCollector();
Expand All @@ -54,13 +51,9 @@ public function testHandle($returnedValue, $expected)
$messages = $collector->getMessages();
$this->assertCount(1, $messages);

$this->assertStringMatchesFormat($expected, $this->getDataAsString($messages[0]));
}

public function getHandleTestData()
{
$file = __FILE__;
$messageDump = <<<DUMP
$expected = <<<DUMP
array:4 [
"bus" => "default"
"stamps" => null
"message" => array:2 [
Expand All @@ -74,48 +67,10 @@ public function getHandleTestData()
"file" => "$file"
"line" => %d
]
DUMP;

yield 'no returned value' => array(
null,
<<<DUMP
array:5 [
$messageDump
"result" => array:2 [
"type" => "NULL"
"value" => null
]
]
DUMP
);

yield 'scalar returned value' => array(
'returned value',
<<<DUMP
array:5 [
$messageDump
"result" => array:2 [
"type" => "string"
"value" => "returned value"
]
]
DUMP
);
DUMP;

yield 'array returned value' => array(
array('returned value'),
<<<DUMP
array:5 [
$messageDump
"result" => array:2 [
"type" => "array"
"value" => array:1 [
0 => "returned value"
]
]
]
DUMP
);
$this->assertStringMatchesFormat($expected, $this->getDataAsString($messages[0]));
}

public function testHandleWithException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,8 @@ public function dummyMethodForSomeBus()

class UselessMiddleware implements MiddlewareInterface
{
public function handle($message, callable $next)
public function handle($message, callable $next): void
{
return $next($message);
$next($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class ChainHandlerTest extends TestCase
{
public function testItCallsTheHandlersAndReturnsAllResults()
public function testItCallsTheHandlers()
{
$message = new DummyMessage('Hey');

Expand All @@ -26,19 +26,15 @@ public function testItCallsTheHandlersAndReturnsAllResults()
->expects($this->once())
->method('__invoke')
->with($message)
->willReturn('Hello')
;
$handler2 = $this->createPartialMock(\stdClass::class, array('__invoke'));
$handler2
->expects($this->once())
->method('__invoke')
->with($message)
->willReturn('World')
;

$results = (new ChainHandler(array($handler1, $handler2)))($message);

$this->assertSame(array('Hello', 'World'), $results);
(new ChainHandler(array($handler1, $handler2)))($message);
}

/**
Expand Down
Loading