-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] ease testing and allow forking the middleware stack #31204
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,31 +20,74 @@ | |
*/ | ||
class StackMiddleware implements MiddlewareInterface, StackInterface | ||
{ | ||
private $middlewareIterator; | ||
private $stack; | ||
private $offset = 0; | ||
|
||
public function __construct(\Iterator $middlewareIterator = null) | ||
/** | ||
* @param iterable|MiddlewareInterface[]|MiddlewareInterface|null $middlewareIterator | ||
*/ | ||
public function __construct($middlewareIterator = null) | ||
{ | ||
$this->middlewareIterator = $middlewareIterator; | ||
$this->stack = new MiddlewareStack(); | ||
|
||
if (null === $middlewareIterator) { | ||
return; | ||
} | ||
|
||
if ($middlewareIterator instanceof \Iterator) { | ||
$this->stack->iterator = $middlewareIterator; | ||
} elseif ($middlewareIterator instanceof MiddlewareInterface) { | ||
$this->stack->stack[] = $middlewareIterator; | ||
} elseif (!\is_iterable($middlewareIterator)) { | ||
throw new \TypeError(sprintf('Argument 1 passed to %s() must be iterable of %s, %s given.', __METHOD__, MiddlewareInterface::class, \is_object($middlewareIterator) ? \get_class($middlewareIterator) : \gettype($middlewareIterator))); | ||
} else { | ||
$this->stack->iterator = (function () use ($middlewareIterator) { | ||
yield from $middlewareIterator; | ||
})(); | ||
} | ||
} | ||
|
||
public function next(): MiddlewareInterface | ||
{ | ||
if (null === $iterator = $this->middlewareIterator) { | ||
if (null === $next = $this->stack->next($this->offset)) { | ||
return $this; | ||
} | ||
$iterator->next(); | ||
|
||
if (!$iterator->valid()) { | ||
$this->middlewareIterator = null; | ||
++$this->offset; | ||
|
||
return $this; | ||
} | ||
|
||
return $iterator->current(); | ||
return $next; | ||
} | ||
|
||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
return $envelope; | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class MiddlewareStack | ||
{ | ||
public $iterator; | ||
public $stack = []; | ||
|
||
public function next(int $offset): ?MiddlewareInterface | ||
{ | ||
if (isset($this->stack[$offset])) { | ||
return $this->stack[$offset]; | ||
} | ||
|
||
if (null === $this->iterator) { | ||
return null; | ||
} | ||
|
||
$this->iterator->next(); | ||
|
||
if (!$this->iterator->valid()) { | ||
return $this->iterator = null; | ||
} | ||
|
||
return $this->stack[] = $this->iterator->current(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to stare at this for a minute. When you clone the |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Middleware; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBus; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
|
||
class StackMiddlewareTest extends TestCase | ||
{ | ||
public function testClone() | ||
{ | ||
$middleware1 = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); | ||
$middleware1 | ||
->expects($this->once()) | ||
->method('handle') | ||
->willReturnCallback(function (Envelope $envelope, StackInterface $stack): Envelope { | ||
$fork = clone $stack; | ||
|
||
$stack->next()->handle($envelope, $stack); | ||
$fork->next()->handle($envelope, $fork); | ||
|
||
return $envelope; | ||
}) | ||
; | ||
|
||
$middleware2 = $this->getMockBuilder(MiddlewareInterface::class)->getMock(); | ||
$middleware2 | ||
->expects($this->exactly(2)) | ||
->method('handle') | ||
->willReturnCallback(function (Envelope $envelope, StackInterface $stack): Envelope { | ||
return $envelope; | ||
}) | ||
; | ||
|
||
$bus = new MessageBus([$middleware1, $middleware2]); | ||
|
||
$bus->dispatch(new \stdClass()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you simply drop the iterator, store middleware as array in
StackMiddleware
and advance the pointer manually?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would defeat laziness, which is a desired property.