Puppy Config
, (*1)
Puppy Config is a config manager that loads your config from files according to your env., (*2)
Config basic logic:, (*3)
- env config management (dev, test, prod, ...)
- multi file formats support (php, ini, json, yaml)
- dynamic config values
- visibility restriction
Installation
Run composer:, (*4)
$ composer require raphhh/puppy-config
Read your config
Add the main config file /config/global.php:, (*5)
// /config/global.php
return [
'key' => 'value',
];
Launch the Config class:, (*6)
use Puppy\Config\Config;
$config = new Config();
$config['key']; //'value'
Dynamic values
You can retrieve dynamically a previous defined value with it key., (*7)
// /config/global.php
return [
'key1' => 'value1',
'key2' => '%key1%_b',
];
```php
use Puppy\Config\Config;, (*8)
$config = new Config();
$config['key2']; //'value1_b', (*9)
## Set new values
You can set new values on the fly, which will be available in the Config object during all the script (and not saved into a file config).
```php
use Puppy\Config\Config;
$config = new Config();
$config['new_key'] = 'new_value';
You can also use the dynamic mapping., (*10)
$config['new_key2'] = '%new_key%';
$config['new_key2']; //'new_value'
Visibility restriction
You can determine some namespaces in your config, and restrict the visibility of each namespace., (*11)
For example, imagine you set this config, with two namespaces: 'a' and 'b'., (*12)
//namespace a
$globalConfig['a.a'] = 'a.a';
$globalConfig['a.b'] = 'a.b';
//namespace b
$globalConfig['b.a'] = 'b.a';
$globalConfig['b.b'] = 'b.b';
With the global scope config you can access to all the values., (*13)
$globalConfig['a.a']; //'a.a'
$globalConfig['b.a']; //'b.a'
Now, restrict the config to the namespace 'a':, (*14)
$restrictedConfig = $globalConfig->restrict('a');
With this restricted scope config, you can only have access to the namespace 'a' values:, (*15)
$restrictedConfig['a']; //'a.a'
$restrictedConfig['b']; //'a.b'
The original keys are not visible anymore:, (*16)
isset($restrictedConfig['a.a']); //false
isset($restrictedConfig['b.a']); //false
But, the global scope config is still bound to your restricted scope. So, if you modify one, you modify the other., (*17)
$restrictedConfig['a'] = 'new value';
$globalConfig['a.a']; //'new value'
By default, Config load 'php' files. This kind of file must return a PHP array., (*18)
// /config/global.php
return [
'key' => 'value',
];
But you can specify or add specific file formats., (*19)
new Config('', null, new YmlFileReader()); //will load config/global.yml
Available formats:
- php (default)
- ini
- json
- yaml, (*20)
Multi environment
Your config is the merge of the values coming from three kind of files:, (*21)
type |
file name |
loading |
overloading |
usage |
global config |
global.php |
always loaded |
prod & test |
env config |
e.i. dev.php |
according to the env |
override global config |
dev |
local config |
local.php |
specific for each machine |
override global and env config |
prod & test - dev |
When is loaded the global config?
In all the cases, Config will load the file 'global.php'. (You can easily change this default file.), (*22)
By default, this is your prod config., (*23)
When is loaded the env config?
If you specify an env in the constructor, it will load also the associated file., (*24)
For example:, (*25)
new Config('dev'); //will load dev.php (in addition to global.php)
The env config will override the global config. So, use it for your dev env, which will override your prod params., (*26)
How env can change dynamically?
Set an environment variable in your server virtual host configuration, and retrieve it with the php getenv() method., (*27)
In your httpd.conf or a .htaccess file of your dev Apache server, put:, (*28)
SetEnv APP_ENV "dev"
In your PHP file, retrieve the env:, (*29)
new Config(getenv('APP_ENV')); //will load dev.php only in your dev server
What is the local config?
The config will load also a local config, if the file config/local.php exists., (*30)
This config will override the global and the env configs. Be careful: this file must be NOT versioned., (*31)
So, it is an individual config, where your can put temporary or specific config. Your can also put config you do not want to version, like the passwords., (*32)