PHP library for simple configuration management -- by, Chris Kankiewicz
Config is a simple PHP configuration management library supporting multiple, configuration file formats and an expressive API.
Supported file formats:
- PHP
- INI
- JSON
- YAML
- XML
Like this project? Keep me caffeinated by making a donation.
- PHP >= 5.6
composer require phlak/config
First, import Config:
use PHLAK\Config;
Then instantiate the class:
$config = new Config\Config($context);
Where $context
is a path to a supported file type, a directory containing one
or more supported file types or an array of configuration options.
A PHP configuration file must have the .php
file extension, be a valid PHP
file and and return a valid PHP array.
<?php
return [
'driver' => 'mysql',
'drivers' => [
'sqlite' => [
'database' => 'database.sqlite',
'prefix' => ''
],
'mysql' => [
'host' => 'localhost',
'database' => 'blog',
'username' => 'blogger',
'password' => 'hunter2',
'charset' => 'utf8',
'prefix' => ''
]
]
];
An INI configuration file must have the .ini
file extension and be a valid INI
file.
driver = mysql
[drivers]
sqlite[database] = database.sqlite
sqlite[prefix] =
mysql[host] = localhost
mysql[database] = blog
mysql[username] = blogger
mysql[password] = hunter2
mysql[charset] = utf8
mysql[prefix] =
A JSON configuration file must have the .json
file extension and contain a
valid JSON object.
{
"driver": "mysql",
"drivers": {
"sqlite": {
"database": "database.sqlite",
"prefix": ""
},
"mysql": {
"host": "localhost",
"database": "blog",
"username": "blogger",
"password": "hunter2",
"charset": "utf8",
"prefix": ""
}
}
}
A YAML configuration file must have the .yaml
file extension, be a valid YAML
file.
driver: mysql
drivers:
sqlite:
database: database.sqlite
prefix:
mysql:
host: localhost
database: blog
username: blogger
password: hunter2
charset: utf8
prefix:
A XML configuration file must have the .xml
file extension and contain valid
XML.
<?xml version='1.0'?>
<database>
<driver>mysql</driver>
<drivers>
<sqlite>
<database>database.sqlite</database>
<prefix></prefix>
</sqlite>
<mysql>
<host>localhost</host>
<database>blog</database>
<username>blogger</username>
<password>hunter2</password>
<charset>utf8</charset>
<prefix></prefix>
</mysql>
</drivers>
</database>
Set a configuration option:
$config->set($key, $value);
Retrieve a configuration option:
$config->get($key, $default = null);
Check if a configuration option exists:
$config->has($key, $override = false);
Load an additional configuration file:
$conifg->load($pathToConfig, $override = true);
Merge two Config objects into one:
$config = new Config\Config(['foo' => 'foo', 'baz' => 'baz']);
$gifnoc = new Config\Config(['bar' => 'rab', 'baz' =>'zab']);
$config->merge($gifnoc);
$config->get('foo'); // Returns 'foo'
$config->get('bar'); // Returns 'rab'
$config->get('baz'); // Returns 'zab'
Split a sub-array of options into it's own object:
$config = new Config\Config([
'foo' => 'foo',
'bar' => [
'baz' => 'barbaz'
]
]);
$bar = $config->split('bar');
$bar->get('baz'); // Returns 'barbaz'
Please report bugs to the GitHub Issue Tracker.
This project is liscensed under the MIT License.