-
Notifications
You must be signed in to change notification settings - Fork 0
The Library Object
InitPHP\Config\Library is the package's main entry point: an injectable
object that holds a configuration tree and knows how to populate it from
several sources. Inject it where you need isolated, testable
configuration — as opposed to the global Config facade.
use InitPHP\Config\Library;
$config = new Library(); // empty
$config = new Library(['db' => []]); // seeded from an arrayLibrary implements ConfigInterface:
$config->set('app.name', 'InitPHP'); // returns $this — chainable
$config->get('app.name'); // 'InitPHP'
$config->get('app.locale', 'en'); // 'en' (default — key absent)
$config->has('app.name'); // true
$config->remove('app.name'); // returns $this
$config->all(); // the whole tree as an arraySee Keys & Dotted Paths for how keys are parsed.
Library can import configuration from arrays, PHP files, directories,
and class/object properties. Each loader returns $this, so they chain
with everything else:
$config
->setArray('site', ['url' => 'http://lvh.me'])
->setFile('db', __DIR__ . '/config/db.php')
->setDir('app', __DIR__ . '/config')
->setClass(\App\Config\Database::class);Each one is documented in detail on the Loaders page:
| Method | Loads from |
|---|---|
setArray() |
An associative array. |
setFile() |
A PHP file that returns an array. |
setDir() |
Every *.php file in a directory. |
setClass() |
A class's / object's public properties. |
set() writes a single key; replace() discards the whole tree and
installs a new one:
$config->set('old', 'value');
$config->replace(['fresh' => 'data']);
$config->has('old'); // false
$config->get('fresh'); // 'data'Top-level entries can be read as nested, read-only stdClass objects:
$config->set('db.host', 'localhost')->set('db.user', 'root');
$config->db->host; // 'localhost'
$config->db->user; // 'root'
$config->name; // a scalar entry is returned as-is
$config->unknown; // null for an unknown keyThis is covered on the Object Access page.
$config->version(); // the package version, e.g. '2.0.0'
$config->close(); // clear the tree (the instance stays usable)close() empties the configuration but keeps the instance ready for
reuse — the dotted-path and case-insensitive behaviour is preserved:
$config->set('a.b', 'c');
$config->close();
$config->all(); // []
$config->set('x.y', 'z'); // still works
$config->get('x.y'); // 'z'The object's destructor calls close() automatically, so there is
normally nothing to clean up by hand.
Because Library is a plain object, it slots into any container. Prefer
type-hinting against the interface so consumers can be tested with a
stub:
use InitPHP\Config\Interfaces\ConfigInterface;
use InitPHP\Config\Library;
$container->set(ConfigInterface::class, function () {
$config = new Library();
$config->setDir('app', __DIR__ . '/config');
return $config;
});
final class ReportService
{
public function __construct(private ConfigInterface $config) {}
public function timezone(): string
{
return $this->config->get('app.timezone', 'UTC');
}
}Heads-up:
ConfigInterfacecoversget/set/has/remove/all. If a consumer needs the loaders (setFile,setDir, …) or object access, type-hint againstLibraryinstead.
- Loaders — every loader, with examples and error cases.
- Object Access — reading subtrees as objects.
- The Config Facade — the static convenience layer.
- Exceptions — what loaders throw on failure.
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