-
One central place for all config files, (*5)
config/
|
|____ production/
| |
| |____ host1/
| | |___ redis.php
| | |___ db.php
| |
| |____ db.php
|
|____ system.php
|____ db.php
|____ redis.php
-
Use an environment value, e.g. production
or production/host1
for
switching between development
, staging
or production
., (*6)
-
Support .php
, .json
, .ini
and .xml
type of configuration files., (*7)
-
Reference is possible, such as ${system.tmpdir}
in configuration
file and environment file., (*8)
-
On demand configuration loading (lazy loading)., (*9)
-
Able to load all configuration files in one shot with $config->get(null)
, (*10)
-
Configuration cache., (*11)
-
Hierachy configuration structure with dot notation like db.auth.host
., (*12)
// returns an array
$db_config = $config->get('db');
// returns a string
$db_host = $config->get('db.auth.host');
-
Both flat notation and array notation fully supported and co-exist at the
same time., (*13)
// db config file
return [
// array notation
'auth' => [
'host' => 'localhost',
'port' => 3306
],
// flat notation
'auth.user' => 'dbuser'
];
-
Un*x shell style environment file '.env' is supported with dereferencing
feature and magic environment values like ${__DIR__}
and ${__FILE__}
, (*14)
-
Running Environment, (*17)
Usually running environment is different for different servers. A good
practice is setting environment in a .env
file in the installation root
and put all configuration files in the config/
directory., (*18)
Sample .env
file,, (*19)
# debugging true|false, change to 'false' ON production server
APP_DEBUG=true
# App environment, change to 'prod' ON production server
APP_ENV=dev
# app root directory, default to current dir
APP_ROOT=${__DIR__}
# central configuration directory
CONFIG_DIR=${APP_ROOT}/config
In the bootstrap.php
file,, (*20)
// load environment
(new Phossa\Config\Env\Environment())->load(__DIR__.'/.env');
// create config object
$config = new Phossa\Config\Config(
getenv('CONFIG_DIR'), // loaded from .env file
getenv('APP_ENV') // loaded from .env file
);
// load all configs in one shot
$conf_data = $config->get(null);
-
Grouping, (*21)
Configurations are grouped into groups, namely files. For example, the
system.php
holds all system.*
configurations, (*22)
// system.php
return [
'tmpdir' => '/usr/local/tmp',
...
];
Later, system related configs can be retrieved as, (*23)
$dir = $config->get('system.tmpdir');
Or being used in other configs as reference., (*24)
-
Caching, (*25)
A cache pool can be passed to the config constructor to have it read all
configs from the cache or save all configs to cache., (*26)
// create config object
$config = new Phossa\Config\Config(
dirname(__DIR__) . '/config', // the config dir
'staging/server2', // config env
'php', // file type
new Phossa\Config\Cache\Cache(__DIR__ . '/cache') // cache location
);
// if cache exists, this will read all configs from the cache
$conf_data = $config->get(null);
// ...
// write to cache
$config->save();
-
Pros of using caching, (*27)
-
Speed up. Read from one file instead of lots of configuration files., (*28)
-
References like ${system.tmpdir}
are done already., (*29)
-
Cons of using caching, (*30)
-
Config data might be stale. need to using $config->save()
to overwrite
or $cache->clear()
to clear the cache., (*31)
-
Need write permission to a cache directory., (*32)
-
Might expose your configuration if you are not careful with cache data., (*33)
-
Reference, (*34)
References make your configuration easy to manage., (*35)
For example, in the system.php
, (*36)
// group: system
return [
'tmpdir' => '/var/local/tmp',
...
];
In your cache.php
file,, (*37)
// group: cache
return [
// a local filesystem cache driver
'local' => [
'driver' => 'filesystem',
'params' => [
'root_dir' => '${system.tmpdir}/cache',
'hash_level' => 2
]
],
...
];
You may reset the reference start and ending chars,, (*38)
// now reference is something like '%system.tmpdir%'
$config = (new Config())->setReferencePattern('%', '%');
Or even disable the reference feature,, (*39)
// now reference is not recognised
$config = (new Config())->setReferencePattern('', '');
-
Overwriting, (*40)
If the environment is set to production/host1
, the precedence order is,, (*41)
-
config/production/host1/db.php
over, (*42)
-
config/production/db.php
over, (*43)
-
config/config/db.php
, (*44)
-
Config API, (*45)
get($key, $default = null)
$key
is the a flat notation like db.auth.host
or NULL
to get all of
the configurations. $default
is used if no configs found., (*46)
Return value might be a string
or array
base on the $key
., (*47)
Set the configuration manually in this session. The value will NOT
be reflected in any config files unless you modify config file manually., (*48)
$value
can be a string
or array
., (*49)
Test if $key
exists or not. Returns a boolean
value., (*50)
Save current full configurations into a cache., (*51)