8000 [FrameworkBundle][Validator] Move the PSR-11 factory to the component · symfony/symfony@68c1917 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68c1917

Browse files
committed
[FrameworkBundle][Validator] Move the PSR-11 factory to the component
1 parent 2642e72 commit 68c1917

File tree

10 files changed

+181
-52
lines changed

10 files changed

+181
-52
lines changed

UPGRADE-3.3.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,6 @@ FrameworkBundle
203203
deprecated and will be removed in 4.0. Use the `Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass`
204204
class instead.
205205

206-
* The `ConstraintValidatorFactory::$validators` and `$container` properties
207-
have been deprecated and will be removed in 4.0.
208-
209-
* Extending `ConstraintValidatorFactory` is deprecated and won't be supported in 4.0.
210-
211206
* Class parameters related to routing have been deprecated and will be removed in 4.0.
212207
* router.options.generator_class
213208
* router.options.generator_base_class
@@ -246,9 +241,9 @@ FrameworkBundle
246241
class has been deprecated and will be removed in 4.0. Use the
247242
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` class instead.
248243

249-
* Passing an array of validators or validator aliases as the second argument of
250-
`ConstraintValidatorFactory::__construct()` is deprecated since 3.3 and will
251-
be removed in 4.0. Use the service locator instead.
244+
* The `Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory`
245+
class has been deprecated and will be removed in 4.0.
246+
Use `Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
252247

253248
HttpFoundation
254249
--------------

UPGRADE-4.0.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,6 @@ FrameworkBundle
294294
removed. Use the `Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass`
295295
class instead.
296296

297-
* The `ConstraintValidatorFactory::$validators` and `$container` properties
298-
have been removed.
299-
300-
* Extending `ConstraintValidatorFactory` is not supported anymore.
301-
302-
* Passing an array of validators or validator aliases as the second argument of
303-
`ConstraintValidatorFactory::__construct()` has been removed.
304-
Use the service locator instead.
305-
306297
* Class parameters related to routing have been removed
307298
* router.options.generator_class
308299
* router.options.generator_base_class
@@ -335,6 +326,9 @@ FrameworkBundle
335326
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ValidateWorkflowsPass` class
336327
has been removed. Use the `Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass`
337328
class instead.
329+
< F438 /td>330+
* The `Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory` class has been removed.
331+
Use `Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
338332

339333
HttpFoundation
340334
--------------

src/Symfony/Bundle/FrameworkBundle/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
-----

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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+
->exp F987 ects($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

src/Symfony/Bundle/FrameworkBundle/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 <kris@symfony.com>
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

src/Symfony/Bundle/FrameworkBundle/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": {

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added `AddValidatorInitializersPass`
88
* added `AddConstraintValidatorsPass`
9+
* added `ContainerConstraintValidatorFactory`
910

1011
3.2.0
1112
-----
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;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
16+
use Symfony\Component\Validator\Exception\ValidatorException;
17+
18+
/**
19+
* Uses a service container to create constraint validators.
20+
*
21+
* @author Kris Wallsmith <kris@symfony.com>
22+
*/
23+
class ContainerConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
24+
{
25+
private $container;
26+
private $validators;
27+
28+
public function __construct(ContainerInterface $container)
29+
{
30+
$this->container = $container;
31+
$this->validators = array();
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*
37+
* @throws ValidatorException When the validator class does not exist
38+
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
39+
*/
40+
public function getInstance(Constraint $constraint)
41+
{
42+
$name = $constraint->validatedBy();
43+
44+
if (!isset($this->validators[$name])) {
45+
if ($this->container->has($name)) {
46+
$this->validators[$name] = $this->container->get($name);
47+
} else {
48+
if (!class_exists($name)) {
49+
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)));
50+
}
51+
52+
$this->validators[$name] = new $name();
53+
}
54+
}
55+
56+
if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
57+
throw new UnexpectedTypeException($this->validators[$name], ConstraintValidatorInterface::class);
58+
}
59+
60+
return $this->validators[$name];
61+
}
62+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Container;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\Validator\Constraint;
18+
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
19+
use Symfony\Component\Validator\ConstraintValidator;
20+
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
21+
22+
class ContainerConstraintValidatorFactoryTest extends TestCase
23+
{
24+
public function testGetInstanceCreatesValidator()
25+
{
26+
$class = get_class($this->getMockForAbstractClass(ConstraintValidator::class));
27+
28+
$constraint = $this->getMockBuilder(Constraint::class)->getMock();
29+
$constraint
30+
->expects($this->once())
31+
->method('validatedBy')
32+
->will($this->returnValue($class));
33+
34+
$factory = new ContainerConstraintValidatorFactory(new Container());
35+
$this->assertInstanceOf($class, $factory->getInstance($constraint));
36+
}
37+
38+
public function testGetInstanceReturnsExistingValidator()
39+
{
40+
$factory = new ContainerConstraintValidatorFactory(new Container());
41+
$v1 = $factory->getInstance(new BlankConstraint());
42+
$v2 = $factory->getInstance(new BlankConstraint());
43+
$this->assertSame($v1, $v2);
44+
}
45+
46+
public function testGetInstanceReturnsService()
47+
{
48+
$service = 'validator_constraint_service';
49+
$validator = $this->getMockForAbstractClass(ConstraintValidator::class);
50+
51+
// mock ContainerBuilder b/c it implements TaggedContainerInterface
52+
$container = $this->getMockBuilder(ContainerBuilder::class)->setMethods(array('get', 'has'))->getMock();
53+
$container
54+
->expects($this->once())
55+
->method('get')
56+
->with($service)
57+
->willReturn($validator);
58+
$container
59+
->expects($this->once())
60+
->method('has')
61+
->with($service)
62+
->willReturn(true);
63+
64+
$constraint = $this->getMockBuilder(Constraint::class)->getMock();
65+
$constraint
66+
->expects($this->once())
67+
->method('validatedBy')
68+
->will($this->returnValue($service));
69+
70+
$factory = new ContainerConstraintValidatorFactory($container);
71+
$this->assertSame($validator, $factory->getInstance($constraint));
72+
}
73+
74+
/**
75+
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
76+
*/
77+
public function testGetInstanceInvalidValidatorClass()
78+
{
79+
$constraint = $this->getMockBuilder(Constraint::class)->getMock();
80+
$constraint
81+
->expects($this->once())
82+
->method('validatedBy')
83+
->will($this->returnValue('Fully\\Qualified\\ConstraintValidator\\Class\\Name'));
84+
85+
$factory = new ContainerConstraintValidatorFactory(new Container());
86+
$factory->getInstance($constraint);
87+
}
88+
}

0 commit comments

Comments
 (0)
0