A component that allows easy object configuration with default and overriding settings
Latest release API documentation: https://aloframework.github.io/config/
dev-develop | Latest release |
---|---|
Installation is available via Composer:
composer require aloframework/config
AloFramework\Config\AbstractConfig
is what your own library configurations should extend. It has three main private properties: $defaults
holding default configuration, $custom
holding custom configuration/overrides and $merged
, which holds the actual configuration that will be used. Your overriding config class will populate the default array and accept any custom variables in the constructor, e.g.:
use AloFramework\Config\AbstractConfig;
class MyConfig extends AbstractConfig {
private static $defaultConfig = ['foo' => 'bar',
'importantVar' => 'andItsValue'];
function __construct($customConfig = []) {
parent::__construct(self::$defaultConfig, $customConfig);
}
}
When reading the configuration, the values are fetched from the $merged
array, which is essentially an array_merge($this->defaults, $this->custom)
. For more information refer to the API documentation above.
You can implement the AloFramework\Config\Configurable
interface in your configuration-reading class to indicate that its runtime settings can be altered using this package. The trait described below can be used to implement the required methods.
If you don't want to write your own methods you can simply include the provided AloFramework\Config\ConfigurableTrait
which will implement all the methods required by the interface.
You can add a custom configuration key or default setting override by calling $config->set('myKey', 'myValue')
, using __set()
like $config->myKey = 'myValue'
, or simply using it like an array: $config['myKey'] = 'myValue'
.
You can remove custom configuration via $config->remove('myKey')
, or by unsetting it like an array value: unset($config['myKey']);
You can retrieve a specific configuration item from the merged array by calling $config->get('myKey')
, using __get()
like $config->myKey
or by using the object like an array: $config['myKey']
.
You can retrieve the merged array via $config->getAll()
For this you would use $config->getDefaultConfig()
For this you would use $config->getCustomConfig()
.