8000 [Messenger] make middlewares truly lazy on a bus by nicolas-grekas · Pull Request #28907 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] make middlewares truly lazy on a bus #28907

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 < 8000 /div>
Diff view
Diff view
53 changes: 33 additions & 20 deletions src/Symfony/Component/Messenger/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,39 @@

namespace Symfony\Component\Messenger;

use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
* @author Matthias Noback <matthiasnoback@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
class MessageBus implements MessageBusInterface
{
private $middlewareHandlers;

/**
* @var MiddlewareInterface[]|null
*/
private $indexedMiddlewareHandlers;
private $middlewareAggregate;

/**
* @param MiddlewareInterface[]|iterable $middlewareHandlers
*/
public function __construct(iterable $middlewareHandlers = array())
{
$this->middlewareHandlers = $middlewareHandlers;
if ($middlewareHandlers instanceof \IteratorAggregate) {
$this->middlewareAggregate = $middlewareHandlers;
} elseif (\is_array($middlewareHandlers)) {
$this->middlewareAggregate = new \ArrayObject($middlewareHandlers);
} else {
$this->middlewareAggregate = new class() {
public $aggregate;
public $iterator;

public function getIterator()
{
return $this->aggregate = new \ArrayObject(iterator_to_array($this->iterator, false));
}
};
$this->middlewareAggregate->aggregate = &$this->middlewareAggregate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move that to be in the constructor of your class instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class is internal, the public properties are not accessible. Adding a constructor would just add unneeded boilerplate. Better not to me.

$this->middlewareAggregate->iterator = $middlewareHandlers;
}
}

/**
Expand All @@ -41,24 +52,26 @@ public function __construct(iterable $middlewareHandlers = array())
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)));
throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object, but got %s.', __METHOD__, \gettype($message)));
}
$middlewareIterator = $this->middlewareAggregate->getIterator();

$this->callableForNextMiddleware(0)($message instanceof Envelope ? $message : new Envelope($message));
}

private function callableForNextMiddleware(int $index): callable
{
if (null === $this->indexedMiddlewareHandlers) {
$this->indexedMiddlewareHandlers = \is_array($this->middlewareHandlers) ? array_values($this->middlewareHandlers) : iterator_to_array($this->middlewareHandlers, false);
while ($middlewareIterator instanceof \IteratorAggregate) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why while? What's the use-case in which it will return an iterator twice (or more)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sroze i suggested that, see #28907 (comment)

Copy link
Contributor
@ro0NL ro0NL Oct 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but maybe we should enforce it resolves once, either here or in the constructor 🤔

Copy link
Member Author
@nicolas-grekas nicolas-grekas Oct 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how "foreach" works internally. We must have it to preserve basic PHP semantics of Traversable.

$middlewareIterator = $middlewareIterator->getIterator();
}
$middlewareIterator->rewind();

if (!isset($this->indexedMiddlewareHandlers[$index])) {
return static function () {};
if (!$middlewareIterator->valid()) {
return;
}
$next = static function (Envelope $envelope) use ($middlewareIterator, &$next) {
$middlewareIterator->next();

return function (Envelope $envelope) use ($index) {
$this->indexedMiddlewareHandlers[$index]->handle($envelope, $this->callableForNextMiddleware($index + 1));
if ($middlewareIterator->valid()) {
$middlewareIterator->current()->handle($envelope, $next);
}
};

$middlewareIterator->current()->handle($message instanceof Envelope ? $message : new Envelope($message), $next);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public function testProcessHandlersByBus()
->setAbstract(true)
;

$middlewares = array(array('id' => 'call_message_handler'));
$middlewareHandlers = array(array('id' => 'call_message_handler'));

$container->setParameter($commandBusId.'.middleware', $middlewares);
$container->setParameter($queryBusId.'.middleware', $middlewares);
$container->setParameter($commandBusId.'.middleware', $middlewareHandlers);
$container->setParameter($queryBusId.'.middleware', $middlewareHandlers);

$container->register(DummyCommandHandler::class)->addTag('messenger.message_handler', array('bus' => $commandBusId));
$container->register(DummyQueryHandler::class)->addTag('messenger.message_handler', array('bus' => $queryBusId));
Expand Down Expand Up @@ -607,10 +607,10 @@ public function testItRegistersTheDebugCommand()

$container->register('console.command.messenger_debug', DebugCommand::class)->addArgument(array());

$middlewares = array(array('id' => 'call_message_handler'));
$middlewareHandlers = array(array('id' => 'call_message_handler'));

$container->setParameter($commandBusId.'.middleware', $middlewares);
$container->setParameter($queryBusId.'.middleware', $middlewares);
$container->setParameter($commandBusId.'.middleware', $middlewareHandlers);
$container->setParameter($queryBusId.'.middleware', $middlewareHandlers);

$container->register(DummyCommandHandler::class)->addTag('messenger.message_handler', array('bus' => $commandBusId));
$container->register(DummyQueryHandler::class)->addTag('messenger.message_handler', array('bus' => $queryBusId));
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Messenger/Tests/MessageBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function testItHasTheRightInterface()
}

/**
* @expectedException \Symfony\Component\Messenger\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid type for message argument. Expected object, but got "string".
* @expectedException \TypeError
* @expectedExceptionMessage Invalid argument provided to "Symfony\Component\Messenger\MessageBus::dispatch()": expected object, but got string.
*/
public function testItDispatchInvalidMessageType()
{
Expand Down
0