



Configuration v1.2.0 documentation

[![logo](_static/img/logo.png)](index.html#document-index)

* [Repository](https://github.com/slickframework/configuration)

* [Getting started](index.html#document-manual/getting-started)

	+ [Basic usage](index.html#basic-usage)

	+ [Creating a Configuration](index.html#creating-a-configuration)

	+ [Retrieving values](index.html#retrieving-values)

	+ [Default values](index.html#default-values)

* [Configuration Chain](index.html#document-manual/multiple-configurations)

	+ [Priority Configuration Chain](index.html#priority-configuration-chain)

	+ [Combined configuration](index.html#combined-configuration)

* [Configuration interface](index.html#document-api/configuration)

* [Contributing](index.html#document-manual/contrib)

	+ [Pull requests](index.html#pull-requests)

	+ [Running tests](index.html#running-tests)

	+ [Security](index.html#security)

* [License](index.html#document-manual/license)

Slick Configuration[¶](#slick-configuration "Permalink to this headline")

=========================================================================

`slick/configuration` is a simple package that deals with configuration files.

It has a very simple interface that you can use to set your own configuration

drivers. By default it uses the PHP arrays for configuration as it does not need

any parser and therefore is more performance friendly.

Installation[¶](#installation "Permalink to this headline")

-----------------------------------------------------------

`slick/configuration` is a php 5.6+ library that you’ll have in your project development

environment. Before you begin, ensure that you have PHP 5.6 or higher installed.

You can install `slick/configuration` with all its dependencies through Composer. Follow

instructions on the [composer website](https://getcomposer.org/download/) if you don’t have it installed yet.

You can use this Composer command to install `slick/configuration`:

```

$ composer require slick/configuration

```

### Getting started[¶](#getting-started "Permalink to this headline")

To create a `ConfigurationInterface` you should use the `Slick\Configuration` factory class

as it really helps you with the creation process.

#### Basic usage[¶](#basic-usage "Permalink to this headline")

Lets start by creating a configuration file:

```

<?php

/\*\*

 \* App configuration file

 \*/

namespace settings;

$settings = [];

$settings['application'] = [

    'version' => 'v1.0.0',

    'environment' => 'develop'

];

return $settings;

```

we save this file as `./settings.php`.

We are using plain PHP arrays for configuration files. Don’t forget to add the

`return` statement at the end of the file so that the defined array could be

assigned when initializing the driver.

#### Creating a Configuration[¶](#creating-a-configuration "Permalink to this headline")

Now we will use the `Slick\Configuration\Configuration` factory o create our

`Slick\Configuration\ConfigurationInterface`:

```

use Slick\Configuration\Configuration;

$settings = Configuration::get('settings');

```

Its really simple.

#### Retrieving values[¶](#retrieving-values "Permalink to this headline")

Now lets use it.

```

print\_r($settings->get('application'));

# the output form above is:

# Array (

# [version] => v1.0.0,

# [environment] => develop

# )

```

You can set any level of nesting in your configuration array but as you add another

level to the array it becomes harder to use. Please check the example bellow:

```

$value = $settings->get('application')['version'];

// OR

$appSettings = $settings->get('application');

$value = $appSettings['version'];

```

To simplify you ca use a “dot notation” to reach a deeper level.

```

$value = $settings->get('application.version');

```

#### Default values[¶](#default-values "Permalink to this headline")

It is possible to have a default value when no key is found on a configuration driver. By

default if a key is not found a `NULL` is returned but if you specify a value it will

be returned by the `ConfigurationInterface::get()` method:

```

$value = $settings->get('application.rowsPerPage', 10);

print $value;

# the output form above is:

# 10

```

### Configuration Chain[¶](#configuration-chain "Permalink to this headline")

Starting form v1.2.0, `Slick\Configuration` is capable of combine multiple configuration

drivers with a single configuration interface. This allows you to add a more important configuration

source (like environment variables) to be also check when you try to retrieve a configuration value.

#### Priority Configuration Chain[¶](#priority-configuration-chain "Permalink to this headline")

v1.2.0 added a `ConfigurationChainInterface` that allows clients to retrieve a value from a combined

chain of `ConfigurationInterface` objects instead of a single configuration source.

It also adds a `PriorityConfigurationChain` that implements `ConfigurationChainInterface` and its

by default the returned value from `Configuration::initialize()` or `Configuration::get()`

factory methods.

The priority is given by a integer value that determines the order that a key is searched in the chain.

Lower value will be checked first.

#### Combined configuration[¶](#combined-configuration "Permalink to this headline")

Lets try an example. This is our PHP file with an associative array with configuration settings:

```

<?php

/\*\*

 \* App configuration file

 \*/

namespace settings;

$settings = [];

$settings['application'] = [

    'version' => 'v1.0.0',

    'environment' => 'develop'

];

return $settings;

```

Now we will create a combined configuration that will have a `Environment` driver and a `Php` with

the values from the file we have just create.

```

use Slick\Configuration\Configuration;

$settings = Configuration::get([

    [null, Configuration::DRIVER\_ENV, 10],

    ['settings', Configuration::DRIVER\_PHP, 20]

]);

```

This configuration setup will create the `Environment` driver as the first configuration driver that

will be check and then, if not found, the `Php` one.

Lets assume that we has define and environment variable as `APPLICATION\_VERSION=v1.2.3` and lets get

that value from the configuration chain:

```

print\_r($settings->get('application.version'));

# the output form above is:

# v1.2.3

```

You can combine any number of configuration drivers in one chain and set the priority on with the

search will occur.

Tip

The example shown here is a very simple way of handling environment variables that can be set

on Docker containers where you some times can’t create files.

### Configuration interface[¶](#namespace-Slick\Configuration "Permalink to this headline")

*class* `Slick\Configuration\``ConfigurationInterface`[¶](#Slick\Configuration\ConfigurationInterface "Permalink to this definition")

ConfigurationInterface, defines a configuration driver

`Slick\Configuration\ConfigurationInterface::``get`(*$key*[, *$default = NULL*])[¶](#Slick\Configuration\ConfigurationInterface::get "Permalink to this definition")

Returns the value store with provided key or the default value.

| Parameters: | * **$key** (*string*) – The key used to store the value in configuration.

* **$default** (*mixed*) – The default value if no value was stored.

 |

| Returns: | The configuration stored value or the default if not found. |

`Slick\Configuration\ConfigurationInterface::``get`(*$key*, *$value*)

Set/Store the provided value with a given key.

| Parameters: | * **$key** (*string*) – The key used to store the value in configuration.

* **$value** (*mixed*) – The value to store under the provided key.

 |

| Returns: | Self instance for method call chains. |

### Contributing[¶](#contributing "Permalink to this headline")

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/slickframework/configuration).

#### Pull requests[¶](#pull-requests "Permalink to this headline")

* [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) - Check the code style with `$ composer check-style` and fix it with `$ composer fix-style`.

* **Add tests!** - Your patch won’t be accepted if it doesn’t have tests.

* **Document any change in behaviour** - Make sure the README.md and any other relevant documentation are kept up-to-date.

* **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org). Randomly breaking public APIs is not an option.

* **Create feature branches** - Don’t ask us to pull from your master branch.

* **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

* **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.

#### Running tests[¶](#running-tests "Permalink to this headline")

We use [phpspec](http://www.phpspec.net) for unit tests.

```

# unit tests

$ vendor/bin/phpspec run -fdot

```

#### Security[¶](#security "Permalink to this headline")

If you discover any security related issues, please email

[slick.framework@gmail.com](mailto:slick.framework%40gmail.com) instead of using the issue tracker.

### License[¶](#license "Permalink to this headline")

Licensed using the [MIT license](http://opensource.org/licenses/MIT).

> 

> Copyright (c) 2014-2017 [The Slick Team](https://github.com/orgs/slickframework/people) <[slick.framework@gmail.com](mailto:slick.framework%40gmail.com)>

> 

> 

> Permission is hereby granted, free of charge, to any person obtaining a copy

> of this software and associated documentation files (the “Software”), to deal

> in the Software without restriction, including without limitation the rights

> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

> copies of the Software, and to permit persons to whom the Software is

> furnished to do so, subject to the following conditions:

> 

> 

> The above copyright notice and this permission notice shall be included in

> all copies or substantial portions of the Software.

> 

> 

> THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

> THE SOFTWARE.

> 

> 

> 

---

Please enable JavaScript to view the [comments powered by Disqus.](https://disqus.com/?ref_noscript)

