8000 [FrameworkBundle][Validator] Deprecate passing validator instances/aliases over using the service locator by ogizanagi · Pull Request #22808 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][Validator] Deprecate passing validator instances/aliases over using the service locator #22808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[FrameworkBundle][Validator] Deprecate passing validator instances/al…
…iases over using the service locator
  • Loading branch information
ogizanagi committed May 21, 2017
commit df747ce43f9b5d16499186c3e1007b09b586779f
4 changes: 4 additions & 0 deletions UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ FrameworkBundle
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` class instead.

* Passing an array of validators or validator aliases as the second argument of
`ConstraintValidatorFactory::__construct()` is deprecated since 3.3 and will
be removed in 4.0. Use the service locator instead.

HttpFoundation
--------------

Expand Down
4 changes: 4 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ FrameworkBundle

* Extending `ConstraintValidatorFactory` is not supported anymore.

* Passing an array of validators or validator aliases as the second argument of
`ConstraintValidatorFactory::__construct()` has been removed.
Use the service locator instead.

* Class parameters related to routing have been removed
* router.options.generator_class
* router.options.generator_base_class
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ CHANGELOG
`Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass` instead
* Deprecated `ValidateWorkflowsPass`, use
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead
* Deprecated `ConstraintValidatorFactory::__construct()` second argument.

3.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
use Symfony\Component\Validator\ConstraintValidator;

class ConstraintValidatorFactoryTest extends TestCase
{
Expand All @@ -41,6 +44,38 @@ public function testGetInstanceReturnsExistingValidator()
}

public function testGetInstanceReturnsService()
{
$service = 'validator_constraint_service';
$validator = $this->getMockForAbstractClass(ConstraintValidator::class);

// mock ContainerBuilder b/c it implements TaggedContainerInterface
$container = $this->getMockBuilder(ContainerBuilder::class)->setMethods(array('get', 'has'))->getMock();
$container
->expects($this->once())
->method('get')
->with($service)
->willReturn($validator);
$container
->expects($this->once())
->method('has')
->with($service)
->willReturn(true);

$constraint = $this->getMockBuilder(Constraint::class)->getMock();
$constraint
->expects($this->once())
->method('validatedBy')
->will($this->returnValue($service));

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

/**
* @group legacy
* @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.
*/
public function testGetInstanceReturnsServiceWithAlias()
{
$service = 'validator_constraint_service';
$alias = 'validator_constraint_alias';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
protected $container;
protected $validators;

/**
* Constructor.
*
* @param ContainerInterface $container The service container
* @param array $validators An array of validators
*/
public function __construct(ContainerInterface $container, array $validators = array())
public function __construct(ContainerInterface $container, array $validators = null)
{
$this->container = $container;

if (null !== $validators) {
@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);
} else {
$validators = array();
}

$this->validators = $validators;
}

Expand Down Expand Up @@ -82,6 +83,7 @@ public function getInstance(Constraint $constraint)
$this->validators[$name] = new $name();
}
} elseif (is_string($this->validators[$name])) {
// To be removed in 4.0
$this->validators[$name] = $this->container->get($this->validators[$name]);
}

Expand Down
0