-
-
Notifications
You must be signed in to change notification settings - Fork 0
Keys and Dotted Paths
Every entry point stores data in a single nested tree. Two rules govern
how a string key maps onto that tree. They are inherited from the
underlying initphp/parameterbag
store, which this package always configures in multi mode with
case-insensitive keys.
A dot (.) separates levels of nesting. Writing a dotted key creates the
intermediate arrays automatically.
use InitPHP\Config\Library;
$config = new Library();
$config->set('db.connections.mysql.host', 'localhost');
$config->get('db.connections.mysql.host'); // 'localhost'
$config->get('db.connections.mysql'); // ['host' => 'localhost']
$config->get('db'); // ['connections' => ['mysql' => ['host' => 'localhost']]]Reading a path that hits a scalar before consuming every segment returns the default rather than descending into the scalar:
$config->set('app.name', 'InitPHP');
$config->get('app.name.first', 'n/a'); // 'n/a'Keys are folded to lower-case internally, so the case you use when reading does not have to match the case you used when writing.
$config->set('App.Database.Host', 'localhost');
$config->get('app.database.host'); // 'localhost'
$config->get('APP.DATABASE.HOST'); // 'localhost'
$config->has('app.DATABASE.host'); // trueThis applies to every source:
- A file returning
['HOST' => 'localhost']loaded underdbis read back asdb.host. - A class
AppConfigloaded withsetClass()is read back underappconfig.
// config/db.php -> return ['HOST' => 'localhost'];
$config->setFile('db', __DIR__ . '/config/db.php');
$config->get('db.host'); // 'localhost'Why case-insensitive? ParameterBag v2 is case-sensitive by default, but
initphp/configopts into case-insensitivity so that file/class keys written in any casing (HOST,Host,host) resolve uniformly. This is configured internally and is not something callers turn off.
has() is the authoritative existence check: a key whose stored value is
null is still considered present.
$config->set('feature.flag', null);
$config->has('feature.flag'); // true
$config->get('feature.flag', 'fallback'); // null (the stored value wins)
$config->has('feature.missing'); // false
$config->get('feature.missing', 'fallback'); // 'fallback'- Loaders — how each source populates the tree.
- Object Access — reading a subtree as a nested object.
- ParameterBag's own Nested Data and Case Sensitivity pages cover the underlying mechanics.
initphp/config · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Concepts
Loading Configuration
Reference
Practical Guides
Migration & Help