Add dsnClassMap property to StaticConfigTrait#19257
Conversation
This change adds the `$dsnClassMap` property directly to StaticConfigTrait using a lazy initialization pattern via `buildDsnClassMap()`. Classes that need a custom DSN class map override `buildDsnClassMap()` instead of declaring their own property. Benefits: - Property is now defined in the trait, eliminating PHPStan issues - Implementing classes use `buildDsnClassMap()` for customization - Maintains backwards compatibility via `getDsnClassMap()`/`setDsnClassMap()` Note: The `$registry` property is NOT added to the trait because implementing classes require specific registry types (CacheRegistry, LogEngineRegistry, etc.) that cannot be generalized in the trait. Refs cakephp/cakephp#wiki/6.0-Ideas
|
If this means not adding the |
|
Dont know who added it to the wiki ideas/roadmap, but since it was there, might as well look into it... |
Idk but noticed it's iffyness when I was creating my attribute facade factory. It was also on my radar to do something about it, guess you beat me to it 💪 |
| * | ||
| * @return array<string, class-string> | ||
| */ | ||
| protected static function buildDsnClassMap(): array |
There was a problem hiding this comment.
I assume this is required because PHP doesn't allow overriding the property with a different value if it's already declared in the trait?
There was a problem hiding this comment.
maybe a more explicit pattern would be to use an interface ? DsnAwareStaticConfigInterface ? Or is this overkill ?
There was a problem hiding this comment.
In this case probably a bit overkill. After all, this is also more internal, so not too much to be used from the outside.
markstory
left a comment
There was a problem hiding this comment.
Seems reasonable to me. Should dsn mapping become a utility class that gets used instead of trait properties being inherited?
To find an answer for that I'd ask if that DSN parsing and mapping Utility would be used somewhere other than inside objects using the StaticConfigTrait. Currently we have examples documented for ConnectionManager, Log, Email and Caching I looked at the InstanceConfigTrait (and the classes using it) as it serves a similar purpose and haven't found a good usecase to use DSN mapping/parsing there. But if there is a case I am not thinking of then sure, it can/should be extracted into a utility class. |
Summary
$dsnClassMapproperty directly toStaticConfigTraitusing lazy initializationbuildDsnClassMap()instead of declaring propertyCache,Log,ConnectionManager,TransportFactory,MailerWhy not
$registry?The
$registryproperty is intentionally NOT added to the trait because implementing classes require specific registry types (CacheRegistry,LogEngineRegistry,ConnectionRegistry, etc.) that cannot be generalized. The trait continues to use@phpstan-ignore property.notFoundfor the optional$registryaccess indrop().Breaking Changes
Classes extending or using
StaticConfigTraitthat declare their own$dsnClassMapproperty must:buildDsnClassMap()method insteadRefs https://github.com/cakephp/cakephp/wiki/6.0-Ideas