Skip to content

Commit 26d9fe4

Browse files
committed
[FrameworkBundle][Validator] Move the PSR-11 factory to the component
1 parent 07a8360 commit 26d9fe4

File tree

5 files changed

+24
-35
lines changed

5 files changed

+24
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ CHANGELOG
3131
* Deprecated `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass`.
3232
Use `Symfony\Component\Console\DependencyInjection\ConfigCachePass` instead.
3333
* Deprecated `PropertyInfoPass`, use `Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass` instead
34-
* Deprecated extending `ConstraintValidatorFactory`
3534
* Deprecated `ControllerArgumentValueResolverPass`. Use
3635
`Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass` instead
3736
* Deprecated `RoutingResolverPass`, use `Symfony\Component\Routing\DependencyInjection\RoutingResolverPass` instead
@@ -47,9 +46,10 @@ CHANGELOG
4746
`Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass` instead
4847
* Deprecated `AddConstraintValidatorsPass`, use
4948
`Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` instead
50-
* Deprecated `ValidateWorkflowsPass`, use
49+
* Deprecated `ValidateWorkflowsPass`, use
5150
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead
52-
* Deprecated `ConstraintValidatorFactory::__construct()` second argument.
51+
* Deprecated `ConstraintValidatorFactory`, use
52+
`Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
5353

5454
3.2.0
5555
-----

Resources/config/validator.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
</argument>
6060
</service>
6161

62-
<service id="validator.validator_factory" class="Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory">
62+
<service id="validator.validator_factory" class="Symfony\Component\Validator\ContainerConstraintValidatorFactory">
6363
<argument /> <!-- Constraint validators locator -->
6464
</service>
6565

Tests/Validator/ConstraintValidatorFactoryTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
2020
use Symfony\Component\Validator\ConstraintValidator;
2121

22+
/**
23+
* @group legacy
24+
*/
2225
class ConstraintValidatorFactoryTest extends TestCase
2326
{
2427
public function testGetInstanceCreatesValidator()
@@ -27,7 +30,7 @@ public function testGetInstanceCreatesValidator()
2730

2831
$constraint = $this->getMockBuilder('Symfony\\Component\\Validator\\Constraint')->getMock();
2932
$constraint
30-
->expects($this->once())
33+
->expects($this->exactly(2))
3134
->method('validatedBy')
3235
->will($this->returnValue($class));
3336

@@ -63,18 +66,14 @@ public function testGetInstanceReturnsService()
6366

6467
$constraint = $this->getMockBuilder(Constraint::class)->getMock();
6568
$constraint
66-
->expects($this->once())
69+
->expects($this->exactly(2))
6770
->method('validatedBy')
6871
->will($this->returnValue($service));
6972

7073
$factory = new ConstraintValidatorFactory($container);
7174
$this->assertSame($validator, $factory->getInstance($constraint));
7275
}
7376

74-
/**
75-
* @group legacy
76-
* @expectedDeprecation Passing an array of validators or validator aliases as the second argument of "Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory::__construct" is deprecated since 3.3 and will be removed in 4.0. Use the service locator instead.
77-
*/
7877
public function testGetInstanceReturnsServiceWithAlias()
7978
{
8079
$service = 'validator_constraint_service';
@@ -106,7 +105,7 @@ public function testGetInstanceInvalidValidatorClass()
106105
{
107106
$constraint = $this->getMockBuilder('Symfony\\Component\\Validator\\Constraint')->getMock();
108107
$constraint
109-
->expects($this->once())
108+
->expects($this->exactly(2))
110109
->method('validatedBy')
111110
->will($this->returnValue('Fully\\Qualified\\ConstraintValidator\\Class\\Name'));
112111

Validator/ConstraintValidatorFactory.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\Validator\Constraint;
16-
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1716
use Symfony\Component\Validator\ConstraintValidatorInterface;
18-
use Symfony\Component\Validator\Exception\ValidatorException;
17+
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
1918
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
use Symfony\Component\Validator\Exception\ValidatorException;
20+
21+
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use %s instead.', ConstraintValidatorFactory::class, ContainerConstraintValidatorFactory::class), E_USER_DEPRECATED);
2022

2123
/**
2224
* Uses a service container to create constraint validators.
@@ -38,24 +40,19 @@
3840
*
3941
* @author Kris Wallsmith <[email protected]>
4042
*
41-
* @final since version 3.3
43+
* @deprecated since version 3.3
4244
*/
43-
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
45+
class ConstraintValidatorFactory extends ContainerConstraintValidatorFactory
4446
{
4547
protected $container;
4648
protected $validators;
4749

48-
public function __construct(ContainerInterface $container, array $validators = null)
50+
public function __construct(ContainerInterface $container, array $validators = array())
4951
{
50-
$this->container = $container;
51-
52-
if (null !== $validators) {
53-
@trigger_error(sprintf('Passing an array of validators or validator aliases as the second argument of "%s" is deprecated since 3.3 and will be removed in 4.0. Use the service locator instead.', __METHOD__), E_USER_DEPRECATED);
54-
} else {
55-
$validators = array();
56-
}
52+
parent::__construct($container);
5753

5854
$this->validators = $validators;
55+
$this->container = $container;
5956
}
6057

6158
/**
@@ -73,17 +70,10 @@ public function getInstance(Constraint $constraint)
7370
$name = $constraint->validatedBy();
7471

7572
if (!isset($this->validators[$name])) {
76-
if ($this->container->has($name)) {
77-
$this->validators[$name] = $this->container->get($name);
78-
} else {
79-
if (!class_exists($name)) {
80-
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)));
81-
}
73+
return parent::getInstance($constraint);
74+
}
8275

83-
$this->validators[$name] = new $name();
84-
}
85-
} elseif (is_string($this->validators[$name])) {
86-
// To be removed in 4.0
76+
if (is_string($this->validators[$name])) {
8777
$this->validators[$name] = $this->container->get($this->validators[$name]);
8878
}
8979

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"symfony/serializer": "~3.3",
5050
"symfony/translation": "~3.2",
5151
"symfony/templating": "~2.8|~3.0",
52-
"symfony/validator": "~3.3",
52+
"symfony/validator": "~3.3-rc2",
5353
"symfony/workflow": "~3.3",
5454
"symfony/yaml": "~3.2",
5555
"symfony/property-info": "~3.3",
@@ -69,7 +69,7 @@
6969
"symfony/property-info": "<3.3",
7070
"symfony/serializer": "<3.3",
7171
"symfony/translation": "<3.2",
72-
"symfony/validator": "<3.3",
72+
"symfony/validator": "<3.3-rc2",
7373
"symfony/workflow": "<3.3"
7474
},
7575
"suggest": {

0 commit comments

Comments
 (0)