Read and write configuration parameters to the YAML file.
Add dependency by Composer
$ composer require mikeevstropov/configuration
As example we have following configuration file.
# app/config/config.yml
my_parameter: my_value
What we can do with it?
<?php
namespace Mikeevstropov\Configuration\Configuration;
// origin configuration
$originFile = 'app/config/config.yml';
// modified configuration
$modifiedFile = 'var/temp/config.yml';
// create instance
$configuration = new Configuration(
$originFile,
$modifiedFile
);
// get parameter
$value = $configuration->get('my_parameter'); // my_value
// set parameter
// and save it to the file of modified configuration
$configuration->set('my_parameter', 'new_value');
// check it
$configuration->get('my_parameter'); // new_value
// now we can remove instance or exit form runtime
unset($configuration);
// create instance again
$configuration = new Configuration(
$originFile,
$modifiedFile
);
// and check saved value
$value = $configuration->get('my_parameter'); // new_value
// it has
$configuration->has('my_parameter'); // true
// remove
$configuration->remove('my_parameter');
// not defined
$configuration->has('my_parameter'); // false
// also you can use "strict getter" to get existed value
// or thrown InvalidArgumentException if not
$configuration->getStrict('my_parameter'); // thrown InvalidArgumentException
// "strict getter" provide type hinting by second argument
// and thrown InvalidArgumentException if does not match
$configuration->getStrict('my_parameter', 'array'); // thrown InvalidArgumentException
// method getAssert provide assertion behaviour by method
// from Webmozart\Assert by second argument
$configuration->getAssert('my_parameter', 'nullOrStringNotEmpty'); // new_value
// arguments following the second will be passed to
// the assertion method from Webmozart\Assert
$configuration->getAssert('my_parameter', 'range', 10, 20); // thrown InvalidArgumentException
As you can see, when we are using setter the value will saved to the file of modified configuration and loaded next time ass well.
Clone
$ git clone https://github.com/mikeevstropov/configuration.git
Go to project
$ cd configuration
Install dependencies
$ composer install
Set the permissions
$ sudo chmod 777 ./var -v -R
Try to test (PHPUnit is required in global scope)
$ composer test