Pass a read-only ConfigSection to adapters instead of config arrays
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 15/100
Hướng nghiên cứu
Start with src/Config/Config.php and the trait from #616 that adds getBool() and getInt(), since the new ConfigSection must reuse it. Then read the factories listed in the issue, such as src/Cache/Factories/CacheFactory.php, and the adapters that convert values by hand, such as src/Cache/Adapters/RedisAdapter.php and src/Auth/Traits/AuthTrait.php. Done means the acceptance criteria hold: no adapter reads $params['key'], no filter_var or (int) config conversion is left in src, and the tests pass. This depends on #616 landing first.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Context
#616 adds getBool() and getInt() to Config, so code that reads the global config (config()->get('app.debug')) can get a bool or an int with the same rules everywhere.
Adapters do not read the global config. A factory reads one slice of it as a plain array and passes it to the adapter constructor, for example in src/Cache/Factories/CacheFactory.php:
$cacheAdapter = new $adapterClass(config()->get('cache.' . $adapter));
and the adapter then converts values itself, for example in src/Cache/Adapters/RedisAdapter.php:
$this->redis->connect($params['host'], (int) $params['port']);
Problem
Because the adapters get a plain array, #616 cannot reach them. These places still convert by hand:
src/Auth/Traits/AuthTrait.php:filter_var($this->config['two_fa'] ?? false, FILTER_VALIDATE_BOOLEAN)src/Auth/Adapters/SessionAuthAdapter.php:(int) ($this->config['session']['remember_lifetime'] ?? self::DEFAULT_REMEMBER_LIFETIME)src/Lang/Traits/RemoteAdapterTrait.php:cache.enabledanduse_source_catalogwithfilter_var(...),cache.ttlwith(int)src/Cache/Adapters/RedisAdapter.php,src/Cache/Adapters/MemcachedAdapter.php,src/RateLimit/Adapters/RedisRateLimitAdapter.php:(int) $params['port']
The rules are copied, and invalid text such as abc silently becomes 0 for (int).
Proposal
Add a read-only class, Quantum\Config\ConfigSection, that holds one slice of the config, and give every adapter its section instead of an array.
Config::section(string $key): ConfigSection creates it:
$section = config()->section('cache.redis');
ConfigSection has only these methods:
get(string $key, $default = null)has(string $key): boolgetBool(string $key, bool $default = false): boolgetInt(string $key, int $default = 0): int
It has no load(), import(), set(), delete() or flush(), so it cannot be confused with the global Config. Keys use dot notation, like Config, so getBool('cache.enabled') works.
The rules for getBool() and getInt() are written once, in a trait from #616 that both Config and ConfigSection use. The trait needs only a get() method:
- a missing key, or a key set to
null, returns the default getBool()returns true fortrue,1,onandyes, ignoring letter case, and false for every other valuegetInt()returns the number for whole-number values such as587or'587', and throwsConfigException::invalidValue()for any other value
config()->getBool('app.debug') style reads of single keys from the global config stay as they are after #616; ConfigSection is only for adapters.
What changes in the adapters
The constructor type changes from array $params to ConfigSection $params, and the adapter reads through the section:
public function __construct(ConfigSection $params)
{
$this->ttl = $params->getInt('ttl');
$this->prefix = $params->get('prefix');
$this->redis->connect($params->get('host'), $params->getInt('port'));
}
The factories pass the section:
new $adapterClass(config()->section('cache.' . $adapter));
Packages whose factory passes a config array today, and so change:
- Cache (
src/Cache/Factories/CacheFactory.php) - Auth (
src/Auth/Factories/AuthFactory.php) - Lang (
src/Lang/Factories/LangFactory.php) - Session (
src/Session/Factories/SessionFactory.php) - Renderer (
src/Renderer/Factories/RendererFactory.php) - Captcha (
src/Captcha/Factories/CaptchaFactory.php) - Mailer (
src/Mailer/Factories/MailerFactory.php) - RateLimit (
src/RateLimit/Factories/RateLimiterFactory.php) - Logger (
src/Logger/Factories/LoggerFactory.php)
Not part of this ticket: factories that pass no config array (Archive, Encryption, Paginator, App) and Storage, which builds a cloud app object from single keys.
In the adapters listed under Problem, the filter_var() and (int) conversions go away, and defaults move to the getInt('key', $default) and getBool('key', $default) calls.
Breaking changes
- Every adapter constructor that takes
array $paramstakesConfigSectioninstead. Custom adapters in apps must be updated. - Code and tests that build an adapter from an array must build a
ConfigSectionfrom it:new ConfigSection([...]). - Invalid whole-number values (
abc) now throw instead of becoming0.
This needs a BREAKING entry in CHANGELOG.md with a migration note for custom adapters.
Tests
ConfigSection:get,has,getBool,getInt, dot-notation keys, missing andnullvalues, invalid whole-number text.Config::section(): returns the requested slice, an empty section for a missing key.- Each adapter test builds its adapter from a
ConfigSection;RedisAdapterTestkeeps its case where the port is text. - The factories pass the section to the adapter.
Docs
The Config page and the page of each package whose adapter constructor changes (adapter contract and custom adapter examples). Tracked in a separate docs ticket after this one is merged.
Acceptance criteria
- no adapter in
srcreads its config through$params['key']or$this->config['key'] - no
filter_var(..., FILTER_VALIDATE_BOOLEAN)and no(int)of a config value is left insrc, or each remaining one has a stated reason - all checks and tests pass
- Ngôn ngữ chính
- PHP
- Star
- 43
- Fork
- 22
- Merge trung bình
- 54 phút
- Pull request đã merge (30 ngày)
- 57
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của quantum-php/framework
-
bug good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
quantum-php/framework#692 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
routing testing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
quantum-php/framework#547 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
quantum-php/framework#694 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Raise PHPStan to level 9Đang mởrefactoring tech debt
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
quantum-php/framework#693 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
install:debugbar reads the package folder from the working directory and uses the default storage adapterCó thể đã có người làm @armanist đã nhận hôm nay. Đang mởbug console debugbar good first issue
quantum-php/framework#691 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của quantum-php/framework
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 70/100
ajeetraina/awesome-docker-sbx#559 ·
-
dependencies enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
novalo-dev/Prestashop-MCP-Server#329 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug origin:interactive status:available
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Ultimate-Multisite/ultimate-multisite#2131 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
sync-en
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày