8000 feature #22081 [FrameworkBundle][Validator] Move Validator passes to … · symfony/symfony@c73009a · GitHub
[go: up one dir, main page]

Skip to content

Commit c73009a

Browse files
committed
feature #22081 [FrameworkBundle][Validator] Move Validator passes to the component (chalasr)
This PR was merged into the 3.3-dev branch. Discussion ---------- [FrameworkBundle][Validator] Move Validator passes to the component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- 0b741da Move AddValidatorInitializersrPass & AddConstraintValidatorsPass to the Validator
2 parents a25fd7d + 0b741da commit c73009a

File tree

14 files changed

+269
-56
lines changed

14 files changed

+269
-56
lines changed

UPGRADE-3.3.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ FrameworkBundle
186186
* The `Symfony\Bundle\FrameworkBundle\Translation\Translator` constructor now takes the
187187
default locale as 3rd argument. Not passing it will trigger an error in 4.0.
188188

189+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass`
190+
class has been deprecated and will be removed in 4.0.
191+
Use the `Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass` class instead.
192+
193+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass`
194+
class has been deprecated and will be removed in 4.0.
195+
Use the `Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` class instead.
196+
189197
HttpFoundation
190198
--------------
191199

UPGRADE-4.0.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,13 @@ FrameworkBundle
279279
* The `Symfony\Bundle\FrameworkBundle\Translation\Translator` constructor now takes the
280280
default locale as mandatory 3rd argument.
281281

282-
HttpFoundation
283-
---------------
282+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass` class has been
283+
removed. Use the `Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass`
284+
class instead.
285+
286+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass` class has been
287+
removed. Use the `Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass`
288+
class instead.
284289

285290
HttpFoundation
286291
--------------

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ CHANGELOG
4040
making `Translator` works with any PSR-11 container
4141
* Added `framework.serializer.mapping` config option allowing to define custom
4242
serialization mapping files and directories
43+
* Deprecated `AddValidatorInitializersPass`, use
44+
`Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass` instead
45+
* Deprecated `AddConstraintValidatorsPass`, use
46+
`Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` instead
4347

4448
3.2.0
4549
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConstraintValidatorsPass.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,13 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
15-
use Symfony\Component\DependencyInjection\ContainerBuilder;
16-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17-
use Symfony\Component\DependencyInjection\Definition;
18-
use Symfony\Component\DependencyInjection\Reference;
19-
use Symfony\Component\DependencyInjection\ServiceLocator;
14+
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass as BaseAddConstraintValidatorsPass;
2015

