Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit09eff3d

Browse files
[Messenger] AddStackInterface, allowing to unstack the call stack
1 parent5ae0e89 commit09eff3d

21 files changed

+231
-155
lines changed

‎src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useDoctrine\ORM\EntityManagerInterface;
1616
useSymfony\Component\Messenger\Envelope;
1717
useSymfony\Component\Messenger\Middleware\MiddlewareInterface;
18+
useSymfony\Component\Messenger\Middleware\StackInterface;
1819

1920
/**
2021
* Wraps all handlers in a single doctrine transaction.
@@ -35,7 +36,7 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
3536
/**
3637
* {@inheritdoc}
3738
*/
38-
publicfunctionhandle(Envelope$envelope,callable$next):void
39+
publicfunctionhandle(Envelope$envelope,StackInterface$stack):void
3940
{
4041
$entityManager =$this->managerRegistry->getManager($this->entityManagerName);
4142

@@ -45,7 +46,7 @@ public function handle(Envelope $envelope, callable $next): void
4546

4647
$entityManager->getConnection()->beginTransaction();
4748
try {
48-
$next($envelope);
49+
$stack->next()->handle($envelope,$stack);
4950
$entityManager->flush();
5051
$entityManager->getConnection()->commit();
5152
}catch (\Throwable$exception) {

‎src/Symfony/Component/Messenger/CHANGELOG.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* The component is not experimental anymore
88
* All the changes below are BC BREAKS
99
*`MessageBusInterface::dispatch()` and`MiddlewareInterface::handle()` now return`void`
10-
*`MiddlewareInterface::handle()` now require an`Envelope` as first argument
10+
*`MiddlewareInterface::handle()` now require an`Envelope` as first argument and a`StackInterface` as second
1111
*`EnvelopeAwareInterface` has been removed
1212
* The signature of`Amqp*` classes changed to take a`Connection` as a first argument and an optional
1313
`Serializer` as a second argument.
@@ -16,7 +16,6 @@ CHANGELOG
1616
Instead, it accepts the sender instance itself instead of its identifier in the container.
1717
*`MessageSubscriberInterface::getHandledMessages()` return value has changed. The value of an array item
1818
needs to be an associative array or the method name.
19-
*`ValidationMiddleware::handle()` and`SendMessageMiddleware::handle()` now require an`Envelope` object
2019
*`StampInterface` replaces`EnvelopeItemInterface` and doesn't extend`Serializable` anymore
2120
* The`ConsumeMessagesCommand` class now takes an instance of`Psr\Container\ContainerInterface`
2221
as first constructor argument

‎src/Symfony/Component/Messenger/MessageBus.php‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Messenger;
1313

1414
useSymfony\Component\Messenger\Middleware\MiddlewareInterface;
15+
useSymfony\Component\Messenger\Middleware\StackMiddleware;
1516

1617
/**
1718
* @author Samuel Roze <samuel.roze@gmail.com>
@@ -64,14 +65,8 @@ public function dispatch($message): void
6465
if (!$middlewareIterator->valid()) {
6566
return;
6667
}
67-
$next =staticfunction (Envelope$envelope)use ($middlewareIterator, &$next) {
68-
$middlewareIterator->next();
68+
$stack =newStackMiddleware($middlewareIterator);
6969

70-
if ($middlewareIterator->valid()) {
71-
$middlewareIterator->current()->handle($envelope,$next);
72-
}
73-
};
74-
75-
$middlewareIterator->current()->handle($messageinstanceof Envelope ?$message :newEnvelope($message),$next);
70+
$middlewareIterator->current()->handle($messageinstanceof Envelope ?$message :newEnvelope($message),$stack);
7671
}
7772
}

‎src/Symfony/Component/Messenger/Middleware/ActivationMiddleware.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
3535
/**
3636
* {@inheritdoc}
3737
*/
38-
publicfunctionhandle(Envelope$envelope,callable$next):void
38+
publicfunctionhandle(Envelope$envelope,StackInterface$stack):void
3939
{
4040
if (\is_callable($this->activated) ? ($this->activated)($envelope) :$this->activated) {
41-
$this->inner->handle($envelope,$next);
41+
$this->inner->handle($envelope,$stack);
4242
}else {
43-
$next($envelope);
43+
$stack->next()->handle($envelope,$stack);
4444
}
4545
}
4646
}

‎src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function __construct(HandlerLocatorInterface $messageHandlerLocator, bool
3434
*
3535
* @throws NoHandlerForMessageException When no handler is found and $allowNoHandlers is false
3636
*/
37-
publicfunctionhandle(Envelope$envelope,callable$next):void
37+
publicfunctionhandle(Envelope$envelope,StackInterface$stack):void
3838
{
3939
if (null !==$handler =$this->messageHandlerLocator->getHandler($envelope)) {
4040
$handler($envelope->getMessage());
41-
$next($envelope);
41+
$stack->next()->handle($envelope,$stack);
4242
}elseif (!$this->allowNoHandlers) {
4343
thrownewNoHandlerForMessageException(sprintf('No handler for message "%s".',\get_class($envelope->getMessage())));
4444
}

‎src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(LoggerInterface $logger)
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
publicfunctionhandle(Envelope$envelope,callable$next):void
32+
publicfunctionhandle(Envelope$envelope,StackInterface$stack):void
3333
{
3434
$message =$envelope->getMessage();
3535
$context =array(
@@ -39,7 +39,7 @@ public function handle(Envelope $envelope, callable $next): void
3939
$this->logger->debug('Starting handling message {name}',$context);
4040

4141
try {
42-
$next($envelope);
42+
$stack->next()->handle($envelope,$stack);
4343
}catch (\Throwable$e) {
4444
$context['exception'] =$e;
4545
$this->logger->warning('An exception occurred while handling message {name}',$context);

‎src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,5 @@
1818
*/
1919
interface MiddlewareInterface
2020
{
21-
/**
22-
* @param callable|NextInterface $next
23-
*/
24-
publicfunctionhandle(Envelope$envelope,callable$next):void;
25-
}
26-
27-
/**
28-
* @internal
29-
*/
30-
interface NextInterface
31-
{
32-
publicfunction__invoke(Envelope$envelope):void;
21+
publicfunctionhandle(Envelope$envelope,StackInterface$stack):void;
3322
}

‎src/Symfony/Component/Messenger/Middleware/SendMessageMiddleware.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
publicfunctionhandle(Envelope$envelope,callable$next):void
37+
publicfunctionhandle(Envelope$envelope,StackInterface$stack):void
3838
{
3939
if ($envelope->get(ReceivedStamp::class)) {
4040
// It's a received message. Do not send it back:
41-
$next($envelope);
41+
$stack->next()->handle($envelope,$stack);
4242

4343
return;
4444
}
@@ -54,6 +54,6 @@ public function handle(Envelope $envelope, callable $next): void
5454
}
5555
}
5656

57-
$next($envelope);
57+
$stack->next()->handle($envelope,$stack);
5858
}
5959
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Middleware;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
interface StackInterface
18+
{
19+
/**
20+
* Returns the next middleware to process a message.
21+
*/
22+
publicfunctionnext():MiddlewareInterface;
23+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Middleware;
13+
14+
useSymfony\Component\Messenger\Envelope;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class StackMiddlewareimplements MiddlewareInterface, StackInterface
20+
{
21+
private$middlewareIterator;
22+
23+
publicfunction__construct(\Iterator$middlewareIterator =null)
24+
{
25+
$this->middlewareIterator =$middlewareIterator;
26+
}
27+
28+
publicfunctionnext():MiddlewareInterface
29+
{
30+
if (null ===$iterator =$this->middlewareIterator) {
31+
return$this;
32+
}
33+
$iterator->next();
34+
35+
if (!$iterator->valid()) {
36+
$this->middlewareIterator =null;
37+
38+
return$this;
39+
}
40+
41+
return$iterator->current();
42+
}
43+
44+
publicfunctionhandle(Envelope$envelope,StackInterface$stack):void
45+
{
46+
// no-op: this is the last null middleware
47+
}
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp