2017 © Pedro Peláez
 

library config

Simple configuration loader.

image

weew/config

Simple configuration loader.

  • Sunday, November 6, 2016
  • by weew
  • Repository
  • 1 Watchers
  • 0 Stars
  • 229 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 36 Versions
  • 0 % Grown

The README.md

Configuration made simple

Build Status Code Quality Test Coverage Version Licence, (*1)

Table of contents

Installation

composer require weew/config, (*2)

Loading configurations

The config loader is responsible for loading of the configuration files. All you have to do is to provide a location(file or directory) where it can find your configs. It will scan the locations(s) for configuration files and return you a config object., (*3)

$loader = new ConfigLoader();
$loader->addPath('path/to/my/config');
$config = $loader->load();

Accessing configurations

You can easily access config values by the config key., (*4)

// get config value
$config->get('key', 'defaultValue');

// set config value
$config->set('key', 'value');

// check if config is set
$config->has('key');

// remove config value
$config->remove('key');

// check if config is set, throws MissingConfigException
// optionally a type may be specified (string, int, array, etc..)
$config
    ->ensure('key', 'errorMessage')
    ->ensure('stringNode', 'errorMessage', 'string')
    ->ensure('arrayNode', 'errorMessage', 'array');

Configuration formats

Following formats are supported out of the box., (*5)

Plain PHP format example:, (*6)

return [
    'key' => 'value',
    'list' => [
        'key' => 'value',
    ],
];

INI format example:, (*7)

key = value

[list]
key = value

YAML format example:, (*8)

key: 'value'
list:
    key: value

Json format example:, (*9)

{
    "key": "value",
    "list": {
        "key": "value",
    }
}

References