21-
class AddConstraintValidatorsPass implements CompilerPassInterface
22-
{
23-
public function process(ContainerBuilder $container)
24-
{
25-
if (!$container->hasDefinition('validator.validator_factory')) {
26-
return;
27-
}
28-
29-
$validators = array();
30-
foreach ($container->findTaggedServiceIds('validator.constraint_validator') as $id => $attributes) {
31-
$definition = $container->getDefinition($id);
32-
33-
if ($definition->isAbstract()) {
34-
continue;
35-
}
36-
37-
if (isset($attributes[0]['alias'])) {
38-
$validators[$attributes[0]['alias']] = new ServiceClosureArgument(new Reference($id));
39-
}
16+
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use %s instead.', AddConstraintValidatorsPass::class, BaseAddConstraintValidatorsPass::class), E_USER_DEPRECATED);
4017

41-
$validators[$definition->getClass()] = new ServiceClosureArgument(new Reference($id));
42-
}
43-
44-
$container->getDefinition('validator.validator_factory')->replaceArgument(0, (new Definition(ServiceLocator::class, array($validators)))->addTag('container.service_locator'));
45-
}
18+
/**
19+
* @deprecated since version 3.3, to be removed in 4.0. Use {@link BaseAddConstraintValidatorsPass} instead
20+
*/
21+
class AddConstraintValidatorsPass extends BaseAddConstraintValidatorsPass
22+
{
4623
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddValidatorInitializersPass.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,13 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\ContainerBuilder;
15-
use Symfony\Component\DependencyInjection\Reference;
16-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14+
use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass as BaseAddValidatorsInitializerPass;
1715

18-
class AddValidatorInitializersPass implements CompilerPassInterface
19-
{
20-
public function process(ContainerBuilder $container)
21-
{
22-
if (!$container->hasDefinition('validator.builder')) {
23-
return;
24-
}
25-
26-
$validatorBuilder = $container->getDefinition('validator.builder');
27-
28-
$initializers = array();
29-
foreach ($container->findTaggedServiceIds('validator.initializer') as $id => $attributes) {
30-
$initializers[] = new Reference($id);
31-
}
16+
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use %s instead.', AddValidatorInitializersPass::class, BaseAddValidatorsInitializerPass::class), E_USER_DEPRECATED);
3217

33-
$validatorBuilder->addMethodCall('addObjectInitializers', array($initializers));
34-
}
18+
/**
19+
* @deprecated since version 3.3, to be removed in 4.0. Use {@link BaseAddValidatorInitializersPass} instead
20+
*/
21+
class AddValidatorInitializersPass extends BaseAddValidatorsInitializerPass
22+
{
3523
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle;
1313

1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
15-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
1615
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
17-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
1816
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CacheCollectorPass;
1917
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
2018
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
@@ -47,6 +45,8 @@
4745
use Symfony\Component\HttpFoundation\Request;
4846
use Symfony\Component\HttpKernel\Bundle\Bundle;
4947
use Symfony\Component\Config\Resource\ClassExistenceResource;
48+
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
49+
use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;
5050

5151
/**
5252
* Bundle.
@@ -84,9 +84,9 @@ public function build(ContainerBuilder $container)
8484
// but as late as possible to get resolved parameters
8585
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
8686
$container->addCompilerPass(new TemplatingPass());
87-
$container->addCompilerPass(new AddConstraintValidatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
87+
$this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class, PassConfig::TYPE_BEFORE_REMOVING);
8888
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
89-
$container->addCompilerPass(new AddValidatorInitializersPass());
89+
$this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
9090
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class);
9191
$container->addCompilerPass(new TranslatorPass());
9292
$container->addCompilerPass(new LoggingTranslatorPass());

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConstraintValidatorsPassTest.php

Expand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConstraintValidatorsPassTest.php
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\ServiceLocator;
2121

22+
/**
23+
* @group legacy
24+
*/
2225
class AddConstraintValidatorsPassTest extends TestCase
2326
{
2427
public function testThatConstraintValidatorServicesAreProcessed()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bundle\FullStack;
1717
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1818
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
19-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
2019
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
2120
use Symfony\Component\Cache\Adapter\AdapterInterface;
2221
use Symfony\Component\Cache\Adapter\ApcuAdapter;
@@ -41,6 +40,7 @@
4140
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
4241
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4342
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
43+
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
4444

4545
abstract class FrameworkExtensionTest extends TestCase
4646
{

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* added `AddValidatorInitializersPass`
8+
* added `AddConstraintValidatorsPass`
9+
410
3.2.0
511
-----
612

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Validator\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\DependencyInjection\ServiceLocator;
20+
21+
/**
22+
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
23+
* @author Robin Chalas <robin.chalas@gmail.com>
24+
*/
25+
class AddConstraintValidatorsPass implements CompilerPassInterface
26+
{
27+
private $validatorFactoryServiceId;
28+
private $constraintValidatorTag;
29+
30+
public function __construct($validatorFactoryServiceId = 'validator.validator_factory', $constraintValidatorTag = 'validator.constraint_validator')
31+
{
32+
$this->validatorFactoryServiceId = $validatorFactoryServiceId;
33+
$this->constraintValidatorTag = $constraintValidatorTag;
34+
}
35+
36+
public function process(ContainerBuilder $container)
37+
{
38+
if (!$container->hasDefinition($this->validatorFactoryServiceId)) {
39+
return;
40+
}
41+
42+
$validators = array();
43+
foreach ($container->findTaggedServiceIds($this->constraintValidatorTag) as $id => $attributes) {
44+
$definition = $container->getDefinition($id);
45+
46+
if ($definition->isAbstract()) {
47+
continue;
48+
}
49+
50+
if (isset($attributes[0]['alias'])) {
51+
$validators[$attributes[0]['alias']] = new ServiceClosureArgument(new Reference($id));
52+
}
53+
54+
$validators[$definition->getClass()] = new ServiceClosureArgument(new Reference($id));
55+
}
56+
57+
$container
58+
->getDefinition('validator.validator_factory')
59+
->replaceArgument(0, (new Definition(ServiceLocator::class, array($validators)))->addTag('container.service_locator'))
60+
;
61+
}
62+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Validator\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Reference;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
18+
/**
19+
* @author Fabien Potencier <fabien@symfony.com>
20+
* @author Robin Chalas <robin.chalas@gmail.com>
21+
*/
22+
class AddValidatorInitializersPass implements CompilerPassInterface
23+
{
24+
private $builderService;
25+
private $initializerTag;
26+
27+
public function __construct($builderService = 'validator.builder', $initializerTag = 'validator.initializer')
28+
{
29+
$this->builderService = $builderService;
30+
$this->initializerTag = $initializerTag;
31+
}
32+
33+
public function process(ContainerBuilder $container)
34+
{
35+
if (!$container->hasDefinition($this->builderService)) {
36+
return;
37+
}
38+
39+
$initializers = array();
40+
foreach ($container->findTaggedServiceIds($this->initializerTag) as $id => $attributes) {
41+
if ($container->getDefinition($id)->isAbstract()) {
42+
continue;
43+
}
44+
45+
$initializers[] = new Reference($id);
46+
}
47+
48+
$container->getDefinition($this->builderService)->addMethodCall('addObjectInitializers', array($initializers));
49+
}
50+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Validator\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
16+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Definition;
19+
use Symfony\Component\DependencyInjection\Reference;
20+
use Symfony\Component\DependencyInjection\ServiceLocator;
21+
22+
class AddConstraintValidatorsPassTest extends TestCase
23+
{
24+
public function testThatConstraintValidatorServicesAreProcessed()
25+
{
26+
$container = new ContainerBuilder();
27+
$validatorFactory = $container->register('validator.validator_factory')
28+
->addArgument(array());
29+
30+
$container->register('my_constraint_validator_service1', Validator1::class)
31+
->addTag('validator.constraint_validator', array('alias' => 'my_constraint_validator_alias1'));
32+
$container->register('my_constraint_validator_service2', Validator2::class)
33+
->addTag('validator.constraint_validator');
34+
$container->register('my_abstract_constraint_validator')
35+
->setAbstract(true)
36+
->addTag('validator.constraint_validator');
37+
38+
$addConstraintValidatorsPass = new AddConstraintValidatorsPass();
39+
$addConstraintValidatorsPass->process($container);
40+
41+
$this->assertEquals((new Definition(ServiceLocator::class, array(array(
42+
Validator1::class => new ServiceClosureArgument(new Reference('my_constraint_validator_service1')),
43+
'my_constraint_validator_alias1' => new ServiceClosureArgument(new Reference('my_constraint_validator_service1')),
44+
Validator2::class => new ServiceClosureArgument(new Reference('my_constraint_validator_service2')),
45+
))))->addTag('container.service_locator'), $validatorFactory->getArgument(0));
46+
}
47+
48+
public function testThatCompilerPassIsIgnoredIfThereIsNoConstraintValidatorFactoryDefinition()
49+
{
50+
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
51+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
52+
53+
$container->expects($this->never())->method('findTaggedServiceIds');
54+
$container->expects($this->never())->method('getDefinition');
55+
$container->expects($this->atLeastOnce())
56+
->method('hasDefinition')
57+
->with('validator.validator_factory')
58+
->will($this->returnValue(false));
59+
$definition->expects($this->never())->method('replaceArgument');
60+
61+
$addConstraintValidatorsPass = new AddConstraintValidatorsPass();
62+
$addConstraintValidatorsPass->process($container);
63+
}
64+
}

0 commit comments

Comments
 (0)
0