cklamm / config
PHP Config
Installs: 138
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/cklamm/config
Requires
- php: ^7.2
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2025-11-29 02:58:35 UTC
README
This small library makes it easy to read configuration variables from PHP files.
Contents
Installation
The library can be installed via Composer.
composer require cklamm/config
ConfigFile
This class reads configuration variables from a single PHP file, which must return an associative array. Consider the following example.
<?php // config.php return [ 'foo' => 'bar', 'array' => ['a', 'b', 'c'], 'db.driver' => 'mysql', 'db.host' => 'localhost', 'db.port' => 3306, 'log.debug.db' => '*', 'log.debug.file' => '*', 'log.prod.db' => 'error,warning,info', 'log.prod.file' => 'error,warning,info', ];
The file name is passed to the constructor of ConfigFile.
The method get($name) is used to access the configuration variables.
- If
$nameequals*, the whole array is returned. - If
$nameexists as a key, the corresponding value is returned. - If
$nameends with.*, an array is returned containing all entries whose key starts with$name(minus the asterisk). The$namepart is removed from the keys. If there are no matching keys, the returned array will be empty. - Else
nullis returned.
<?php use cklamm\Config\ConfigFile; $config = new ConfigFile('config.php'); $config->get('foo'); // 'bar' $config->get('array'); // ['a', 'b', 'c'] $config->get('undefined'); // null $config->get('db.driver'); // 'mysql' $config->get('log.prod.db'); // 'error,warning,info' $config->get('*'); // all entries $config->get('db.*'); // all entries whose key starts with db. $config->get('log.prod.*'); // all entries whose key starts with log.prod.
ConfigFolder
This class reads configuration variables from a folder containing multiple PHP files. Each file must return an associative array. For example, we could have the following two files in a config folder.
<?php // config/db.php return [ 'driver' => 'mysql', 'host' => 'localhost', 'port' => 3306, ];
<?php // config/log.php return [ 'debug.db' => '*', 'debug.file' => '*', 'prod.db' => 'error,warning,info', 'prod.file' => 'error,warning,info', ];
The folder name is passed to the constructor of ConfigFolder.
The method get($name) is used to access the configuration variables. The part of $name before the first . corresponds to the file name.
- If
$namecontains no.,nullis returned. - If no corresponding file exists,
nullis returned. - Otherwise it behaves similarly to the
getmethod ofConfigFile.
<?php use cklamm\Config\ConfigFolder; $config = new ConfigFolder('config'); $config->get('db.driver'); // 'mysql' $config->get('log.prod.db'); // 'error,warning,info' $config->get('*'); // null $config->get('db'); // null $config->get('db.*'); // all entries from config/db.php $config->get('log.prod.*'); // all entries from config/log.php // whose key starts with prod.