https://github.com/sunchayn/simple-2way-config
Simple 2 way (read/write) php-based configuration
https://github.com/sunchayn/simple-2way-config
config configuration php two-way-config
Last synced: 3 months ago
JSON representation
Simple 2 way (read/write) php-based configuration
- Host: GitHub
- URL: https://github.com/sunchayn/simple-2way-config
- Owner: sunchayn
- License: mit
- Created: 2018-11-12T23:35:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-14T20:13:32.000Z (over 7 years ago)
- Last Synced: 2026-03-21T23:28:25.497Z (4 months ago)
- Topics: config, configuration, php, two-way-config
- Language: PHP
- Size: 16.6 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
## mazentouati/simple-2way-config
[](https://github.com/mazentouati/simple-2way-config/releases/tag/0.1.0)
[](https://travis-ci.org/mazentouati/simple-2way-config)
[](https://scrutinizer-ci.com/g/mazentouati/simple-2way-config/?branch=master)
[](https://codecov.io/gh/mazentouati/simple-2way-config)
[](https://styleci.io/repos/157292080)
[](https://codeclimate.com/github/mazentouati/simple-2way-config/maintainability)
[](./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](http://getcomposer.org/) :
```bash
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.
```php
use MazenTouati\Simple2wayConfig\S2WConfigFactory;
$config = S2WConfigFactory::create( __DIR__ . '/demo' );
```
Now you can access to a config value using dot notation '{filename}.path.to.value'
```php
$host = $config->get('database.drivers.mysql.host');
```
*Note: your config file should be an array-based configuration, check this [example](#configuration-file-formats)*
## 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`
```php
$config->get('database.drivers.mysql.host');
```
optionally you can pass a default value to return when it find nothing, by default it returns `null`
```php
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.*
```php
$config->set('database.drivers.mysql.host', '127.0.0.1');
```
### sync(mixed $specificConfiguration = false)
Syncs the runtime configuration with the source file
```php
$config->sync();
```
by default it will sync all files, though you can pass a specific file to sync
```php
$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
```php
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
'mysql',
'drivers' => [
'mysql' => [
'host' => 'your_host',
'dbname' => 'your_database',
'user' => 'your_user',
'password' => 'your_password',
],
],
];
```
## Contributing
Please check [the guide](./CONTRIBUTING.md)
## LICENSE
> © [MIT](./LICENSE) | 2018, mazentouati/simple-2way-config