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

Skip to content

Commit 43a5171

Browse files
Nyholmsroze
authored andcommitted
[Messenger] Added a middleware that validates messages
1 parent e6302e2 commit 43a5171

File tree

11 files changed

+155
-4
lines changed

11 files changed

+155
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,10 +1005,14 @@ function ($a) {
10051005
->arrayNode('middlewares')
10061006
->addDefaultsIfNotSet()
10071007
->children()
1008-
->arrayNode('doctrine_transaction')
1009-
->canBeEnabled()
1010-
->children()
1011-
->scalarNode('entity_manager_name')->info('The name of the entity manager to use')->defaultNull()->end()
1008+
->arrayNode('doctrine_transaction')
1009+
->canBeEnabled()
1010+
->children()
1011+
->scalarNode('entity_manager_name')->info('The name of the entity manager to use')->defaultNull()->end()
1012+
->end()
1013+
->end()
1014+
->arrayNode('validation')
1015+
->{!class_exists(FullStack::class) && class_exists(Validation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
10121016
->end()
10131017
->end()
10141018
->end()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,14 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
14591459
} else {
14601460
$container->removeDefinition('messenger.middleware.doctrine_transaction');
14611461
}
1462+
1463+
if ($config['middlewares']['validation']['enabled']) {
1464+
if (!$container->has('validator')) {
1465+
throw new LogicException('The Validation middleware is only available when the Validator component is installed and enabled. Try running "composer require symfony/validator".');
1466+
}
1467+
} else {
1468+
$container->removeDefinition('messenger.middleware.validator');
1469+
}
14621470
}
14631471

14641472
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\ValidationMiddleware">
29+
<argument type="service" id="validator" />
30+
31+
<tag name="message_bus_middleware" priority="100" />
32+
</service>
33+
2834
<service id="messenger.middleware.doctrine_transaction" class="Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware">
2935
<argument type="service" id="doctrine" />
3036
<argument /> <!-- Entity manager name -->

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,16 @@
371371
<xsd:complexType name="messenger_middleware">
372372
<xsd:sequence>
373373
<xsd:element name="doctrine-transaction" type="messenger_doctrine_transaction" minOccurs="0" maxOccurs="1" />
374+
<xsd:element name="validation" type="messenger_validation" minOccurs="0" maxOccurs="1" />
374375
</xsd:sequence>
375376
</xsd:complexType>
376377

377378
<xsd:complexType name="messenger_doctrine_transaction">
378379
<xsd:attribute name="entity-manager-name" type="xsd:string" />
379380
<xsd:attribute name="enabled" type="xsd:boolean" />
380381
</xsd:complexType>
382+
383+
<xsd:complexType name="messenger_validation">
384+
<xsd:attribute name="enabled" type="xsd:boolean" />
385+
</xsd:complexType>
381386
</xsd:schema>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
258258
'enabled' => false,
259259
'entity_manager_name' => null,
260260
),
261+
'validation' => array(
262+
'enabled' => false,
263+
),
261264
),
262265
),
263266
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', array(
4+
'messenger' => array(
5+
'middlewares' => array(
6+
'validation' => array(
7+
'enabled' => false,
8+
),
9+
),
10+
),
11+
));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<framework:config>
9+
<framework:messenger>
10+
<framework:middlewares>
11+
<framework:validation enabled="false"/>
12+
</framework:middlewares>
13+
</framework:messenger>
14+
</framework:config>
15+
</container>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework:
2+
messenger:
3+
middlewares:
4+
validation:
5+
enabled: false

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
4646
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
4747
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
48+
use Symfony\Component\Validator\Validation;
4849
use Symfony\Component\Workflow;
4950

5051
abstract class FrameworkExtensionTest extends TestCase
@@ -532,6 +533,16 @@ public function testMessengerDoctrine()
532533
$this->assertEquals('foobar', $def->getArgument(1));
533534
}
534535

536+
public function testMessengerValidationDisabled()
537+
{
538+
if (!class_exists(Validation::class)) {
539+
self::markTestSkipped('Skipping tests since Validator component is not installed');
540+
}
541+
542+
$container = $this->createContainerFromFile('messenger_validation');
543+
$this->assertFalse($container->hasDefinition('messenger.middleware.validator'));
544+
}
545+
535546
public function testTranslator()
536547
{
537548
$container = $this->createContainerFromFile('full');
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
private $violations;
22+
private $violatingMessage;
23+
24+
/**
25+
* @param object $violatingMessage
26+
*/
27+
public function __construct($violatingMessage, ConstraintViolationListInterface $violations)
28+
{
29+
$this->violatingMessage = $violatingMessage;
30+
$this->violations = $violations;
31+
32+
parent::__construct(sprintf('Message of type "%s" failed validation.', get_class($this->violatingMessage)));
33+
}
34+
35+
public function getViolatingMessage()
36+
{
37+
return $this->violatingMessage;
38+
}
39+
40+
public function getViolations(): ConstraintViolationListInterface
41+
{
42+
return $this->violations;
43+
}
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
private $validator;
24+
25+
public function __construct(ValidatorInterface $validator)
26+
{
27+
$this->validator = $validator;
28+
}
29+
30+
public function handle($message, callable $next)
31+
{
32+
$violations = $this->validator->validate($message);
33+
if (count($violations)) {
34+
throw new ValidationFailedException($message, $violations);
35+
}
36+
37+
return $next($message);
38+
}
39+
}

0 commit comments

Comments
 (0)
0