Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Form] allow form types + form type extensions + form type guessers to be private services#21690
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
hhamon commentedFeb 20, 2017
Related to#21553. |
2ca9847 to5fc62deCompare| * @param iterable[] $typeExtensionServices | ||
| * @param iterable[] $guesserServices | ||
| */ | ||
| publicfunction__construct(ContainerInterface$typeContainer,array$typeExtensionServices,array$guesserServices,array$guesserServiceIds =null) |
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.
I would not rename$container to reduce the diff
| <!-- DependencyInjectionExtension--> | ||
| <serviceid="form.extension"class="Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension"public="false"> | ||
| <argumenttype="service"id="service_container" /> | ||
| <argumenttype="service-locator" /> |
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.
According to the current deprecation the fourth argument should be removed
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.
Yep good catch!
| $servicesMap[$serviceId] =newReference($serviceId); | ||
| } | ||
| $definition->replaceArgument(0,newServiceLocatorArgument($servicesMap)); |
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.
I'm wondering if mixing types/guessers/extensions in the same locator is a good thing here, as we have still 3 constructor arguments that's a bit confusing to me.
Instead, couldn'tguesserServices be anIteratorArgument of guessers? It seems we only iterate over them to build a chain one, no need for having them identified right?
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.
Yes It's on the way! This is what I'm currently doing :)
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.
Why not 2 service locators, and one iterator?
So theDependencyInjectionExtension::__construct() method signature would be:
publicfunction __construct(ContainerInterface$typesLocator,ContainerInterface$typeExtensionsLocator,iterator$guessers)
Might not be the easiest for BC though.
ogizanagiFeb 20, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
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.
Oh, actually$typeExtensionServiceIds cannot easily be a locator, as it is a map of multiple type extension services indexed by type. Symfony'sServiceLocator does not handle collection of services as values.
But couldn't it be a collection of iterables indexed by type?
Edit: actually in order to keep things simple, I think the current implementation is probably safer.
| * | ||
| * @param ContainerInterface $typeContainer | ||
| * @param iterable[] $typeExtensionServices | ||
| * @param iterable[] $guesserServices |
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.
iterable[] make me understand that the value should be an array of iterables.iterable|FormTypeGuesserInterface[] instead?
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.
Yes it can be an array or an iterator.
| $service =$this->typeContainer->get($serviceId); | ||
| } | ||
| $guessers[] =$service; |
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.
In fact, guessers do not need to be instantiated at this point.
I think we should registerFormTypeGuesserChain as a service, pass it guessers as an IteratorArgument and deprecate both$guesserServices and$guesserServiceIds in favor of aFormTypeGuesserInterface $guesser here, even more lazy. Not sure how it would be hard to keep it BC though, maybe deprecating the whole class would be simpler.
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.
Yes that's probably a good thing! I'll check this tomorrow with@nicolas-grekas.
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.
Not sure we needFormTypeGuesserChain to be a service, but simply pass it the guessers iterator (so allowFormTypeGuesserChain to accept iterable).
IMHO the extension is in charge of building the whole thing and the use of theFormTypeGuesserChain is an implementation detail. There is no need to pass the guesser chain as a dependency.
There is an issue withFormTypeGuesserChain, though... its constructor. It iterates over the guessers and this would trigger the loading too soon anyway.
I'm not sure lazy loading here is a big deal though, asFormExtensionInterface::getTypeGuesser() is called inFormRegistryInterface::getTypeGuesser(), called inFormfactory::createBuilderForProperty() and is used directly to guess types by iterating over every guessers.
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.
IMHO the extension is in charge of building the whole thing
Too many responsibilities to me.
and the use of the FormTypeGuesserChain is an implementation detail. There is no need to pass the guesser chain as a dependency.
The fact the guessers are multiple services is a detail IMO. At the end we have only oneFormTypeGuesserInterface $guesser, the class could be usable with an iterable or a simple guesser, which looks better to me. Also it'd be consistent with other places in the core.
There is an issue with FormTypeGuesserChain, though... its constructor. It iterates over the guessers and this would trigger the loading too soon anyway.
Just validation/merge, can be moved at compile time I think.
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.
I thought about deprecating the whole class and replacing it with a new one. But after discussing with@nicolas-grekas it requires too much efforts then to maintain both implementations. This is why we choose to adapt the class itself. One class to maintain and one tests suite. It's just a bit more difficult to read and understand the code at the moment because of the mixed two layers.
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.
It's just a bit more difficult to read and understand the code at the moment because of the mixed two layers.
Indeed :)
I'm unable to say if the BC layer is correct right now, but the approach looks better!
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.
Unit tests prove the BC layer is guaranteed ;)
| __CLASS__.'_Type1' =>'my.type1', | ||
| __CLASS__.'_Type2' =>'my.type2', | ||
| ),$extDefinition->getArgument(1)); | ||
| $this->assertSame( |
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.
This change should be reverted I guess.
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.
Why?
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.
The code style and theassertSame change isn't related to this PR, right?
| 'my.guesser1', | ||
| 'my.guesser2', | ||
| ),$extDefinition->getArgument(3)); | ||
| $this->assertSame( |
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.
Same here.
| $servicesMap[$serviceId] =newReference($serviceId); | ||
| } | ||
| $definition->replaceArgument(0,newServiceLocatorArgument($servicesMap)); |
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.
Why not 2 service locators, and one iterator?
So theDependencyInjectionExtension::__construct() method signature would be:
publicfunction __construct(ContainerInterface$typesLocator,ContainerInterface$typeExtensionsLocator,iterator$guessers)
Might not be the easiest for BC though.
| $service =$this->typeContainer->get($serviceId); | ||
| } | ||
| $guessers[] =$service; |
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.
Not sure we needFormTypeGuesserChain to be a service, but simply pass it the guessers iterator (so allowFormTypeGuesserChain to accept iterable).
IMHO the extension is in charge of building the whole thing and the use of theFormTypeGuesserChain is an implementation detail. There is no need to pass the guesser chain as a dependency.
There is an issue withFormTypeGuesserChain, though... its constructor. It iterates over the guessers and this would trigger the loading too soon anyway.
I'm not sure lazy loading here is a big deal though, asFormExtensionInterface::getTypeGuesser() is called inFormRegistryInterface::getTypeGuesser(), called inFormfactory::createBuilderForProperty() and is used directly to guess types by iterating over every guessers.
| __CLASS__.'_Type1' =>'my.type1', | ||
| __CLASS__.'_Type2' =>'my.type2', | ||
| ),$extDefinition->getArgument(1)); | ||
| $this->assertSame( |
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.
Should be reverted too I think.
| 'my.guesser2', | ||
| ),$extDefinition->getArgument(3)); | ||
| $this->assertSame( | ||
| array( |
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.
same
deb23a2 to27e2a1aCompare| ),$extDefinition->getArgument(1)); | ||
| $locator =$extDefinition->getArgument(0); | ||
| $this->assertInstanceOf(ServiceLocatorArgument::class,$locator); |
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.
No need forassertInstanceOf asassertEquals fails on an type-class mismatch.
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.
Fixed. thanks.
ab23d37 toe4beebfCompare| // Support type access by FQCN | ||
| $types[$serviceDefinition->getClass()] =$serviceId; | ||
| // Add form type service to the service locator | ||
| $servicesMap[$serviceId] =newReference($serviceId); |
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.
this is wrong. Form types are accessible only by class, not by service id
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.
Ok thanks!
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.
Fixed.
0702a5b to5c0a576Comparehhamon commentedFeb 27, 2017
@nicolas-grekas@HeahDude comments were addressed. I'll squash after your new view. |
0753fcd toec76718Compare
HeahDude left a comment
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.
👍
nicolas-grekas left a comment
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.
👍
| * Constructor. | ||
| * | ||
| * @param ContainerInterface $typeContainer | ||
| * @param iterable[] $typeExtensionServices |
nicolas-grekasFeb 27, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
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.
iterable[] is more precise: array of iterables
| }, | ||
| "require-dev": { | ||
| "doctrine/collections":"~1.0", | ||
| "psr/container":"^1.0", |
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.
not required (as symfony/dependency-injection already requires it)
16cab61 toa354cacComparehhamon commentedFeb 27, 2017
@stof@webmozart can I have your review please? |
| <!-- ValidatorTypeGuesser--> | ||
| <serviceid="form.type_guesser.validator"class="Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser"> | ||
| <serviceid="form.type_guesser.validator"class="Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser"public="false"> |
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.
see PR description for why this is not a BC break (same for all addedpublic="false")
chalasr left a comment
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.
👍 (needs rebase)
… so that form types can be made private at some point.
a354cac to600e75cComparefabpot commentedFeb 28, 2017
Thank you@hhamon. |
…type guessers to be private services (hhamon)This PR was merged into the 3.3-dev branch.Discussion----------[Form] allow form types + form type extensions + form type guessers to be private services| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | ~| License | MIT| Doc PR | ~This pull request is about making internal form services (aka form types, form type extensions and form type guessers) private. They used to be public until Symfony 3.2 for one valid reason: lazyness. However, Symfony 3.3 now comes with built-in mechanism to support effective lazy loading of private services with service locators and proxies.This PR makes the `DependencyInjectionExtension` class of the `Form` component leverage these new DI component mechanisms. Form types, form type extensions and form type guessers can now be declared private as a best practice. We decided to make these services private as of Symfony 3.3 and of course it would break BC. But this PR introduces a BC layer using a Symfony trick to keep internal form services public. The service container currently has a known issue where private services are not really private if they're referenced by at least two other services in the container. We use this trick to maintain the legacy services public even though the new API relies on private ones. This trick is done thanks to the `deprecated.form.registry` and `deprecated.form.registry.csrf` fake services that will be removed in Symfony 4.0.Commits-------600e75c [Form] use new service locator in DependencyInjectionExtension class, so that form types can be made private at some point.
Uh oh!
There was an error while loading.Please reload this page.
This pull request is about making internal form services (aka form types, form type extensions and form type guessers) private. They used to be public until Symfony 3.2 for one valid reason: lazyness. However, Symfony 3.3 now comes with built-in mechanism to support effective lazy loading of private services with service locators and proxies.
This PR makes the
DependencyInjectionExtensionclass of theFormcomponent leverage these new DI component mechanisms. Form types, form type extensions and form type guessers can now be declared private as a best practice. We decided to make these services private as of Symfony 3.3 and of course it would break BC. But this PR introduces a BC layer using a Symfony trick to keep internal form services public. The service container currently has a known issue where private services are not really private if they're referenced by at least two other services in the container. We use this trick to maintain the legacy services public even though the new API relies on private ones. This trick is done thanks to thedeprecated.form.registryanddeprecated.form.registry.csrffake services that will be removed in Symfony 4.0.