-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Use a service locator in AddConstraintValidatorsPass #21730
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
Changes from all commits
1088d62
7505eb8
2928fbe
7518b5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
|
||
namespace Symfony\Bundle\FrameworkBundle\Validator; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Psr\Container\ContainerInterface; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given $container is protected I think that's a BC break There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's acceptable, the methods defined are the same and it will only fail if someone checks its interface which is unlikely. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure. ContainerInterface contains more methods than the Psr one, calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note also that adding deprecations for using experimental features has been rejected in #21625. I would wait for 4.0 on this one (that's why I did not proposed it), let's see what other think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. about deprecating in this PR, see #21625 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've an idea of a BC layer (including deprecations but not about experimental features), I'll update the pr soon. |
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; | ||
use Symfony\Component\Validator\ConstraintValidatorInterface; | ||
|
@@ -37,6 +37,8 @@ | |
* } | ||
* | ||
* @author Kris Wallsmith <kris@symfony.com> | ||
* | ||
* @final since version 3.3 | ||
*/ | ||
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface | ||
{ | ||
|
@@ -70,11 +72,15 @@ public function getInstance(Constraint $constraint) | |
$name = $constraint->validatedBy(); | ||
|
||
if (!isset($this->validators[$name])) { | ||
if (!class_exists($name)) { | ||
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))); | ||
} | ||
if ($this->container->has($name)) { | ||
$this->validators[$name] = $this->container->get($name); | ||
} else { | ||
if (!class_exists($name)) { | ||
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))); | ||
} | ||
|
||
$this->validators[$name] = new $name(); | ||
$this->validators[$name] = new $name(); | ||
} | ||
} elseif (is_string($this->validators[$name])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? In case what is returned by the container is a string ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Above it's needed because we want to know when to fallback to manual instantiating, here it just depends of the exception we want, with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
$this->validators[$name] = $this->container->get($this->validators[$name]); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
broken here, the constructor expects an argument. See #21821