-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Messenger] Envelope as first class citizen in middleware too #27322
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[Messenger] Envelope as first class citizen in middleware too
- Loading branch information
commit a2d7960cea7b22e6e6498a5d3764e71a638fa34b
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,13 @@ | |
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface; | ||
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\EnvelopeAwareInterface; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
|
||
/** | ||
* @author Samuel Roze <[email protected]> | ||
* @author Tobias Schultze <http://tobion.de> | ||
*/ | ||
class SendMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface | ||
class SendMessageMiddleware implements MiddlewareInterface | ||
{ | ||
private $senderLocator; | ||
private $messagesToSendAndHandleMapping; | ||
|
@@ -36,12 +35,11 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($message, callable $next) | ||
public function handle(Envelope $envelope, callable $next) | ||
{ | ||
$envelope = Envelope::wrap($message); | ||
if ($envelope->get(ReceivedMessage::class)) { | ||
// It's a received message. Do not send it back: | ||
return $next($message); | ||
return $next($envelope); | ||
} | ||
|
||
$sender = $this->senderLocator->getSenderForMessage($envelope->getMessage()); | ||
|
@@ -54,7 +52,7 @@ public function handle($message, callable $next) | |
} | ||
} | ||
|
||
return $next($message); | ||
return $next($envelope); | ||
} | ||
|
||
private function mustSendAndHandle($message): bool | ||
|
23 changes: 0 additions & 23 deletions
23
src/Symfony/Component/Messenger/EnvelopeAwareInterface.php
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,17 +11,18 @@ | |
|
||
namespace Symfony\Component\Messenger\Middleware; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; | ||
|
||
/** | ||
* @author Samuel Roze <[email protected]> | ||
*/ | ||
class AllowNoHandlerMiddleware implements MiddlewareInterface | ||
{ | ||
public function handle($message, callable $next) | ||
public function handle(Envelope $envelope, callable $next) | ||
{ | ||
try { | ||
return $next($message); | ||
return $next($envelope); | ||
} catch (NoHandlerForMessageException $e) { | ||
// We allow not having a handler for this message. | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Messenger\Middleware; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Messenger\Envelope; | ||
|
||
/** | ||
* @author Samuel Roze <[email protected]> | ||
|
@@ -28,12 +29,14 @@ public function __construct(LoggerInterface $logger) | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($message, callable $next) | ||
public function handle(Envelope $envelope, callable $next) | ||
{ | ||
$message = $envelope->getMessage(); | ||
|
||
$this->logger->debug('Starting handling message {class}', $this->createContext($message)); | ||
|
||
try { | ||
$result = $next($message); | ||
$result = $next($envelope); | ||
} catch (\Throwable $e) { | ||
$this->logger->warning('An exception occurred while handling message {class}', array_merge( | ||
$this->createContext($message), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
namespace Symfony\Component\Messenger\Middleware; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
|
||
/** | ||
* @author Samuel Roze <[email protected]> | ||
* | ||
|
@@ -19,9 +21,9 @@ | |
interface MiddlewareInterface | ||
{ | ||
/** | ||
* @param object $message | ||
* @param Envelope $envelope | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle($message, callable $next); | ||
public function handle(Envelope $envelope, callable $next); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,15 +12,14 @@ | |
namespace Symfony\Component\Messenger\Middleware; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\EnvelopeAwareInterface; | ||
use Symfony\Component\Messenger\Exception\ValidationFailedException; | ||
use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class ValidationMiddleware implements MiddlewareInterface, EnvelopeAwareInterface | ||
class ValidationMiddleware implements MiddlewareInterface | ||
{ | ||
private $validator; | ||
|
||
|
@@ -29,21 +28,20 @@ public function __construct(ValidatorInterface $validator) | |
$this->validator = $validator; | ||
} | ||
|
||
public function handle($message, callable $next) | ||
public function handle(Envelope $envelope, callable $next) | ||
{ | ||
$envelope = Envelope::wrap($message); | ||
$subject = $envelope->getMessage(); | ||
$message = $envelope->getMessage(); | ||
$groups = null; | ||
/** @var ValidationConfiguration|null $validationConfig */ | ||
if ($validationConfig = $envelope->get(ValidationConfiguration::class)) { | ||
$groups = $validationConfig->getGroups(); | ||
} | ||
|
||
$violations = $this->validator->validate($subject, null, $groups); | ||
$violations = $this->validator->validate($message, null, $groups); | ||
if (\count($violations)) { | ||
throw new ValidationFailedException($subject, $violations); | ||
throw new ValidationFailedException($message, $violations); | ||
} | ||
|
||
return $next($message); | ||
return $next($envelope); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It feels like
new Envelope(...)
makes more sense here if you're sure that the message isn't another envelope instance, it also seems natural to me, IMHO. (same for other tests)Uh oh!
There was an error while loading. Please reload this page.
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.
You're right. I think I'll remove this named construct as well as it doesn't really serve a purpose anymore, appart the fluent api without extra
(
)