Allows easy, interfaceable class configurator with the ability to change the configuration during runtime

2.0.1 2016-11-07 21:11 UTC

This package is not auto-updated.

Last update: 2024-12-21 20:13:54 UTC


README

A component that allows easy object configuration with default and overriding settings

Latest release API documentation: https://aloframework.github.io/config/

License Latest Stable Version Total Downloads

Installation

Installation is available via Composer:

composer require aloframework/config

The abstract class

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.

The interface

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.

The trait

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.

Updating the configuration

Setting an item

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'.

Removing an item

You can remove custom configuration via $config->remove('myKey'), or by unsetting it like an array value: unset($config['myKey']);

Reading the configuration

Specific value

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'].

The merged config

You can retrieve the merged array via $config->getAll()

The default config

For this you would use $config->getDefaultConfig()

The custom overrides

For this you would use $config->getCustomConfig().