8000 Added a middleware that validates messages · symfony/symfony@271b067 · GitHub
[go: up one dir, main page]

Skip to content

Commit 271b067

Browse files
committed
Added a middleware that validates messages
1 parent 9dd89e0 commit 271b067

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,10 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
14451445

14461446
$container->getDefinition('messenger.sender_locator')->replaceArgument(0, $senderLocatorMapping);
14471447
$container->getDefinition('messenger.asynchronous.routing.sender_locator')->replaceArgument(1, $messageToSenderIdsMapping);
1448+
1449+
if (!$container->has('validator')) {
1450+
$container->removeDefinition('messenger.middleware.validator');
1451+
}
14481452
}
14491453

14501454
private function registerCacheConfiguration(array $config, ContainerBuilder $container)

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<tag name="message_bus_middleware" priority="-10" />
2626
</service>
2727

28+
<service id="messenger.middleware.validator" class="Symfony\Component\Messenger\Middleware\ValidatingMiddleware">
29+
<argument type="service" id="validator" />
30+
31+
<tag name="message_bus_middleware" priority="100" />
32+
</service>
33+
2834
<!-- Asynchronous -->
2935
<service id="messenger.asynchronous.routing.sender_locator" class="Symfony\Component\Messenger\Asynchronous\Routing\SenderLocator">
3036
<argument type="service" id="messenger.sender_locator" />
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
namespace Symfony\Component\Messenger\Exception;
13+
14+
use Symfony\Component\Validator\ConstraintViolationListInterface;
15+
16+
/**
17+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
18+
*/
19+
class ValidationFailedException extends \RuntimeException implements ExceptionInterface
20+
{
21+
protected $violations;
22+
protected $violatingMessage;
23+
24+
public function __construct($violatingMessage, ConstraintViolationListInterface $violations)
25+
{
26+
$this->violatingMessage = $violatingMessage;
27+
$this->violations = $violations;
28+
29+
parent::__construct($this->__toString());
30+
}
31+
32+
public function __toString()
33+
{
34+
return sprintf("Message of type %s failed validation\n\n%s", get_class($this->violatingMessage), (string) $this->violations);
35+
}
36+
37+
public function getViolatingMessage()
38+
{
39+
return $this->violatingMessage;
40+
}
41+
42+
public function getViolations()
43+
{
44+
return $this->violations;
45+
}
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
namespace Symfony\Component\Messenger\Middleware;
13+
14+
use Symfony\Component\Messenger\Exception\ValidationFailedException;
15+
use Symfony\Component\Messenger\MiddlewareInterface;
16+
use Symfony\Component\Validator\Validator\ValidatorInterface;
17+
18+
/**
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class ValidationMiddleware implements MiddlewareInterface
22+
{
23+
/**
24+
* @var ValidatorInterface
25+
*/
26+
private $validator;
27+
28+
public function __construct(ValidatorInterface $validator)
29+
{
30+
$this->validator = $validator;
31+
}
32+
33+
public function handle($message, callable $next)
34+
{
35+
$violations = $this->validator->validate($message);
36+
if (count($violations)) {
37+
throw new ValidationFailedException($message, $violations);
38+
}
39+
40+
return $next($message);
41+
}
42+
}

0 commit comments

Comments
 (0)
0