The Config

The Config Core Library, read and write the configuration files.

Internal configuration files

The internal configuration files are saved inside the "Config" directory. The configuration files are internal because when your app is converted into a distributable PHAR file, the internal configuration files are still available inside the PHAR file.

By default Mamuph has the following three internal configuration files:

  • Modules: Contains the list of activated modules
  • Params: Contains the list of accepted parameters/arguments
  • Version: Contains the application version information

By default the internal config directory is loaded by Mamuph using a default "Config" object. The default "Config" object is instantiated in the Bootstrap.php file. The internal configuration files can be loaded using the "load" method. Example:

    $version = Config::instance()->load('version')->as_array();

It is not recommended to save inside the "Config" directory those configuration files that are liable to changes such database configuration files. It is recommended to save this kind of files in an external and safe location.

Instantiation & Driver attachment

By default Mamuph already create an instance inside Bootstrap.php file. It means that if you want to read additional configuration files you should pass an unique instance identificator to your "Config" instance when the singleton method is used. Example:

    Config::instance('external_configuration);

After than new Config instance is created it is possible to attach two different drivers.

FileReader Driver

The "FileReader" driver reads the configuration file from a specify directory.

The class for this driver is: FileReader

Parameters

  • directory (string): The directory where the configuration files are located (By default the "Config" directory is used when this parameter is not passed)

Example

    Config::instance('external_conf')->attach(new FileReader('/etc/'));

FileWriter Driver

The "FileWriter" driver writes into the configuration files located into a specify directory.

The class for this driver is: FileWriter

Parameters

  • directory (string): The directory where the configuration files are located (By default the "Config" directory is used when this parameter is not passed)

Example

    Config::instance('external_conf')->attach(new FileWriter('/home/user/configs/'));

Multiple Drivers

It is possible to attach multiple drivers in the following way:

    // Attach FileReader and FileWriter
    // Both drivers can read and write into the same config files
    Config::instance('external_conf')
        ->attach(new FileReader('/home/user/configs/'))
        ->attach(new FileWriter('/home/user/configs/'));

    // Attach FileReader and FileWriter
    // The FileWriter is going to write the configuration into a different location
    Config::instance('external_conf'
        ->attach(new FileReader('/home/user/configs/'))
        ->attach(new FileWriter('/home/user/new_configs/'));

    // Attach the FileWriter to the default instance
    // Now it is possible to write into the internal configuration directory (Config)
    Config::instance()->attach(new FileWriter());

How to read and write configuration files

Read

It is possible to read the configuration using the method "load".

Example

    // Read the configuration as array
    $version = Config::instance()->load('Version')->as_array();

    // Read the major version
    $major = Config::instance()->load('Version.major');

Write

It is possible to write the configuration using the method "set" or modiying the configuration group property.

Example

    // First we to read the current configuration
    $version = Config::instance()->load('Version');

    // Increase minor version        
    $version->set('minor' $version->minor + 1);

    // or just      
    $version->minor++;

Driver detachment

It is possible to attach and detach drivers on run-time:

Example

    Config::instance('external_conf')->detach(new FileWriter());