2017 © Pedro Peláez
 

library configuration

Configuration component

image

puzzle/configuration

Configuration component

  • Tuesday, February 6, 2018
  • by nlenardou
  • Repository
  • 1 Watchers
  • 3 Stars
  • 26,131 Installations
  • PHP
  • 10 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 15 Versions
  • 7 % Grown

The README.md

Puzzle-configuration PHP >= 5.6

PHP 7.x users please use puzzle/configuration 4.x PHP 5.6 users please use puzzle/configuration 3.x, (*1)

Hide configuration implementation behind common interface., (*2)

Some advantages : * Application does not depend upon configuration implementation details * Application does not have to manage filesystem issues (for filesystem based implementations) * Application can be easily tested, even for configuration edge cases (missing or wrong configuration values) * Define configuration as a service in your dependency injection container, (*3)

QA

SensioLabsInsight, (*4)

Service Result
CI CI
Scrutinizer Scrutinizer Code Quality
Code coverage codecov
Packagist Latest Stable Version Total Downloads

Installation

Use composer :, (*5)

{
    "require": {
            "puzzle/configuration" : "~4.0"
    }
}

Documentation

Configuration as a service

<?php

class Example
{
    public function __construct(Puzzle\Configuration $config)
    {
        $threshold = $config->read('app/detection/threshold');
    }
}

The way the configuration value is read depends on the chosen implementation., (*6)

Up to now, 2 implementations are provided : * Memory (for unit testing purpose) * Yaml (based on Symfony/Yaml)., (*7)

For YAML one, 'app/detection/threshold' means detection[thresold] in app.yml file. When you instanciate YamlConfiguration object, you need to provide where yaml files can be found :, (*8)

<?php

$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$config = new Puzzle\Configuration\Yaml($fileSystem);

$example = new Example($config);

# app.yml 

detection:
  threshold: 3

Unit testing

<?php

$config = new Puzzle\Configuration\Memory(array(
    'app/detection/threshold' => 2
);

$example = new ExampleTest($config);

Default values

<?php

$configuration->read('a/b/c', 'default value if a/b/c does not exist');

But if a/b/c is required :, (*9)

<?php

// will throw an exception if a/b/c does not exist
$configuration->readRequired('a/b/c');

Fallback strategy

<?php

// Since 1.5.0
// returns value associated to first existing key
// will throw an exception if none exist
$configuration->readFirstExisting('a/b/c', 'd/e/f', 'x/y/z');

Override configuration

If you need some configuration to (partially or not) override another one :, (*10)

<?php

// Since 1.6.0
$defaultFileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$defaultConfig = new Puzzle\Configuration\Yaml($defaultFileSystem);

$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/another/config/files')
);
$localConfig = new Puzzle\Configuration\Yaml($fileSystem);

$config = new Puzzle\Configuration\Stacked();
$config->overrideBy($defaultConfig)
       ->overrideBy($localConfig);

// values will be read in localConfig first. They will be read in default config only if they don't exist in local one.

Another example :, (*11)

<?php

$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$defaultConfig = new Puzzle\Configuration\Yaml($fileSystem);

$overrideConfig = new Puzzle\Configuration\Memory(array(
    'app/detection/threshold' => 2
);

$config = new Puzzle\Configuration\Stacked();
$config->overrideBy($defaultConfig)
       ->overrideBy($overrideConfig);

You can add as many as configuration instances you want in the stack. The last inserted is the most prioritary., (*12)

If you want to add the least prioritary, use the addBase() method :, (*13)

<?php

// Since 2.0.0
$config = new Puzzle\Configuration\Stacked();
$config->overrideBy($overrideConfig)
       ->addBase($defaultConfig);

Prefixed configuration

You can use automatic prefix decorator PrefixedConfiguration. It can be useful for "namespace like" configurations such as loggers or multiple databases ones., (*14)

# logger.yml 

app:
  filename: app.log
  verbosity: INFO
users:
  filename: users.log
  verbosity: WARNING
<?php

// Since 1.7.0
$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$config = new Puzzle\Configuration\Yaml($fileSystem);
$config = new Puzzle\PrefixedConfiguration($config, "logger/$loggerName");

$filename = $config->readRequired('filename');
$verbosity = $config->readRequired('verbosity');

Changelog

4.x --> 5.x, (*15)

  • Drop php 7 support. Minimal version is 8.0

3.x --> 4.x, (*16)

  • Drop php 5.6 & 7.0 support. Minimal version is 7.1.0, (*17)

    2.x --> 3.x, (*18)

  • Drop php 5.5 support. Minimal version is 5.6.0, (*19)

1.x -> 2.x, (*20)

  • Drop php 5.4 support. Minimal version is 5.5.0

The Versions

06/02 2018

dev-master

9999999-dev

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

06/02 2018

3.1.0

3.1.0.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

10/03 2017

3.0.0

3.0.0.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

19/02 2017

2.0.1

2.0.1.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

19/02 2017

2.0.0

2.0.0.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

05/09 2016

1.7.2

1.7.2.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

13/01 2015

1.7.1

1.7.1.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

13/01 2015

1.7.0

1.7.0.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

26/11 2014

1.6.0

1.6.0.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

06/10 2014

1.5.0

1.5.0.0

Configuration component

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration

06/02 2014

1.4

1.4.0.0

Configuration component

  Sources   Download

The Requires

 

The Development Requires

configuration

16/12 2013

v1.3

1.3.0.0

Configuration component

  Sources   Download

The Requires

 

The Development Requires

configuration

15/12 2013

v1.2

1.2.0.0

Configuration component

  Sources   Download

The Requires

 

The Development Requires

configuration

18/11 2013

v1.1

1.1.0.0

Configuration component

  Sources   Download

The Requires

 

configuration

18/11 2013

v1.0

1.0.0.0

Configuration component

  Sources   Download

The Requires

 

configuration