fyrkat / configmap
Configuration classes
Requires
- php: >=8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- phpunit/phpunit: ^10
- vimeo/psalm: ^5.26
This package is not auto-updated.
Last update: 2026-08-29 07:58:44 UTC
README
Library for handling larger configuration for PHP applications
Features
Use PHP files as configuration files
Simply write a file starting with <?php return [ and write your configuration as a dictionary in PHP array format.
Have default values in a separate file
<?php
$config = new DictionaryPhpFile(
'settings.conf.php',
['/path/to/config', '/path/to/defaults'],
DictionaryPhpFile::sigils(),
);
Now the user-provided settings are read from the file in the first path, any settings not explicitly provided are read from the file in the second path. Any number of paths is allowed.
Read values with guaranteed types
<?php
$config->getString( 'string_value' ); // returns string
$config->getInt( 'string_value' ); // exception
The error message shows the full path of the wrong configuration setting, so that the user can easily troubleshoot. No more "function expected string" without knowing which value is wrong.
If the option is optional, use the OrNull method, for example $config->getStringOrNull( 'string_value' )
Read arbitrary values using ArrayAccess
<?php $config['string_value']; // returns whatever the user has set
Iterate over the dictionary
<?php
foreach( $config as $key => $value )
do_something( $key, $value );
Split larger configurations into multiple files
<?php return [
// settings.conf.php, topmost file
'large_setting#inc' => 'large.conf.php',
];
<?php return [
// large.conf.php
'some_setting' => 'some_value',
… // long file
];
$config->getDictionary( 'large_setting' )->getString( 'some_setting' ); // some_value
The include is executed transparently for the calling code
Use a directory as a dictionary
<?php return [
// ./settings.conf.php, topmost file
'user_settings#dir' => 'users/',
];
<?php return [
// ./large/bob.conf.php
'some_setting' => 'some_value',
];
$user = 'bob';
$config->getDictionary( 'user_settings' )->getDictionary( $user )->getString( 'some_setting' ); // some_value
You can also iterate over all configuration files in the directory. Note that the iterator will throw an exception if the directory contains no configuration files.
<?php
foreach( $config->getDictionary( 'user_settings' ) as $username => $user )
do_something( $username, $user->getString(' some_setting' ) );
Instantiate objects directly
<?php return [
'date' => 'now';
];
<?php $config->getObject( 'date', DateTime::class ); // runs `new DateTime( 'now' )` underneath