2017 © Pedro Peláez
 

library config

Minimal, reusable Config component.

image

brightnucleus/config

Minimal, reusable Config component.

  • Sunday, December 17, 2017
  • by schlessera
  • Repository
  • 2 Watchers
  • 22 Stars
  • 13,979 Installations
  • PHP
  • 15 Dependents
  • 0 Suggesters
  • 3 Forks
  • 5 Open issues
  • 37 Versions
  • 17 % Grown

The README.md

Bright Nucleus Config Component

PHP Version Require Scrutinizer Code Quality Code Coverage Build Status Codacy Badge Code Climate, (*1)

Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

This is a very lean Config component to help you write reusable code. It only offers basic functionality and is meant to be used in libraries and small projects. If you need a Config component for more complex projects, you should take a look at the Symfony Config Component., (*3)

Table Of Contents

Installation

The best way to use this component is through Composer:, (*4)

composer require brightnucleus/config

Basic Usage

A class that wants to be configurable should accept a ConfigInterface in its constructor, so that the Config can be injected. The surrounding code then should inject an instance of an object (for example the generic Config that is provided with this component). This way, the class that accepts the Config can be written in a 100% reusable way, while all project-specific stuff will be injected through the Config., (*5)

Working With Config Data

Checking The Existence Of A Key

To check whether the configuration has a certain key, you can use the ConfigInterface::hasKey($key) method, or, if you are using the ConfigTrait in your class, you can use the convenience method $this->hasConfigKey($key)., (*6)

Getting The Value Of A Key

To get the configuration value of a certain key, you can use the ConfigInterface::getKey($key) method, or, if you are using the ConfigTrait in your class, you can use the convenience method $this->getConfigKey($key)., (*7)

If you use closures in your Config file, you can also use the convenience function $this->getConfigCallable( $key, array $args ) provided by the ConfigTrait, which will immediately execute the closure by passing it the provided arguments, and return the resultant value instead., (*8)

Nested Keys

If your keys are nested, you can provide multiple levels of keys in one request. So, whenever you need to provide a key and want to use a nested one, you can either provide a comma-separated list of keys ( $this->getConfigKey( 'level1', 'level2', 'level3' ); ) or a string that contains the list of keys separated by a delimiter ( $this->getConfigKey( 'level1/level2/level3' ); )., (*9)

You can freely mix-and-match these two approaches as you like., (*10)

The default delimiters are: /, \ and .. You can choose different delimiters by passing an array of delimiters as a fourth argument to the Configs constructor., (*11)

Example - Configuration File

The snippet below shows the basic structure of a config file., (*12)

<?php namespace BrightNucleus\Example;

/*
 * Example class main settings.
 */
$example = [
    'test_key' => 'test_value',
];

return [
    'BrightNucleus' => [
        'Example' => $example,
    ],
];

Example - Configurable Class

Here is an example setup of how you could feed this configuration into a plugin., (*13)

<?php namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigInterface;
use BrightNucleus\Config\ConfigTrait;
use BrightNucleus\Exception\RuntimeException;

class Example {

    use ConfigTrait;

    /**
     * Instantiate an Example object.
     *
     * @param ConfigInterface $config Config to parametrize the object.
     * @throws RuntimeException       If the Config could not be parsed correctly.
     */
    public function __construct( ConfigInterface $config ) {
        $this->processConfig( $config );
    }

    /**
     * Do something.
     */
    public function run() {
        $key = 'test_key';

        return sprintf(
        _( 'The value of the config key "$1%s" is "$2%s".'),
            $key,
            $this->getConfigKey( $key )
        );
    }
}

Example - Getting The Config Into The Class

You can combine all of your configurations into 1 single file and only pass "Sub-Configurations" to the individual components using the getSubConfig() method. This way, you can avoid an additional file access and an additional validation pass for each component., (*14)

To create a new instance of a ConfigInterface, you should use the ConfigFactory. The basic method ConfigFactory::create() can take either an array of values, or one or more file names (with absolute paths) as strings., (*15)

If you provide a comma-separated list of file names, they are processed consecutively until the first one could be loaded successfully., (*16)

There's a convenience function ConfigFactory::createSubConfig() to immediately fetch a sub-config from a loaded config file. This allows you to quickly bypass the vendor/package prefixes and only pass in the relevant data into the new object., (*17)

Here's how you can pass the configuration file into the class:, (*18)

<?php namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigFactory;

