-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] add support for prioritizing form type extension tags #19790
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
$definition->replaceArgument(2, $typeExtensions); | ||
$sortedTypeExtensions = []; | ||
foreach ($typeExtensions as $extendedType => $extensionsWithPriority) { | ||
usort($extensionsWithPriority, function (array $a, array $b) { |
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.
You can use the https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php, it will do the job for you :)
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.
Ah good to know 😊
But that would globally sort all form extensions. We only want to sort per extended type though? 😉
79d2af6
to
7d7c957
Compare
Actually I just realized that the http://www.php.net/manual/en/function.usort.php
This means we would have to sort differently to be 100% sure there is no BC break? |
7d7c957
to
3d0bda3
Compare
What about using the |
It seems that also |
@dmaicher There is no stable sorting by default either as it depends on the order of the services registration (which is not predictable unless using a custom pass, not sure it is worth it in that case though), and this is partially responsible for the issue you're trying to solve.
Currently form type extensions cannot rely on other extensions, at least handling priorities will solve this. |
@HeahDude But as you said currently without priorities it depends on the order of service registration. If we introduce a non-stable sorting then it might be a BC break though as the order of service registration is not 100% guaranteed to be used anymore for the form extensions (when all extensions have equal priority 0)? |
What I'm saying is that this order is not reliable already, so it is an acceptable BC break IMO because it does not change anything unless some extensions rely on each other, which is buggy right now unless using your new feature :) |
@HeahDude Ok now I see what you mean 😊 If this "BC break" is acceptable then I can surely change it 😄 Other opinions on that? |
$sortedTypeExtensions = array(); | ||
foreach ($typeExtensions as $extendedType => $extensionsWithPriority) { | ||
usort($extensionsWithPriority, function (array $a, array $b) { | ||
return $a[1] > $b[1] ? -1 : 1; |
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 never returns 0
, strange, why that?
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.
Changed in favour of \SplPriorityQueue
now 😉
You can still use the trait as inspiration and maybe play around with the existing function. |
c982f84
to
f9f8b57
Compare
I changed it to use If all agree this feature is useful as is I could prepare a PR for the docs? |
bb0e184
to
7570bbb
Compare
Relying on the order is it really something we want to support? Do we have enough real-world use cases? Nobody ever had this issue in the last 5 years, so I'm wondering if we really need to support this. |
Form type extensions are meant to decouple specific configuration, thus may add options to resolve. So on one hand I agree, this is maybe covering edge cases where one need to handle that kind of option globally. Actually resolving the form configuration is why we need this composite pattern, and the process works well because it is mainly based on inheritance which do keep an order, so if we can support it for extensions too, I guess we should. Good things:
Bad things:
Anyway thank you @dmaicher for proposing this feature. |
I would also agree with @HeahDude. I don't see any disadvantages of having this really small (+10, -3 LOC excluding tests) feature. Sure maybe not so many people will use it but currently the behaviour is a bit unpredictable for the order of form type extensions which is quite bad imho 😢 |
@dmaicher After re-reading this, I still think you should use the trait and change only this line, you can then keep the same logic as before to handle the indexation by extended type but you'll create the priority job only once which will also use only one instance one I think that is is a good thing too because in the future if there is some issue about extensions overriding each other we will be able to solve it thanks to this feature. |
with this foreach ($this->findAndSortTaggedServices('form.type_extension', $container) as $reference) {
...
} I just have a sorted list of service But I think you are right about sorting all extensions globally though 😊 Might make more sense. |
@dmaicher : The |
7570bbb
to
ab302dc
Compare
ab302dc
to
e3aa701
Compare
$serviceDefinition = $container->getDefinition($serviceId); | ||
if (!$serviceDefinition->isPublic()) { | ||
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type extensions are lazy-loaded.', $serviceId)); | ||
} | ||
|
||
$tag = $serviceDefinition->getTag('form.type_extension'); | ||
|
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 empty line is not needed :)
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.
👍
e3aa701
to
aac8a3f
Compare
@dmaicher can you please update the CHANGELOG file of the component? |
aac8a3f
to
ce051af
Compare
@nicolas-grekas done 😉 Let me know if I need to change anything else |
@dmaicher Can you please update the description too (tests are passing now :) and submit a PR in the docs? Thanks again! |
@HeahDude done 😉 |
* @dataProvider addTaggedTypeExtensionsDataProvider | ||
* | ||
* @param array $extensions | ||
* @param array $expectedRegisteredExtensions |
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 phpdoc for params is needless
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 removing it 👍
👍 Status: Reviewed |
ce051af
to
a3db5f0
Compare
Thank you @dmaicher. |
…pe extension tags (dmaicher) This PR was merged into the 3.2-dev branch. Discussion ---------- [FrameworkBundle] add support for prioritizing form type extension tags | Q | A | ------------- | --- | Branch? | "master" | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19735 | License | MIT | Doc PR | symfony/symfony-docs#6958 This PR proposes to add support for `priority` on `form.type_extension` dependecyinjection tags to enable sorting/prioritizing form type extensions. Issue was mentioned here: #19735 Commits ------- a3db5f0 [FrameworkBundle] add support for prioritizing form type extension tags
…e extension tags (dmaicher) This PR was squashed before being merged into the master branch (closes #6958). Discussion ---------- [FrameworkBundle] add support for prioritizing form type extension tags Doc PR for symfony/symfony#19790 Commits ------- 0e0bdc3 [FrameworkBundle] add support for prioritizing form type extension tags
A bit late to the party here, but I absolutely think this was a BC break. See dunglas/DunglasAngularCsrfBundle#39 The entire bundle's csrf protection form extension is broken because it now loads before the core csrf extension -- it disables csrf on a form in favor of its own system only to have it re-enabled by the core extension which now loads later in 3.2. |
@chrisguitarguy this should have been fixed with #20995 |
Great! Will have to downgrade to Symfony 3.1 until the next bugfix release of 3.2, though. |
This PR proposes to add support for
priority
onform.type_extension
dependecyinjection tags to enable sorting/prioritizing form type extensions.Issue was mentioned here: #19735