You can always reference to other config values from within a config file. To create a reference, simple wrap a config key with curly braces {config.key}`., (*10)

// config1
return [
    'list' => [
        'foo' => 'bar'
    ]
];

// config2
return [
    'reference' => 'foo {list.foo}'
];

// returns 'foo bar'
$config->get('reference');

You can even reference whole config blocks., (*11)

// config1
return [
    'list' => [
        'foo' => 'bar'
    ]
];

// config2
return [
    'reference' => '{list}'
];

// returns ['foo' => 'bar']
$config->get('reference');

Now when you access the reference value you will get "bar" in return. Keep in mind that references are interpolated at access time (when you call $config->get()). This means that if you change a config value, everyone who references it will receive it's updated value and not the old one., (*12)

Runtime config

Sometimes you might want to apply runtime config from an array or similar that also has a higher priority as the config loaded from the filesystem., (*13)

$loader->addRuntimeConfig(['my' => 'config']);

// or

$loader->addRunetimeConfig(new Config(['my' => 'config']));

Environments

Often you want to split your configurations in multiple files or directories and load them depending on your current environment. This can be achieved trough the environment settings., (*14)

Setting an environment

Out of the box it comes with support for dev, test and prod environments. Custom environments can be added on the fly., (*15)

$loader->setEnviroonment('prod');

How it works

To understand how environments detection works, lets take a look at this directory structure:, (*16)

- test
    - db.php
- prod
    - db.php
- config.php
- config_test.php
- config_prod.php

In the test environment only the "test" directory and it's contents, "config_test.php" and "config.php" will be loaded. In the prod environment however, it will load only the "prod" directory, "config_prod.php" and "config.php"., (*17)

Files and folders that have been added to the config loader will be loaded in the order of registration. Inside directories, files are loaded alphabetically., (*18)

Adding custom environments

To create your own environments you'll have to register a new rule on the environment detector. The first argument is the name of the environment and the second is an array of masks., (*19)

$loader->getEnvironmentDetector()
    ->addEnvironmentRule('integ', ['integ', 'integration', 'stage']);

Below is a list of some files and directories that would match the integration environment:, (*20)

- stage
- _stage
- _stage_
- foo_stage
- _stage.txt
- foo_stage.txt

This files will not match:, (*21)

- stagefoo
- foostage
- foo_stage_bar
- _stagebar

As delimiter you can use any of this characters: ._+:-., (*22)

Ignoring files

This files are ignored by default:, (*23)

- dist
- _dist
- _dist_
- foo_dist
- _dist.txt
- foo_dist.txt

- ignore
- _ignore
- _ignore_
- foo_ignore
- _ignore.txt
- foo_ignore.txt

You may specify custom rules to ignore certain files:, (*24)

php $loader->getEnvironmentDetector() ->addIgnoreRule('sample', ['dist', 'ignore', 'sample']);, (*25)

Extending

Config loader provides you multiple extension points to alter its behaviour and functionality., (*26)

Custom config drivers

Adding your own drivers is very easy. All you have to do is to implement the IConfigDriver interface and pass an instance of the driver to the config loader. You can have multiple active drivers at the same time., (*27)

class MyDriver implements IConfigDriver {}

$loader->addDriver(new MyDriver());

Custom environment detector

You can replace the default environment detector with your own. Just create a new detector which implements the IEnvironmentDetector interface and pass it to the config loader. Also take a look on how to create your own environments., (*28)

class MyDetector implements IEnvironmentDetector {}

$loader->setEnvironmentDetector(new MyDetector());

The Versions

06/11 2016

dev-master

9999999-dev

Simple configuration loader.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

06/11 2016
21/07 2016
26/05 2016

v1.18.0

1.18.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

25/05 2016

v1.17.0

1.17.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

25/05 2016

v1.16.0

1.16.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

20/04 2016

v1.15.0

1.15.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

20/04 2016

v1.14.0

1.14.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

19/04 2016

v1.13.0

1.13.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

07/04 2016

v1.12.0

1.12.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

01/04 2016

v1.11.0

1.11.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

22/03 2016

v1.10.0

1.10.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

21/03 2016

v1.9.1

1.9.1.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

21/03 2016

v1.9.0

1.9.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

15/03 2016

v1.8.0

1.8.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

03/03 2016

v1.7.0

1.7.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

09/02 2016

v1.6.0

1.6.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

25/01 2016

v1.5.0

1.5.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^3.0
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

19/01 2016

v1.4.0

1.4.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

19/01 2016

v1.3.1

1.3.1.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

19/01 2016

v1.3.0

1.3.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

15/01 2016

v1.2.2

1.2.2.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

13/01 2016

v1.2.1

1.2.1.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

11/01 2016

v1.2.0

1.2.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0
  • weew/php-helpers-array ^1.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

14/12 2015

v1.1.1

1.1.1.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0.0
  • weew/php-helpers-array ^1.0.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

18/11 2015

v1.1.0

1.1.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0.0
  • weew/php-helpers-array ^1.0.0
  • symfony/yaml ^2.7
  • weew/php-contracts ^1.1

 

The Development Requires

by Maxim Kott

16/11 2015

v1.0.0

1.0.0.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file ^1.0.0
  • weew/php-helpers-array ^1.0.0
  • weew/php-foundation ^1.0.0
  • symfony/yaml ^2.7

 

The Development Requires

by Maxim Kott

28/09 2015

v0.0.9

0.0.9.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*
  • symfony/yaml ^2.7

 

The Development Requires

by Maxim Kott

28/09 2015

v0.0.8

0.0.8.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*
  • symfony/yaml ^2.7

 

The Development Requires

by Maxim Kott

25/09 2015

v0.0.7

0.0.7.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*
  • symfony/yaml ^2.7

 

The Development Requires

by Maxim Kott

25/09 2015

v0.0.6

0.0.6.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*
  • symfony/yaml ^2.7

 

The Development Requires

by Maxim Kott

25/09 2015

v0.0.5

0.0.5.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*

 

The Development Requires

by Maxim Kott

25/09 2015

v0.0.4

0.0.4.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*

 

The Development Requires

by Maxim Kott

25/09 2015

v0.0.3

0.0.3.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*

 

The Development Requires

by Maxim Kott

25/09 2015

v0.0.2

0.0.2.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*

 

The Development Requires

by Maxim Kott

22/09 2015

v0.0.1

0.0.1.0

Very simple configuration loader.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-file 0.*
  • weew/php-helpers-array 0.*
  • weew/php-foundation 0.*

 

The Development Requires

by Maxim Kott