-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Related to #21553. |
2ca9847
to
5fc62de
Compare
* @param iterable[] $typeExtensionServices | ||
* @param iterable[] $guesserServices | ||
*/ | ||
public function __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
@@ -30,7 +30,7 @@ | |||
|
|||
<!-- DependencyInjectionExtension --> | |||
<service id="form.extension" class="Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension" public="false"> | |||
<argument type="service" id="service_container" /> | |||
<argument type="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!
} | ||
|
||
$definition->replaceArgument(0, new ServiceLocatorArgument($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't guesserServices
be an IteratorArgument
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 the DependencyInjectionExtension::__construct()
method signature would be:
public function __construct(ContainerInterface $typesLocator, ContainerInterface $typeExtensionsLocator, iterator $guessers)
Might not be the easiest for BC though.
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's ServiceLocator
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 register FormTypeGuesserChain
as a service, pass it guessers as an IteratorArgument and deprecate both $guesserServices
and $guesserServiceIds
in favor of a FormTypeGuesserInterface $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 need FormTypeGuesserChain
to be a service, but simply pass it the guessers iterator (so allow FormTypeGuesserChain
to accept iterable).
IMHO the extension is in charge of building the whole thing and the use of the FormTypeGuesserChain
is an implementation detail. There is no need to pass the guesser chain as a dependency.
There is an issue with FormTypeGuesserChain
, 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, as FormExtensionInterface::getTypeGuesser()
is called in FormRegistryInterface::getTypeGuesser()
, called in Formfactory::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 one FormTypeGuesserInterface $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 the assertSame
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.
} | ||
|
||
$definition->replaceArgument(0, new ServiceLocatorArgument($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 the DependencyInjectionExtension::__construct()
method signature would be:
public function __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 need FormTypeGuesserChain
to be a service, but simply pass it the guessers iterator (so allow FormTypeGuesserChain
to accept iterable).
IMHO the extension is in charge of building the whole thing and the use of the FormTypeGuesserChain
is an implementation detail. There is no need to pass the guesser chain as a dependency.
There is an issue with FormTypeGuesserChain
, 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, as FormExtensionInterface::getTypeGuesser()
is called in FormRegistryInterface::getTypeGuesser()
, called in Formfactory::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
to
27e2a1a
Compare
), $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 for assertInstanceOf
as assertEquals
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
to
e4beebf
Compare
// Support type access by FQCN | ||
$types[$serviceDefinition->getClass()] = $serviceId; | ||
// Add form type service to the service locator | ||
$servicesMap[$serviceId] = new Reference($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
to
5c0a576
Compare
@nicolas-grekas @HeahDude comments were addressed. I'll squash after your new view. |
0753fcd
to
ec76718
Compare
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.
👍
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 |
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
@@ -25,8 +25,9 @@ | |||
}, | |||
"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
to
a354cac
Compare
@stof @webmozart can I have your review please? |
</service> | ||
|
||
<!-- ValidatorTypeGuesser --> | ||
<service id="form.type_guesser.validator" class="Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser"> | ||
<service id="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 added 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.
👍 (needs rebase)
… so that form types can be made private at some point.
a354cac
to
600e75c
Compare
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.
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 theForm
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 thedeprecated.form.registry
anddeprecated.form.registry.csrf
fake services that will be removed in Symfony 4.0.