, (*1)
, (*2)
PHP library for simple configuration management
-- Created by Chris Kankiewicz
(@PHLAK)
, (*3)
Introduction
Config is a simple PHP configuration management library supporting multiple
configuration file formats and an expressive API., (*4)
Supported file formats:, (*5)
- PHP
- INI
- JSON
- TOML
- YAML
- XML
Requirements
Install with Composer
composer require phlak/config
Initializing the Client
First, import Config:, (*6)
use PHLAK\Config\Config;
Then instantiate the class:, (*7)
$config = new Config($context, $prefix = null);
Where $context
is one of the following:, (*8)
- A path to a supported file type
- A directory path containing one or more supported file types
- An array of configuration options
And $prefix
is a string to be used as a key prefix for options of this Config object., (*9)
PHP
A PHP configuration file must have the .php
file extension, be a valid PHP
file and and return a valid PHP array., (*10)
Example PHP file
, (*11)
```php
<?php, (*12)
return [
'driver' => 'mysql',
'drivers' => [
'sqlite' => [
'database' => 'database.sqlite',
'prefix' => ''
],
'mysql' => [
'host' => 'localhost',
'database' => 'blog',
'username' => 'blogger',
'password' => 'hunter2',
'charset' => 'utf8',
'prefix' => ''
]
]
];
```
, (*13)
INI
An INI configuration file must have the .ini
file extension and be a valid INI
file., (*14)
Example INI file
, (*15)
```ini
driver = mysql, (*16)
[drivers], (*17)
sqlite[database] = database.sqlite
sqlite[prefix] =, (*18)
mysql[host] = localhost
mysql[database] = blog
mysql[username] = blogger
mysql[password] = hunter2
mysql[charset] = utf8
mysql[prefix] =
```
, (*19)
JSON
A JSON configuration file must have the .json
file extension and contain a
valid JSON object., (*20)
Example JSON file
, (*21)
json
{
"driver": "mysql",
"drivers": {
"sqlite": {
"database": "database.sqlite",
"prefix": ""
},
"mysql": {
"host": "localhost",
"database": "blog",
"username": "blogger",
"password": "hunter2",
"charset": "utf8",
"prefix": ""
}
}
}
, (*22)
TOML
A TOML configuration file must have the .toml
file extension and be a valid
TOML file., (*23)
Example TOML file
, (*24)
```toml
driver = 'mysql', (*25)
[drivers.sqlite]
database = 'database.sqlite'
prefix = '', (*26)
[drivers.mysql]
host = 'localhost'
database = 'blog'
username = 'blogger'
password = 'hunter2'
charset = 'utf8'
prefix = ''
```
, (*27)
YAML
A YAML configuration file must have the .yaml
file extension, be a valid YAML
file., (*28)
Example YAML file
, (*29)
```yaml
driver: mysql, (*30)
drivers:, (*31)
sqlite:
database: database.sqlite
prefix:
mysql:
host: localhost
database: blog
username: blogger
password: hunter2
charset: utf8
prefix:
```
, (*32)
XML
A XML configuration file must have the .xml
file extension and contain valid
XML., (*33)
Example XML file
, (*34)
```xml
, (*35)
mysql
database.sqlite
localhost
blog
blogger
hunter2
utf8
```
, (*36)
Usage
__construct
Create a new Config object., (*37)
Config::__construct( mixed $context [, string $prefix = null ] ) : Config
$context
- Raw array of configuration options or path to a configuration file or directory containing one or more configuration files
$prefix
- A key under which the loaded config will be nested
Examples
Create a new Config object from a YAML file., (*38)
$config = new Config('path/to/conifg.yaml');
Create a new Config object from a directory of config files., (*39)
$config = new Config('path/to/conifgs/');
Create a new Config object from an array., (*40)
$config = new Config([
'hostname' => 'localhost',
'port' => 12345
]);
set
Store a config value with a specified key., (*41)
Config::set( string $key, mixed $value ) : bool
$key
- Unique configuration option key
$value
- Config item value
Example
$config->set('hostname', 'localhost');
$config->set('port', 12345);
get
Retrieve a configuration option via a provided key., (*42)
Config::get( string $key [, mixed $default = null ] ) : mixed
$key
- Unique configuration option key
$value
- Config item value
Examples
// Return the hostname option value or null if not found.
$config->get('hostname');
Define a default value to return if the option is not set., (*43)
// Returns 'localhost' if hostname option is not set
$config->get('hostname', 'localhost');
has
Check for the existence of a configuration item., (*44)
Config::has( string $key ) : bool
$key
- Unique configuration option key
Example
$config = new Config([
'hostname' => 'localhost'
]);
$config->has('hostname'); // Returns true
$config->has('port'); // Returns false
append
Append a value onto an existing array configuration option., (*45)
Config::append( string $key, mixed $value ) : bool
$key
- Unique configuration option key
$value
- Config item value
Example
Append baz
to the tags
config item array., (*46)
$config->set('tags', ['foo', 'bar'])
$config->append('tags', 'baz'); // ['foo', 'bar', 'baz']
prepend
Prepend a value onto an existing array configuration option., (*47)
Config::append( string $key, mixed $value ) : bool
$key
- Unique configuration option key
$value
- Config item value
Example
Prepend baz
to the tags
config item array., (*48)
$config->set('tags', ['foo', 'bar'])
$config->append('tags', 'baz'); // ['baz', 'foo', 'bar']
unset
Unset a configuration option via a provided key., (*49)
Config::unset( string $key ) : bool
$key
- Unique configuration option key
Example
$config->unset('hostname');
load
Load configuration options from a file or directory., (*50)
Config::load( string $path [, string $prefix = null [, bool $override = true ]] ) : self
$path
- Path to configuration file or directory
$prefix
- A key under which the loaded config will be nested
$override
- Whether or not to override existing options with values from the loaded file
Examples
Load a single additional file., (*51)
$conifg->load('path/to/config.php');
Load an additional file with a prefix., (*52)
$config->load('databaes.php', 'database');
Load an additional file without overriding existing values., (*53)
$config->load('additional-options.php', null, false);
merge
Merge another Config object into this one., (*54)
Config::merge( Config $config [, bool $override = true ] ) : self
$config
- Instance of Config
$override
- Whether or not to override existing options with values from the merged config object
Examples
Merge $anotherConfig into $config and override values in $config
with values
from $anotherConfig
., (*55)
$anotherConfig = new Config('some/config.php');
$config->merge($anotherConfig);
Merge $anotherConfig
into $config
without overriding any values. Duplicate
values in $anotherConfig
will be lost., (*56)
$anotherConfig = new Config('some/config.php');
$config->merge($anotherConfig, false);
split
Split a sub-array of configuration options into it's own Config object., (*57)
Config::split( string $key ) : Config
$key
- Unique configuration option key
Example
$config = new Config([
'foo' => 'foo',
'bar' => [
'baz' => 'barbaz'
],
]);
$barConfig = $config->split('bar');
$barConfig->get('baz'); // Returns 'barbaz'
toArray
Return the entire configuration as an array., (*58)
Config::toArray( void ) : array
Example
$config = new Config(['foo' => 'foo']);
$config->toArray(); // Returns ['foo' => 'foo']
Troubleshooting
For general help and support join our GitHub Discussion or reach out on Twitter., (*59)
Please report bugs to the GitHub Issue Tracker., (*60)
Copyright
This project is liscensed under the MIT License., (*61)