Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 99e3fc3

Browse filesBrowse files
committed
Move AddValidatorInitializersrPass & AddConstraintValidatorsPass to the Validator
1 parent a001c2e commit 99e3fc3
Copy full SHA for 99e3fc3

14 files changed

+261
-54
lines changed

‎UPGRADE-3.3.md

Copy file name to clipboardExpand all lines: UPGRADE-3.3.md
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ FrameworkBundle
172172
class has been deprecated and will be removed in 4.0. Use the
173173
`Symfony\Component\Routing\DependencyInjection\RoutingResolverPass` class instead.
174174

175+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass`
176+
class has been deprecated and will be removed in 4.0.
177+
Use the `Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass` class instead.
178+
179+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass`
180+
class has been deprecated and will be removed in 4.0.
181+
Use the `Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` class instead.
182+
175183
HttpKernel
176184
-----------
177185

‎UPGRADE-4.0.md

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ FrameworkBundle
271271
class has been removed. Use the
272272
`Symfony\Component\Routing\DependencyInjection\RoutingResolverPass` class instead.
273273

274+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass` class has been
275+
removed. Use the `Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass`
276+
class instead.
277+
278+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass` class has been
279+
removed. Use the `Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass`
280+
class instead.
281+
274282
HttpFoundation
275283
---------------
276284

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ CHANGELOG
3030
* Deprecated `ControllerArgumentValueResolverPass`. Use
3131
`Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass` instead
3232
* Deprecated `RoutingResolverPass`, use `Symfony\Component\Routing\DependencyInjection\RoutingResolverPass` instead
33+
* Deprecated `AddValidatorInitializersPass`, use `Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass` instead
34+
* Deprecated `AddConstraintValidatorsPass`. Use `Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass`
35+
instead
3336

3437
3.2.0
3538
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConstraintValidatorsPass.php
+7-30Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddValidatorInitializersPass.php
+7-19Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
+4-4Lines 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.
@@ -82,9 +82,9 @@ public function build(ContainerBuilder $container)
8282
// but as late as possible to get resolved parameters
8383
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
8484
$container->addCompilerPass(new TemplatingPass());
85-
$container->addCompilerPass(new AddConstraintValidatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
85+
$this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class, PassConfig::TYPE_BEFORE_REMOVING);
8686
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
87-
$container->addCompilerPass(new AddValidatorInitializersPass());
87+
$this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
8888
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class);
8989
$container->addCompilerPass(new TranslatorPass());
9090
$container->addCompilerPass(new LoggingTranslatorPass());

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConstraintValidatorsPassTest.php
+3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Bundle\FullStack;
1616
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1717
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
18-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
1918
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
2019
use Symfony\Component\Cache\Adapter\AdapterInterface;
2120
use Symfony\Component\Cache\Adapter\ApcuAdapter;
@@ -38,6 +37,7 @@
3837
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
3938
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4039
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
40+
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
4141

4242
abstract class FrameworkExtensionTest extends TestCase
4343
{

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+6Lines 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

+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
class AddConstraintValidatorsPass implements CompilerPassInterface
22+
{
23+
private $validatorFactoryServiceId;
24+
private $constraintValidatorTag;
25+
26+
public function __construct($validatorFactoryServiceId = 'validator.validator_factory', $constraintValidatorTag = 'validator.constraint_validator')
27+
{
28+
$this->validatorFactoryServiceId = $validatorFactoryServiceId;
29+
$this->constraintValidatorTag = $constraintValidatorTag;
30+
}
31+
32+
public function process(ContainerBuilder $container)
33+
{
34+
if (!$container->hasDefinition($this->validatorFactoryServiceId)) {
35+
return;
36+
}
37+
38+
$validators = array();
39+
foreach ($container->findTaggedServiceIds($this->constraintValidatorTag) as $id => $attributes) {
40+
$definition = $container->getDefinition($id);
41+
42+
if ($definition->isAbstract()) {
43+
continue;
44+
}
45+
46+
if (isset($attributes[0]['alias'])) {
47+
$validators[$attributes[0]['alias']] = new ServiceClosureArgument(new Reference($id));
48+
}
49+
50+
$validators[$definition->getClass()] = new ServiceClosureArgument(new Reference($id));
51+
}
52+
53+
$container
54+
->getDefinition('validator.validator_factory')
55+
->replaceArgument(0, (new Definition(ServiceLocator::class, array($validators)))->addTag('container.service_locator'))
56+
;
57+
}
58+
}
+46Lines 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\Validator\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Reference;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
18+
class AddValidatorInitializersPass implements CompilerPassInterface
19+
{
20+
private $builderService;
21+
private $initializerTag;
22+
23+
public function __construct($builderService = 'validator.builder', $initializerTag = 'validator.initializer')
24+
{
25+
$this->builderService = $builderService;
26+
$this->initializerTag = $initializerTag;
27+
}
28+
29+
public function process(ContainerBuilder $container)
30+
{
31+
if (!$container->hasDefinition($this->builderService)) {
32+
return;
33+
}
34+
35+
$initializers = array();
36+
foreach ($container->findTaggedServiceIds($this->initializerTag) as $id => $attributes) {
37+
if ($container->getDefinition($id)->isAbstract()) {
38+
continue;
39+
}
40+
41+
$initializers[] = new Reference($id);
42+
}
43+
44+
$container->getDefinition($this->builderService)->addMethodCall('addObjectInitializers', array($initializers));
45+
}
46+
}
+64Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.