8000 [Validator] Changed ValidatorBuilder to always use LegacyExecutionCon… · symfony/symfony@274d4e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 274d4e6

Browse files
committed
[Validator] Changed ValidatorBuilder to always use LegacyExecutionContext
This is necessary because, until Symfony 3.0, constraint validators will continue to rely on the old context methods in order to be backwards compatible.
1 parent 38e26fb commit 274d4e6

File tree

6 files changed

+36
-25
lines changed

6 files changed

+36
-25
lines changed

src/Symfony/Component/Validator/Context/LegacyExecutionContext.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraints\Valid;
1717
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1818
use Symfony\Component\Validator\Group\GroupManagerInterface;
19+
use Symfony\Component\Validator\MetadataFactoryInterface;
1920
use Symfony\Component\Validator\Validator\ValidatorInterface;
2021
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
2122

@@ -30,33 +31,29 @@
3031
*/
3132
class LegacyExecutionContext extends ExecutionContext
3233
{
34+
/**
35+
* @var MetadataFactoryInterface
36+
*/
37+
private $metadataFactory;
38+
3339
/**
3440
* Creates a new context.
3541
*
36-
* This constructor ensures that the given validator implements the
37-
* deprecated {@link \Symfony\Component\Validator\ValidatorInterface}. If
38-
* it does not, an {@link InvalidArgumentException} is thrown.
39-
*
4042
* @see ExecutionContext::__construct()
4143
*
4244
* @internal Called by {@link LegacyExecutionContextFactory}. Should not be used
4345
* in user code.
4446
*/
45-
public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
47+
public function __construct(ValidatorInterface $validator, $root, MetadataFactoryInterface $metadataFactory, TranslatorInterface $translator, $translationDomain = null)
4648
{
47-
if (!$validator instanceof LegacyValidatorInterface) {
48-
throw new InvalidArgumentException(
49-
'The validator passed to LegacyExecutionContext must implement '.
50-
'"Symfony\Component\Validator\ValidatorInterface".'
51-
);
52-
}
53-
5449
parent::__construct(
5550
$validator,
5651
$root,
5752
$translator,
5853
$translationDomain
5954
);
55+
56+
$this->metadataFactory = $metadataFactory;
6057
}
6158

6259
/**
@@ -158,6 +155,6 @@ public function validateValue($value, $constraints, $subPath = '', $groups = nul
158155
*/
159156
public function getMetadataFactory()
160157
{
161-
return $this->getValidator()->getMetadataFactory();
158+
return $this->metadataFactory;
162159
}
163160
}

src/Symfony/Component/Validator/Context/LegacyExecutionContextFactory.php

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

1414
use Symfony\Component\Translation\TranslatorInterface;
1515
use Symfony\Component\Validator\Group\GroupManagerInterface;
16+
use Symfony\Component\Validator\MetadataFactoryInterface;
1617
use Symfony\Component\Validator\Validator\ValidatorInterface;
1718

1819
/**
@@ -26,6 +27,11 @@
2627
*/
2728
class LegacyExecutionContextFactory implements ExecutionContextFactoryInterface
2829
{
30+
/**
31+
* @var MetadataFactoryInterface
32+
*/
33+
private $metadataFactory;
34+
2935
/**
3036
* @var TranslatorInterface
3137
*/
@@ -39,13 +45,15 @@ class LegacyExecutionContextFactory implements ExecutionContextFactoryInterface
3945
/**
4046
* Creates a new context factory.
4147
*
42-
* @param TranslatorInterface $translator The translator
43-
* @param string|null $translationDomain The translation domain to
44-
* use for translating
45-
* violation messages
48+
* @param MetadataFactoryInterface $metadataFactory The metadata factory
49+
* @param TranslatorInterface $translator The translator
50+
* @param string|null $translationDomain The translation domain
51+
* to use for translating
52+
* violation messages
4653
*/
47-
public function __construct(TranslatorInterface $translator, $translationDomain = null)
54+
public function __construct(MetadataFactoryInterface $metadataFactory, TranslatorInterface $translator, $translationDomain = null)
4855
{
56+
$this->metadataFactory = $metadataFactory;
4957
$this->translator = $translator;
5058
$this->translationDomain = $translationDomain;
5159
}
@@ -58,6 +66,7 @@ public function createContext(ValidatorInterface $validator, $root)
5866
return new LegacyExecutionContext(
5967
$validator,
6068
$root,
69+
$this->metadataFactory,
6170
$this->translator,
6271
$this->translationDomain
6372
);

src/Symfony/Component/Validator/Tests/Validator/LegacyValidator2Dot5ApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function setUp()
3434

3535
protected function createValidator(MetadataFactoryInterface $metadataFactory)
3636
{
37-
$contextFactory = new LegacyExecutionContextFactory(new DefaultTranslator());
37+
$contextFactory = new LegacyExecutionContextFactory($metadataFactory, new DefaultTranslator());
3838

3939
return new LegacyValidator($contextFactory, $metadataFactory, new ConstraintValidatorFactory());
4040
}

src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorLegacyApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function setUp()
3434

3535
protected function createValidator(MetadataFactoryInterface $metadataFactory)
3636
{
37-
$contextFactory = new LegacyExecutionContextFactory(new DefaultTranslator());
37+
$contextFactory = new LegacyExecutionContextFactory($metadataFactory, new DefaultTranslator());
3838

3939
return new LegacyValidator($contextFactory, $metadataFactory, new ConstraintValidatorFactory());
4040
}

src/Symfony/Component/Validator/Validator/LegacyValidator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
/**
2020
* A validator that supports both the API of Symfony < 2.5 and Symfony 2.5+.
2121
*
22+
* This class is incompatible with PHP versions < 5.3.9, because it implements
23+
* two different interfaces specifying the same method validate():
24+
*
25+
* - {@link \Symfony\Component\Validator\ValidatorInterface}
26+
* - {@link \Symfony\Component\Validator\Validator\ValidatorInterface}
27+
*
28+
* In PHP versions prior to 5.3.9, either use {@link RecursiveValidator} or the
29+
* deprecated class {@link \Symfony\Component\Validator\Validator} instead.
30+
*
2231
* @since 2.5
2332
* @author Bernhard Schussek <bschussek@gmail.com>
2433
*

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,7 @@ public function getValidator()
415415
return new ValidatorV24($metadataFactory, $validatorFactory, $translator, $this->translationDomain, $this->initializers);
416416
}
417417

418-
if (Validation::API_VERSION_2_5 === $apiVersion) {
419-
$contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
420-
} else {
421-
$contextFactory = new LegacyExecutionContextFactory($translator, $this->translationDomain);
422-
}
418+
$contextFactory = new LegacyExecutionContextFactory($metadataFactory, $translator, $this->translationDomain);
423419

424420
if (Validation::API_VERSION_2_5 === $apiVersion) {
425421
$nodeTraverser = new NonRecursiveNodeTraverser($metadataFactory);

0 commit comments

Comments
 (0)
0