-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Added a middleware that validates messages #26648
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
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
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
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
11 changes: 11 additions & 0 deletions
11
...ny/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_validation.php
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', array( | ||
'messenger' => array( | ||
'middlewares' => array( | ||
'validation' => array( | ||
'enabled' => false, | ||
), | ||
), | ||
), | ||
)); |
15 changes: 15 additions & 0 deletions
15
...ny/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_validation.xml
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<container xmlns="http://symfony.com/schema/d 6293 ic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config> | ||
<framework:messenger> | ||
<framework:middlewares> | ||
<framework:validation enabled="false"/> | ||
</framework:middlewares> | ||
</framework:messenger> | ||
</framework:config> | ||
</container> |
5 changes: 5 additions & 0 deletions
5
...ny/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_validation.yml
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
framework: | ||
messenger: | ||
middlewares: | ||
validation: | ||
enabled: false |
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
44 changes: 44 additions & 0 deletions
44
src/Symfony/Component/Messenger/Exception/ValidationFailedException.php
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?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\Exception; | ||
|
||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class ValidationFailedException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
private $violations; | ||
private $violatingMessage; | ||
|
||
/** | ||
* @param object $violatingMessage | ||
*/ | ||
public function __construct($violatingMessage, ConstraintViolationListInterface $violations) | ||
{ | ||
$this->violatingMessage = $violatingMessage; | ||
$this->violations = $violations; | ||
|
||
parent::__construct(sprintf('Message of type "%s" failed validation.', get_class($this->violatingMessage))); | ||
} | ||
|
||
public function getViolatingMessage() | ||
{ | ||
return $this->violatingMessage; | ||
} | ||
|
||
public function getViolations(): ConstraintViolationListInterface | ||
{ | ||
return $this->violations; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?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\Middleware; | ||
|
||
use Symfony\Component\Messenger\Exception\ValidationFailedException; | ||
use Symfony\Component\Messenger\MiddlewareInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class ValidationMiddleware implements MiddlewareInterface | ||
{ | ||
private $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
public function handle($message, callable $next) | ||
{ | ||
$violations = $this->validator->validate($message); | ||
if (count($violations)) { | ||
throw new ValidationFailedException($message, $violations); | ||
} | ||
|
||
return $next($message); | ||
} | ||
} |
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.
Is it really needed? I'm used to handle one command at a time so I always have the message at hand, am I missing a use case? The existing
NoHandlerForMessageException
doesn't expose itThere 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.
The difference is that the
NoHandlerForMessageException
is a logic exception (or at least, should be to me), it is meant to fail badly. No need to access the exception programmatically and retrieve the issuing message.ValidationFailedException
is a runtime exception, meant to be caught and handled/transformed to a better representation (e.g: an API problem response). It can also be used in a profiler panel or whatever :)Anyway, even if we often handle one message at the time, use-cases with multiple messages do exist. We should cover them. 👍