PHPackages                             mazentouati/simple-2way-config - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mazentouati/simple-2way-config

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mazentouati/simple-2way-config
==============================

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

0.1.0(7y ago)979[1 issues](https://github.com/sunchayn/simple-2way-config/issues)2MITPHPPHP &gt;=7.0.0CI failing

Since Nov 13Pushed 7y ago2 watchersCompare

[ Source](https://github.com/sunchayn/simple-2way-config)[ Packagist](https://packagist.org/packages/mazentouati/simple-2way-config)[ RSS](/packages/mazentouati-simple-2way-config/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (2)Versions (2)Used By (2)

mazentouati/simple-2way-config
------------------------------

[](#mazentouatisimple-2way-config)

[![GitHub (pre-)release](https://camo.githubusercontent.com/df3cf83972d77d7412d7c786ce45949fe672fdfb4d54ddaec7f27e203cd81ab8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d7072652f6d617a656e746f756174692f73696d706c652d327761792d636f6e6669672e737667)](https://github.com/mazentouati/simple-2way-config/releases/tag/0.1.0)[![Build Status](https://camo.githubusercontent.com/8cbf7909b10be0b3e4c1c73f97146b3587d55e9b59dff750519722131f07f567/68747470733a2f2f7472617669732d63692e6f72672f6d617a656e746f756174692f73696d706c652d327761792d636f6e6669672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mazentouati/simple-2way-config)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f132a7cfe9e871207ae5a9dbf2522f6d2ede3837b75750a1a4681abe08ea20a6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d617a656e746f756174692f73696d706c652d327761792d636f6e6669672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mazentouati/simple-2way-config/?branch=master)[![Codecov branch](https://camo.githubusercontent.com/47e2cf7131e8b6ec047a747c806a36cec0451857915e6d215403e03b236034f2/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6d617a656e746f756174692f73696d706c652d327761792d636f6e6669672f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/gh/mazentouati/simple-2way-config)[![StyleCI](https://camo.githubusercontent.com/21c0752c1b8eac9f4f1045dfccbaf446e68a50b30fef37cc42687f307a2c05ca/68747470733a2f2f7374796c6563692e696f2f7265706f732f3135373239323038302f736869656c64)](https://styleci.io/repos/157292080)[![Maintainability](https://camo.githubusercontent.com/83deddd34c3608d6b95680d73ef299eb2ed0f832082bf1a27d088636f07ae7bf/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38663731626130333533363335633766343335302f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/mazentouati/simple-2way-config/maintainability)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](./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
------------

[](#installation)

we recommend installing this package through [composer](http://getcomposer.org/) :

```
composer require mazentouati/simple-2way-config
```

Usage
-----

[](#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](#configuration-file-formats)*

API
---

[](#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)

[](#getstring-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)

[](#setstring-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)

[](#syncmixed-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
==========================

[](#configuration-file-formats)

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

```
