minond / configurare
Configuration management
Requires
- php: >=5.4.0
- minond/cache: 1.*
- minond/utilitatis: 1.*
- symfony/yaml: 2.*
Requires (Dev)
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-12-16 15:38:21 UTC
README
Sample usage
Initializing configuration
use Efficio\Configurare\Configuration; $conf = new Configuration; $conf->setDirectory('./config/');
# config/app.yml name: 'My Application' usa: utah: provo: author: 'Marcos Minond'
Configuring configuratio file formats and parsers
// available parsers use Efficio\Configurare\Parser\Json; $conf->setExtension('.json'); // default is '.yml' $conf->setParser(new Json); // default is Efficio\Configurare\Parser\Yaml
Using custom formats
use Efficio\Configurare\Parser\Parser; class CustomParser implements Parser { /** * takes a raw string, parses it, and returns the array representing the * data * @param string $raw * @return array */ public function decode($raw) { return unserialize($raw); } /** * takes an array or an object and converts it into a string that can be * saved in a file * @param mixed $obj * @return string */ public function encode($obj) { return serialize($obj); } } $conf->setExtension('.custom'); $conf->setParser(new CustomParser);
Getting values
// looks for in ./config/app.yml // this gets [ 'name': ] echo $conf->get('app:name'); // => My Application // this gets [ 'usa': 'utah': 'provo': 'author': ] echo $conf->get('app:usa:utah:provo:author'); // => Marcos Minond // you can also get nested configuration files // looks in config/users/2014/jan.yml echo $conf->get('users/2014/jan:activities:music');
Setting values
// if a key(s) already exists, just set it $conf->set('app:name', 'My Other Application'); // if they do not then the write must be forced by passing a third parameter // set to true $conf->set('app:does:not:exists:yet', 'yes', true);
# config/app.yml name: 'My Other Application' usa: utah: provo: author: 'Marcos Minond' does: not: exists: yet: 'yes'
Enviroments
Adding enviroments allows additional configuration files, which may or may not be tracked by version control, to be used. For example, you may commit a "default" config/app.yml configuration file which makes assumptions about the enviroments (ie. database connection information) and overwrite it using config/app.prod.yml. This "prod" file has data which is sensative and is only stored in the production server where your application is running. This allows you to use the same configuration retrieval code, get the correct configuration for your enviroments, AND not have to check that into source control.
// I can have one enviroment or multiple $conf->setEnvironments([ 'dev', 'test' ]); // the following files will be parsed and merged before the configuration // value is sent back // - config/database.yaml // - config/database.dev.yaml // - config/database.test.yaml $conf->get('database:connection:username');
Caching
Configurare is compatible with the Cache package
use Efficio\Cache\NullCache; $conf->setCache(new NullCache);