Skip to content

Pass a read-only ConfigSection to adapters instead of config arrays #672

Done

Description

@armanist

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.enabled and use_source_catalog with filter_var(...), cache.ttl with (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): bool
  • getBool(string $key, ?bool $default = null): bool
  • getInt(string $key, ?int $default = null): int
  • all(): array, the whole slice, for an adapter that hands all of its options to a library (the Twig renderer passes them to Twig\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:

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 as KEY= in .env) is not set
  • a key that is not set returns the default when one is given, and throws ConfigException::missingKey() ("The config key x is missing.") when none is given: a reader without a default states a required key
  • getBool() returns true for true, 1, on and yes, ignoring letter case, and false for every other value
  • getInt() returns the number for whole-number values such as 587 or '587', and throws ConfigException::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 $params takes ConfigSection instead.
  • Code and tests that build an adapter from an array must build a ConfigSection from it: new ConfigSection([...]).
  • Invalid whole-number values (abc) now throw instead of becoming 0, and a missing cache or rate limit ttl or port throws.
  • getBool() and getInt() without a default require the key: a missing, null or empty value throws instead of returning false or 0. 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 of null when empty; ConfigInterface declares section() and all(): array.
  • ValueTrait is renamed WriteTrait, ConfigFileTrait is removed, LoggerTrait::initialize() takes a ConfigSection, and a class that uses AuthTrait must implement readConfig().

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, null and 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; RedisAdapterTest keeps 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 src reads its config through $params['key'] or $this->config['key']
  • no filter_var(..., FILTER_VALIDATE_BOOLEAN) and no (int) of a config value is left in src, 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

  1. added this to the 3.2.0 milestone on Nov 14, 2026
  2. added
    breaking change
    config
    on Nov 14, 2026
  3. self-assigned this
    on Nov 23, 2026
  4. added 16 commits that reference this issue on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

breaking changeconfigrefactoring

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions