slick/configuration

Slick/Configuration is a simple package that deals with configuration files.

v2.0.2 2022-04-04 15:32 UTC

This package is auto-updated.

Last update: 2024-04-04 19:54:53 UTC


README

GitHub release (latest SemVer) Software License GitHub Workflow Status Quality Score Total Downloads

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.

This package is compliant with PSR-2 code standards and PSR-4 autoload standards. It also applies the semantic version 2.0.0 specification.

Install

Via Composer

$ composer require slick/configuration

For PHP <= 7 you should use the following:

$ composer require slick/configuration:^1.2

Usage

Let's 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

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

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

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

Please check documentation site for a complete reference.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email slick.framework@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.