Repository navigation
Description
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 = null): boolgetInt(string $key, ?int $default = null): intall(): array, the whole slice, for an adapter that hands all of its options to a library (the Twig renderer passes them toTwig\Environment)section(string $key): ConfigSection, a smaller section of it
It has no load(), import(), set(), delete() or flush(). Keys use dot notation, like Config, so getBool('cache.enabled') works. A key that is missing, or whose value is not an array, gives an empty section, and a section keeps the values it was created with. Arrays are copied, so a section and the arrays it returns never change each other; objects stored in the config, such as closures or services, are shared, not cloned.
Config and ConfigSection are separate classes that read with the same code, so the reading methods are written once:
Traits/ReadTrait:get(),has(),all()andsection(), used by bothTraits/TypedValuesTrait(from Add getBool() and getInt() to Config and use them instead of filter_var and (int) in the framework #616):getBool()andgetInt(), used by bothTraits/WriteTrait:set(),delete()andflush(), used only byConfig(renamed fromValueTrait)Configitself loads and imports the config files; the code ofConfigFileTraitmoves back into it, since no other class used the trait
Config::all() returns an array (a copy) instead of the internal Dflydev\DotAccessData\Data object, through which the config could be changed without set().
The rules for getBool() and getInt() are written once, in the trait from #616, and apply to Config and ConfigSection alike. The trait needs only a get() method:
- a key that is missing,
null, or empty text (such asKEY=in.env) is not set - a key that is not set returns the default when one is given, and throws
ConfigException::missingKey()("The config keyxis missing.") when none is given: a reader without a default states a required key 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
The framework's own optional reads pass their default, such as config()->getBool('app.debug', false), so they behave as before.
get() is unchanged: it returns the stored value, null and '' included, and its default only when the key does not exist.
What changes in the adapters
The constructor argument changes from array $params to ConfigSection $config, and the adapter reads through the section:
public function __construct(ConfigSection $config)
{
$this->ttl = $config->getInt('ttl');
$this->prefix = $config->get('prefix');
$this->redis->connect($config->get('host'), $config->getInt('port'));
}getInt('ttl') and getInt('port') have no default, so a missing cache or rate limit ttl or port throws ConfigException::missingKey(); a ttl of 0 would remove every item at once. The SMTP adapter reads getInt('port', 25), PHPMailer's default, because projects created before 3.1.2 have MAIL_PORT= empty in .env.
The auth adapters read their settings once, when they are built, so an invalid value fails there and not halfway through a sign-in. AuthTrait declares abstract protected function readConfig(ConfigSection $config): void, like LoggerTrait::initialize(), and each adapter implements it: the session adapter reads two_fa, otp_expires and session.remember_lifetime, the JWT adapter two_fa and otp_expires.
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. - 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, and a missing cache or rate limitttlorportthrows. getBool()andgetInt()without a default require the key: a missing,nullor empty value throws instead of returningfalseor0. Empty text counts as not set, so with a default the default is used.- The auth adapters require their config again; it was optional since 3.0.0, so an adapter built without it ran with two-factor authentication off.
Config::all()returns an array, and[]instead ofnullwhen empty;ConfigInterfacedeclaressection()andall(): array.ValueTraitis renamedWriteTrait,ConfigFileTraitis removed,LoggerTrait::initialize()takes aConfigSection, and a class that usesAuthTraitmust implementreadConfig().
Also fixed, because sections rely on it: config()->set() with a dotted key on an empty config stored the literal key ('cache.redis'), so section('cache.redis') found nothing; it now nests the value as it does on a config that has values.
This needs a BREAKING entry in CHANGELOG.md with a migration note for custom adapters.
Tests
ConfigSection:get,has,getBool,getInt,all,section, dot-notation keys, missing,nulland empty values with and without a default, invalid whole-number text, nested arrays copied and objects shared.Config::section(): returns the requested slice, an empty section for a missing key or a value that is not an array.- 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
Updated 2026-11-24, after the review of PR #706: getBool() and getInt() without a default require the key, and empty text counts as not set; the per-adapter ttl checks are gone; the SMTP port defaults to 25; each auth adapter reads its settings in readConfig(); the set() fix is listed.
Updated 2026-11-24, to match PR #706: Config and ConfigSection are separate classes that share ReadTrait; all() returns an array on both (no toArray()); ValueTrait is renamed WriteTrait and ConfigFileTrait folded into Config; a missing ttl throws; the auth adapters require their config.
Activity
[quantum-php#672] Test the missing ttl in every cache and rate limit …
[quantum-php#672] Require the key in getInt() and getBool() without a…
[quantum-php#672] Treat an empty config value as not set and default …
[quantum-php#672] Read the auth settings when the auth adapter is built
[quantum-php#672] Let each auth adapter read its own settings
[quantum-php#672] Test that sections are detached from the config
[quantum-php#672] Remove the comments on the trait use lines in Config
[#672] Add ConfigSection and Config::section()
[#672] Move the config file loading back into Config
[#672] Pass a ConfigSection to the cache and rate limit adapters
[#672] Pass a ConfigSection to the mailer and captcha adapters
[#672] Pass a ConfigSection to the logger, renderer and session adapters
[#672] Pass a ConfigSection to the lang adapters
[#672] Pass a ConfigSection to the auth adapters
[#672] Require the config in the auth adapters
Merge pull request #706 from armanist/issue/672-config-section