-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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 1 commit
1088d62
7505eb8
2928fbe
7518b5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…sPass
- Loading branch information
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,17 +37,12 @@ | |
* } | ||
* | ||
* @author Kris Wallsmith <kris@symfony.com> | ||
* | ||
* @final since version 3.3 | ||
*/ | ||
class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface | ||
{ | ||
/** | ||
* @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below | ||
*/ | ||
protected $validators; | ||
|
||
/** | ||
|
@@ -77,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]); | ||
} | ||
|
@@ -92,52 +91,4 @@ public function getInstance(Constraint $constraint) | |
|
||
return $this->validators[$name]; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function __get($name) | ||
{ | ||
if ('validators' === $name || 'container' === $name) { | ||
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); | ||
} | ||
|
||
return $this->$name; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
if ('validators' === $name || 'container' === $name) { | ||
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); | ||
} | ||
|
||
$this->$name = $value; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function __isset($name) | ||
{ | ||
if ('validators' === $name || 'container' === $name) { | ||
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); | ||
} | ||
|
||
return isset($this->$name); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function __unset($name) | ||
{ | ||
if ('validators' === $name || 'container' === $name) { | ||
@trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); | ||
} | ||
|
||
unset($this->$name); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
Abstract services should be ignored ie you should "continue". Note that this applies to many other cases in the code base: many passes throw on abstract services, but now that tags can be inherited in child definitions, we should just "continue" instead. Deserves another PR if you'd like to do it :)
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.
Seems like
ContainerBuilder::findTaggedServiceIds()
deserves a new argument ;)I can't do it this week so I opened #21761 in case someone is interested.