Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a76669

Browse files
feature #22808 [FrameworkBundle][Validator] Deprecate passing validator instances/aliases over using the service locator (ogizanagi)
This PR was merged into the 3.3 branch. Discussion ---------- [FrameworkBundle][Validator] Deprecate passing validator instances/aliases over using the service locator | Q | A | ------------- | --- | Branch? | 3.3 <!-- see comment below --> | Bug fix? | no | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | yes <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #22800 (comment) <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A Commits ------- df747ce [FrameworkBundle][Validator] Deprecate passing validator instances/aliases over using the service locator
2 parents cbd2561 + df747ce commit 4a76669

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

UPGRADE-3.3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ FrameworkBundle
244244
class has been deprecated and will be removed in 4.0. Use the
245245
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` class instead.
246246

247+
* Passing an array of validators or validator aliases as the second argument of
248+
`ConstraintValidatorFactory::__construct()` is deprecated since 3.3 and will
249+
be removed in 4.0. Use the service locator instead.
250+
247251
HttpFoundation
248252
--------------
249253

UPGRADE-4.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ FrameworkBundle
299299

300300
* Extending `ConstraintValidatorFactory` is not supported anymore.
301301

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+
302306
* Class parameters related to routing have been removed
303307
* router.options.generator_class
304308
* router.options.generator_base_class

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ CHANGELOG
4949
`Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` instead
5050
* Deprecated `ValidateWorkflowsPass`, use
5151
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead
52+
* Deprecated `ConstraintValidatorFactory::__construct()` second argument.
5253

5354
3.2.0
5455
-----

src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory;
1616
use Symfony\Component\DependencyInjection\Container;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\Validator\Constraint;
1719
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
20+
use Symfony\Component\Validator\ConstraintValidator;
1821

1922
class ConstraintValidatorFactoryTest extends TestCase
2023
{
@@ -41,6 +44,38 @@ public function testGetInstanceReturnsExistingValidator()
4144
}
4245

4346
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 ConstraintValidatorFactory($container);
71+
$this->assertSame($validator, $factory->getInstance($constraint));
72+
}
73+
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+
*/
78+
public function testGetInstanceReturnsServiceWithAlias()
4479
{
4580
$service = 'validator_constraint_service';
4681
$alias = 'validator_constraint_alias';

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
4545
protected $container;
4646
protected $validators;
4747

48-
/**
49-
* Constructor.
50-
*
51-
* @param ContainerInterface $container The service container
52-
* @param array $validators An array of validators
53-
*/
54-
public function __construct(ContainerInterface $container, array $validators = array())
48+
public function __construct(ContainerInterface $container, array $validators = null)
5549
{
5650
$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+
}
57+
5758
$this->validators = $validators;
5859
}
5960

@@ -82,6 +83,7 @@ public function getInstance(Constraint $constraint)
8283
$this->validators[$name] = new $name();
8384
}
8485
} elseif (is_string($this->validators[$name])) {
86+
// To be removed in 4.0
8587
$this->validators[$name] = $this->container->get($this->validators[$name]);
8688
}
8789

0 commit comments

Comments
 (0)