Download the PHP package mazentouati/simple-2way-config without Composer
On this page you can find all versions of the php package mazentouati/simple-2way-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mazentouati/simple-2way-config
More information about mazentouati/simple-2way-config
Files in mazentouati/simple-2way-config
Package simple-2way-config
Short Description Simple 2 way (read/write) php-based configuration.
License MIT
Informations about the package simple-2way-config
mazentouati/simple-2way-config
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 :
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.
Now you can access to a config value using dot notation '{filename}.path.to.value'
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
optionally you can pass a default value to return when it find nothing, by default it returns null
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.
sync(mixed $specificConfiguration = false)
Syncs the runtime configuration with the source file
by default it will sync all files, though you can pass a specific file to sync
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
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.
Contributing
Please check the guide
LICENSE
© MIT | 2018, mazentouati/simple-2way-config