-
Notifications
You must be signed in to change notification settings - Fork 645
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
feat: Add fluent model methods to config classes #11591
Conversation
So I really wanted to change
Unfortunately, Possible solution: rename the Wasn't sure how to solve this in a way you'd be happy with, so just leaving notes here in case you want to think about it and do something with it. |
…a `__call()` magic method if no actual fluent model method is provided
I don't see a problem with this, one is a property and the other is a method. The only issue is if a plugin/module is calling the magic getter UPDATE: Ah I see the issue now, it conflicts with the fluent model method. |
…atic $configCategory property
… plugins to use `BaseConfig` for their Settings model
Resolved this by:
|
Merged via #11656! |
4.2.0 is out with this! |
Description
This PR adds fluent model methods to the
GeneralConfig
andDbConfig
objects, allowing you to write config files like this:This gives you:
...and makes it work similar to how the Craft
Query
class works currently, with both properties and fluent model methods of the same name, both fully annotated.general-config-demo.mov
It also adds a
BaseConfig
object with aBaseConfig::create()
factory method to make it nicer to work with when using the fluent model methods.Also added
craft\base\FluentModelTrait
that is used inBaseConfig
to allow fluent model methods to work via__call()
magic method if no actual fluent model method is provided in the config class. It is preferred to add actual methods to the config class, to avoid the magic method overhead, but more importantly, to provide multi-line inline documentation via the method docblock, but the fallback is nice to have.There should be no breaking changes from this PR, it just adds to the existing functionality.
Related Issues
Working with arrays in PHP is troublesome, especially for things like config files where a large number of specific keys are needed. It's easy to make typos, there's no immediate feedback on the type correctness, and you spend quite a bit of time going back and forth between the documentation and the config file.
This implementation should also make it easier to deprecate config file values that are removed or renamed.
Alternatives
I initially tried using PhpStorm's ArrayShape annotations on the
GeneralConfig::__construct()
method's$config
parameter, however they don't allow for per-array element comments, so we are missing the inline documentation, which is critically important for config file settingsI had also tried implementing a fluent model using PHP magic methods, but
@method
and@property
annotations only allow for a single line documentation, so we'd be missing out on theGeneralConfig
class's copious documentation, and also then we have magic method overhead for setting each config property