8000 [DependencyInjection] Use a service locator in AddConstraintValidator… · symfony/symfony@7505eb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7505eb8

Browse files
committed
[DependencyInjection] Use a service locator in AddConstraintValidatorsPass
1 parent 1088d62 commit 7505eb8

File tree

7 files changed

+32
-82
lines changed

7 files changed

+32
-82
lines changed

UPGRADE-3.3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ FrameworkBundle
146146
* The `ConstraintValidatorFactory::$validators` and `$container` properties
147147
have been deprecated and will be removed in 4.0.
148148

149+
* Extending `ConstraintValidatorFactory` is deprecated and won't be supported in 4.0.
150+
149151
HttpKernel
150152
-----------
151153

UPGRADE-4.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ FrameworkBundle
188188
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass` class has been
189189
removed. Use the `Symfony\Component\Form\DependencyInjection\FormPass` class instead.
190190

191+
<<<<<<< HEAD
191192
* The `Symfony\Bundle\FrameworkBundle\EventListener\SessionListener` class has been removed.
192193
Use the `Symfony\Component\HttpKernel\EventListener\SessionListener` class instead.
193194

@@ -205,6 +206,8 @@ FrameworkBundle
205206
* The `ConstraintValidatorFactory::$validators` and `$container` properties
206207
have been removed.
207208

209+
* Extending `ConstraintValidatorFactory` is not supported anymore.
210+
208211
HttpFoundation
209212
---------------
210213

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

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

14+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17+
use Symfony\Component\DependencyInjection\Reference;
1718

1819
class AddConstraintValidatorsPass implements CompilerPassInterface
1920
{
@@ -26,22 +27,13 @@ public function process(ContainerBuilder $container)
2627
$validators = array();
2728
foreach ($container->findTaggedServiceIds('validator.constraint_validator') as $id => $attributes) {
2829
if (isset($attributes[0]['alias'])) {
29-
$validators[$attributes[0]['alias']] = $id;
30+
$validators[$attributes[0]['alias']] = new Reference($id);
3031
}
3132

3233
$definition = $container->getDefinition($id);
33-
34-
if (!$definition->isPublic()) {
35-
throw new InvalidArgumentException(sprintf('The service "%s" must be public as it can be lazy-loaded.', $id));
36-
}
37-
38-
if ($definition->isAbstract()) {
39-
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as it can be lazy-loaded.', $id));
40-
}
41-
42-
$validators[$definition->getClass()] = $id;
34+
$validators[$definition->getClass()] = new Reference($id);
4335
}
4436

45-
$container->getDefinition('validator.validator_factory')->replaceArgument(1, $validators);
37+
$container->getDefinition('validator.validator_factory')->replaceArgument(0, new ServiceLocatorArgument($validators));
4638
}
4739
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
</service>
5858

5959
<service id="validator.validator_factory" class="Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory" public="false">
60-
<argument type="service" id="service_container" />
61-
<argument type="collection" />
60+
<argument type="service-locator" /> <!-- Constraint validators locator -->
6261
</service>
6362

6463
<service id="validator.expression" class="Symfony\Component\Validator\Constraints\ExpressionValidator">

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
16+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
17+
use Symfony\Component\DependencyInjection\Reference;
1618

1719
class AddConstraintValidatorsPassTest extends TestCase
1820
{
@@ -55,11 +57,11 @@ public function testThatConstraintValidatorServicesAreProcessed()
5557

5658
$validatorFactoryDefinition->expects($this->once())
5759
->method('replaceArgument')
58-
->with(1, array(
59-
'My\Fully\Qualified\Class\Named\Validator1' => 'my_constraint_validator_service1',
60-
'my_constraint_validator_alias1' => 'my_constraint_validator_service1',
61-
'My\Fully\Qualified\Class\Named\Validator2' => 'my_constraint_validator_service2',
62-
));
60+
->with(0, new ServiceLocatorArgument(array(
61+
'My\Fully\Qualified\Class\Named\Validator1' => new Reference('my_constraint_validator_service1'),
62+
'my_constraint_validator_alias1' => new Reference('my_constraint_validator_service1'),
63+
'My\Fully\Qualified\Class\Named\Validator2' => new Reference('my_constraint_validator_service2'),
64+
)));
6365

6466
$addConstraintValidatorsPass = new AddConstraintValidatorsPass();
6567
$addConstraintValidatorsPass->process($container);

src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php

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

1212
namespace Symfony\Bundle\FrameworkBundle\Validator;
1313

14-
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1717
use Symfony\Component\Validator\ConstraintValidatorInterface;
@@ -37,17 +37,12 @@
3737
* }
3838
*
3939
* @author Kris Wallsmith <kris@symfony.com>
40+
*
41+
* @final since version 3.3
4042
*/
4143
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
4244
{
43-
/**
44-
* @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below
45-
*/
4645
protected $container;
47-
48-
/**
49-
* @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below
50-
*/
5146
protected $validators;
5247

5348
/**
@@ -77,11 +72,15 @@ public function getInstance(Constraint $constraint)
7772
$name = $constraint->validatedBy();
7873

7974
if (!isset($this->validators[$name])) {
80-
if (!class_exists($name)) {
81-
throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, get_class($constraint)));
75+
if ($this->container->has($name)) {
76+
$this->validators[$name] = $this->container->get($name);
77+
} else {
78+
if (!class_exists($name)) {
79+
throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, get_class($constraint)));
80+
}
81+
82+
$this->validators[$name] = new $name();
8283
}
83-
84-
$this->validators[$name] = new $name();
8584
} elseif (is_string($this->validators[$name])) {
8685
$this->validators[$name] = $this->container->get($this->validators[$name]);
8786
}
@@ -92,52 +91,4 @@ public function getInstance(Constraint $constraint)
9291

9392
return $this->validators[$name];
9493
}
95-
96-
/**
97-
* @internal
98-
*/
99-
public function __get($name)
100-
{
101-
if ('validators' === $name || 'container' === $name) {
102-
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED);
103-
}
104-
105-
return $this->$name;
106-
}
107-
108-
/**
109-
* @internal
110-
*/
111-
public function __set($name, $value)
112-
{
113-
if ('validators' === $name || 'container' === $name) {
114-
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED);
115-
}
116-
117-
$this->$name = $value;
118-
}
119-
120-
/**
121-
* @internal
122-
*/
123-
public function __isset($name)
124-
{
125-
if ('validators' === $name || 'container' === $name) {
126-
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED);
127-
}
128-
129-
return isset($this->$name);
130-
}
131-
132-
/**
133-
* @internal
134-
*/
135-
public function __unset($name)
136-
{
137-
if ('validators' === $name || 'container' === $name) {
138-
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED);
139-
}
140-
141-
unset($this->$name);
142-
}
14394
}

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"symfony/serializer": "~3.3",
5050
"symfony/translation": "~2.8|~3.0",
5151
"symfony/templating": "~2.8|~3.0",
52-
"symfony/validator": "~3.2",
52+
"symfony/validator": "~3.3",
5353
"symfony/yaml": "~3.2",
5454
"symfony/property-info": "~3.3",
5555
"doctrine/annotations": "~1.0",
@@ -65,7 +65,8 @@
6565
"symfony/console": "<3.3",
6666
"symfony/serializer": "<3.3",
6767
"symfony/form": "<3.3",
68-
"symfony/property-info": "<3.3"
68+
"symfony/property-info": "<3.3",
69+
"symfony/validator": "<3.3"
6970
},
7071
"suggest": {
7172
"ext-apcu": "For best performance of the system caches",

0 commit comments

Comments
 (0)
0