function init() {
    $configFile = __DIR__ . '/config/example_settings.php';
    $config     = ConfigFactory::createSubConfig($configFile, 'BrightNucleus\Example');
    $example    = new Example( $config );

    // Outputs:
    // The value of the config key "test_key" is "test_value".
    echo $example->run();
}

Example - Class That Loads Default Config Unless Config Was Injected

The ConfigTrait provides some convenience functionality that lets you write classes that can receive an injected Config, but fall back to a default configuration file if non was injected., (*19)

Here's you can code such a class:, (*20)

<?php namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigInterface;
use BrightNucleus\Config\ConfigTrait;
use BrightNucleus\Exception\RuntimeException;

class Example {

    use ConfigTrait;

    /**
     * Instantiate an Example object.
     *
     * For this constructor, the `$config` argument is optional, and the class will
     * load a default configuration if none was injected.
     *
     * @param ConfigInterface|null $config Optional. Config to parametrize the object.
     * @throws RuntimeException If the Config could not be parsed correctly.
     */
    public function __construct( ConfigInterface $config = null ) {

        // We either process the $config that was injected or fetch a default one.
        $this->processConfig( $config ?: $this->fetchDefaultConfig() );
    }

    /**
     * Get the default configuration file name.
     *
     * This is used to override the default location.
     *
     * @return string Path & filename to the default configuration file.
     */
    protected function getDefaultConfigFile() {
        return __DIR__ . '/../config/my_default_config.php';
    }
}

Example - Merging Several Configs Into One

You can provide a comma-separated list of file names to the ConfigFactory::merge() method. They are loaded consecutively and merged into one coherent Config. For each duplicate Config key, the value in the later files will override the value in the earlier files., (*21)

For our example, we'll define a new Config file called override_settings.php, that overrides a key that was already set in the default Config file., (*22)

<?php namespace BrightNucleus\Example;

/*
 * Example class main settings.
 */
$example = [
    'test_key' => 'override_value',
];

return [
    'BrightNucleus' => [
        'Example' => $example,
    ],
];
<?php namespace BrightNucleus\Example;

use BrightNucleus\Config\ConfigFactory;

function init() {
    $configFile   = __DIR__ . '/config/example_settings.php';
    $overrideFile = __DIR__ . '/config/override_settings.php';
    $config       = ConfigFactory::merge($configFile, $overrideFile);
    $example      = new Example( $config );

    // Outputs:
    // Both files will be loaded, but values from the second override the first.
    // The value of the config key "test_key" is "override_value".
    echo $example->run();
}

Config Formats

The Bright Nucleus Config component can be extended to load a multitude of different file formats. The base package includes a very minimal PHPLoader class. It can load basic PHP files that just return an array., (*23)

Additional packages that add other formats like JSON or YAML are planned and will be released soon., (*24)

Custom loaders are lazily instantiated only when needed., (*25)

Advanced Usage

Configuration Schema

TODO, (*26)

Configuration Validation

TODO, (*27)

Custom Implementations

TODO, (*28)

Contributing

All feedback / bug reports / pull requests are welcome., (*29)

License

This code is released under the MIT license., (*30)

For the full copyright and license information, please view the LICENSE file distributed with this source code., (*31)

The Versions

17/12 2017

dev-master

9999999-dev

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

16/02 2017

v0.4.10

0.4.10.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

16/02 2017

v0.4.9

0.4.9.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

06/08 2016

v0.4.8

0.4.8.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

22/07 2016

v0.4.7

0.4.7.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

22/07 2016

v0.4.6

0.4.6.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

05/07 2016

v0.4.5

0.4.5.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

05/07 2016

v0.4.4

0.4.4.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

05/07 2016

v0.4.3

0.4.3.0

Minimal, reusable Config component.

  Sources   Download

MIT

The Requires

 

The Development Requires

09/06 2016
08/06 2016
22/03 2016
26/02 2016
19/02 2016

dev-nested-options

dev-nested-options

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

19/02 2016
18/02 2016
18/02 2016
17/02 2016
17/02 2016
17/02 2016
16/02 2016
16/02 2016

v0.1.9

0.1.9.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

11/02 2016

v0.1.8

0.1.8.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

11/02 2016

v0.1.7

0.1.7.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

11/02 2016

v0.1.6

0.1.6.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

11/02 2016

v0.1.5

0.1.5.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

11/02 2016

v0.1.4

0.1.4.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

10/02 2016

v0.1.3

0.1.3.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

01/02 2016

0.1.2

0.1.2.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

29/01 2016

0.1.1

0.1.1.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

29/01 2016

0.1.0

0.1.0.0

Minimal, reusable Config component.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires