mazentouati/simple-2way-config

Simple 2 way (read/write) php-based configuration.

0.1.0 2018-11-13 14:51 UTC

This package is auto-updated.

Last update: 2024-04-18 17:15:56 UTC


README

GitHub (pre-)release Build Status Scrutinizer Code Quality Codecov branch StyleCI Maintainability Software License

Simple 2 way configuration is a php-based read and write configuration library. It's suitable for applications that require the use of file system to store preferences or configuration.

Installation

we recommend installing this package through composer :

composer require mazentouati/simple-2way-config

Usage

The simplest way to use it is through the package's factory. The factory's required parameter is the path of the directory that holds your config files.

use MazenTouati\Simple2wayConfig\S2WConfigFactory;

$config = S2WConfigFactory::create( __DIR__ . '/demo' );

Now you can access to a config value using dot notation '{filename}.path.to.value'

$host = $config->get('database.drivers.mysql.host');

Note: your config file should be an array-based configuration, check this example

API

the config API implements the S2WConfigInterface.

the examples shown below will assume that you already assigned your config to a variable called $config

get(string $path, mixed $default = null)

Get a value using dot-notation path using this convention {filename}.path.to.value

$config->get('database.drivers.mysql.host');

optionally you can pass a default value to return when it find nothing, by default it returns null

echo $config->get('somewhere.where.are.you', 'here');
> here

set(string $path, mixed $value)

Update a value in the runtime configuration.

Note: if it's unable to find the config's filename it will create a new key for that filename in the runtime instance. The same for values, if there's any missing part in the dot path it will automatically create it in the runtime instance.

$config->set('database.drivers.mysql.host', '127.0.0.1');

sync(mixed $specificConfiguration = false)

Syncs the runtime configuration with the source file

$config->sync();

by default it will sync all files, though you can pass a specific file to sync

$config->sync('database');

using sync will create a backup file to stay safe if something wrong happen. the backup will create alongside the original file holding this name {original_file_name}.backup.php. if the backup fails due to a lack of permission ( it uses PHP copy function) it will throw an exception S2WConfigException. To avoid any ugly expections errors you can use sync this way

use MazenTouati\Simple2wayConfig\S2WConfigException;
...
try {
    $config->sync();
} catch (S2WConfigException $e) {
    die($e->getMessage());
}

in case of expection this code will print something like

Configuration sync is unable to save a backup for { path_to_directory\database.php }, please check your permissions

Note: this method will sync any news values or updated values you made using the set method. Even if you set inexistent config file into the runtime configuration, using set, this method will create that file for you. Use it with caution if you don't want any unwanted behavior

Configuration File Formats

The configuration file must be a valid php file and return a valid array.

<?php
return [
    'driver' => 'mysql',
    'drivers' => [
        'mysql' => [
            'host' => 'your_host',
            'dbname' => 'your_database',
            'user' => 'your_user',
            'password' => 'your_password',
        ],
    ],
];

Contributing

Please check the guide

LICENSE

© MIT | 2018, mazentouati/simple-2way-config