-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Added support for caching choice lists based on options #30994
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\ChoiceList; | ||
|
||
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceAttr; | ||
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceFieldName; | ||
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel; | ||
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader; | ||
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceValue; | ||
use Symfony\Component\Form\ChoiceList\Factory\Cache\GroupBy; | ||
use Symfony\Component\Form\ChoiceList\Factory\Cache\PreferredChoice; | ||
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
use Symfony\Component\Form\FormTypeExtensionInterface; | ||
use Symfony\Component\Form\FormTypeInterface; | ||
|
||
/** | ||
* A set of convenient static methods to create cacheable choice list options. | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
final class ChoiceList | ||
{ | ||
/** | ||
* Creates a cacheable loader from any callable providing iterable choices. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param callable $choices A callable that must return iterable choices or grouped choices | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader | ||
*/ | ||
public static function lazy($formType, callable $choices, $vary = null): ChoiceLoader | ||
{ | ||
return self::loader($formType, new CallbackChoiceLoader($choices), $vary); | ||
} | ||
|
||
/** | ||
* Decorates a loader to make it cacheable. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader | ||
*/ | ||
public static function loader($formType, ChoiceLoaderInterface $loader, $vary = null): ChoiceLoader | ||
{ | ||
return new ChoiceLoader($formType, $loader, $vary); | ||
} | ||
|
||
/** | ||
* Decorates a "choice_value" callback to make it cacheable. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param callable $value Any pseudo callable to create a unique string value from a choice | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback | ||
*/ | ||
public static function value($formType, $value, $vary = null): ChoiceValue | ||
{ | ||
return new ChoiceValue($formType, $value, $vary); | ||
} | ||
|
||
/** | ||
* Decorates a "choice_label" option to make it cacheable. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option | ||
*/ | ||
public static function label($formType, $label, $vary = null): ChoiceLabel | ||
{ | ||
return new ChoiceLabel($formType, $label, $vary); | ||
} | ||
|
||
/** | ||
* Decorates a "choice_name" callback to make it cacheable. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param callable $fieldName Any pseudo callable to create a field name from a choice | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback | ||
*/ | ||
public static function fieldName($formType, $fieldName, $vary = null): ChoiceFieldName | ||
{ | ||
return new ChoiceFieldName($formType, $fieldName, $vary); | ||
} | ||
|
||
/** | ||
* Decorates a "choice_attr" option to make it cacheable. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param callable|array $attr Any pseudo callable or array to create html attributes from a choice | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option | ||
*/ | ||
public static function attr($formType, $attr, $vary = null): ChoiceAttr | ||
{ | ||
return new ChoiceAttr($formType, $attr, $vary); | ||
} | ||
|
||
/** | ||
* Decorates a "group_by" callback to make it cacheable. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param callable $groupBy Any pseudo callable to return a group name from a choice | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback | ||
*/ | ||
public static function groupBy($formType, $groupBy, $vary = null): GroupBy | ||
{ | ||
return new GroupBy($formType, $groupBy, $vary); | ||
} | ||
|
||
/** | ||
* Decorates a "preferred_choices" option to make it cacheable. | ||
* | ||
* @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list | ||
* @param callable|array $preferred Any pseudo callable or array to return a group name from a choice | ||
* @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option | ||
*/ | ||
public static function preferred($formType, $preferred, $vary = null): PreferredChoice | ||
{ | ||
return new PreferredChoice($formType, $preferred, $vary); | ||
} | ||
|
||
/** | ||
* Should not be instantiated. | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 not sure I see the point of this class. I'd suggest removing it and inlining the instantiations 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.
It's the user land part of this feature, everything else is about optimising the internals, many advantages to me:
easier to document/learn, only one class instead of many (I also add the
ChoiceFilter
in [Form] Added a "choice_filter" option to ChoiceType #35733 and we may add more in the future like aChoiceLabelAttr
I'got locally)better discovery since every factory method has its own specific phpdoc instead of the generic constructor in the abstract class
better auto complete, there are a lot of classes using the

Choice
word already:VS
I agree we could inline in our own usage in this PR to save a few static calls, but I wouldn't either since the types are some kind of documentation and given the Blackfire profiles I got with the current implementation.
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 see this class as a
ChoiceList
builder or configurator