Description
Description
We should drop the Stack concept and reintroduce $next in MiddlewareInterface::handle, like in the good old days:
interface MiddlewareInterface
{
/**
* @param callable|NextInterface $next
*/
public function handle(Envelope $envelope, callable $next): Envelope;
}
/**
* @internal
*/
interface NextInterface
{
public function __invoke(Envelope $envelope): void;
}
Update: New proposal on #31185
My case agains the Stack is:
- It makes middlewares harder to test
Instead of calling our Middlewares with a simple assertion function we must create a Stack.
When trying to use a simple Stack spy we have to mock both the next() and handle() methods
With the Stack:
$stackSpy = $this->prophesize(StackInterface::Class);
$stackSpy->next()->shouldBeCalledOnce()->willReturn($stackSpy);
$stackSpy->handle($envelope, $stackSpy)->shouldBeCalledOnce()->willReturn($envelope);
$myMiddleware->handle($envelope, $stackSpy->reveal());
Without the stack:
$nextSpy = $this->prophesize(NextInterface::Class);
$nextSpy->__invoke($envelope)->shouldBeCalledOnce()->willReturn($envelope);
$myMiddleware->handle($nextSpy->reveal());
- It introduces non-determinism in the bus
Because of the iterator that lives inside the stack we cannot use middlewares to control the bus flow without getting into weird behaviours.
For instance, if not for the stack, the Retry mechanism (#30557, #27215) could be implemented as a Middleware in a trivial way, with the added benefit of being able to retry from specific positions in the pipeline:
public function handle(Envelope $envelope, callable $next) : Envelope
{
while ($this->tries-- > 0) {
try {
return $next($envelope);
} catch (\Throwable $t) {
}
}
throw $t;
}
If we try this with the Stack it's internal iterator will have advanced, making the middlewares pipeline non-reproducible.
- It makes middlewares more verbose
$stack->next()->handle($envelope, $stack);
vs
$next($envelope);