2017 © Pedro Peláez
 

library config

A simple, easy to use, yet powerful configuration management library for PHP.

image

phossa2/config

A simple, easy to use, yet powerful configuration management library for PHP.

  • Tuesday, October 4, 2016
  • by phossa2
  • Repository
  • 1 Watchers
  • 2 Stars
  • 202 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 1 % Grown

The README.md

phossa2/config [ABANDONED]

PLEASE USE phoole/config library instead, (*1)

Build Status Code Quality Code Climate PHP 7 ready HHVM Latest Stable Version License, (*2)

phossa2/config is a simple, easy to use, yet powerful configuration management library for PHP. The design idea was inspired by another github project mrjgreen/config but some cool features added., (*3)

It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with PSR-1, PSR-2, PSR-4., (*4)

Installation

Install via the composer utility., (*5)

composer require "phossa2/config=2.*"

or add the following lines to your composer.json, (*6)

{
    "require": {
       "phossa2/config": "2.*"
    }
}

Features

  • Simple interface, get($id, $default = null) and has($id)., (*7)

  • One central place for all config files for ease of management., (*8)

    config/
    |
    |___ production/
    |       |
    |       |___ host1/
    |       |      |___ db.php
    |       |      |___ redis.php
    |       |
    |       |___ db.php
    |
    |___ dev/
    |     |
    |     |___ redis.php
    |     |___ db.php
    |
    |___ db.php
    |___ redis.php
    |___ system.php
    
  • Use an environment value, such as production or production/host1 for switching between different configurations., (*9)

  • Use of references in configuration value is fully supported, such as ${system.tmpdir}., (*10)

  • On demand configuration loading (lazy loading)., (*11)

  • Hierachy configuration structure with dot notation like db.auth.host., (*12)

  • Array access for ease of use. e.g. $config['db.user'] = 'www';., (*13)

  • Reference lookup delegation and config chaining., (*14)

  • Support .php, .json, .ini, .xml and .serialized type of config files., (*15)

Usage

  • Use environment value, (*16)

    Usually application running environment is different on different servers. A good practice is setting environment in a .env file somewhere on the host, and put all configuration files in one central config/ directory., (*17)

    A sample .env file,, (*18)

    # installation base
    BASE_DIR=/www
    
    # app directory
    APP_DIR=${BASE_DIR}/app
    
    # config directory
    CONFIG_DIR=${APP_DIR}/config
    
    # app env for current host
    APP_ENV=production/host1
    

    In a sample bootstrap.php file,, (*19)

    use Phossa2\Config\Config;
    use Phossa2\Env\Environment;
    use Phossa2\Config\Loader\ConfigFileLoader;
    
    // load environment from '.env' file
    (new Environment())->load(__DIR__ . '/.env');
    
    // create config instance with the config file loader
    $config = new Config(
      new ConfigFileLoader(
          getenv('CONFIG_DIR'),
          getenv('APP_ENV')
      )
    );
    
    // object access of $config
    $db_config = $config->get('db');
    
    // array access of $config
    $config['db.user'] = 'www';
    
  • Central config directory and configuration grouping, (*20)

    • Configuration grouping

    Configurations are gathered into one directory and are grouped into files and subdirectories for ease of management., (*21)

    For example, the config/system.php holds system.* configurations, (*22)

    // system.php
    return [
        'tmpdir' => '/usr/local/tmp',
        // ...
    ];
    

    Later, system related configs can be retrieved as, (*23)

    // object acess of config
    $dir = $config->get('system.tmpdir');
    
    // array access of $config
    $dir = $config['system.tmpdir'];
    

    Or being used in other configs as references., (*24)

    • Configuration files loading order

    If the environment is set to production/host1, the config files loading order are,, (*25)

    1. config/config/*.php, (*26)

    2. config/production/*.php, (*27)

    3. config/production/host1/*.php, (*28)

    Configuration values are overwritten and replaced those from later loaded files., (*29)

  • Use of references, (*30)

    References make your configuration easy to manage., (*31)

    For example, in the system.php, (*32)

    // group: system
    return [
      'tmpdir' => '/var/local/tmp',
      ...
    ];
    

    In your cache.php file,, (*33)

    // group: cache
    return [
      // a local filesystem cache driver
      'local' => [
          'driver' => 'filesystem',
          'params' => [
              'root_dir'   => '${system.tmpdir}/cache', // use reference here
              'hash_level' => 2
          ]
      ],
      ...
    ];
    

    You may reset the reference start and ending matching pattern as follows,, (*34)

    // now reference is something like '%{system.tmpdir}%'
    $config->setReferencePattern('%{', '}%');
    
  • ArrayAccess and DOT notation, (*35)

    Config class implements ArrayAccess interface. So config values can be accessed just like an array., (*36)

    // test
    if (!isset($config['db.auth.user'])) {
      // set
      $config['db.auth.user'] = 'www';
    }
    

    Hierachy configuration structure with dot notation like db.auth.host., (*37)

    // returns the db config array
    $db_config = $config->get('db');
    
    // returns a string
    $db_host = $config->get('db.auth.host');
    

    Both flat notation and array notation are supported and can co-exist at the same time., (*38)

    // db config file
    return [
      // array notation
      'auth' => [
          'host' => 'localhost',
          'port' => 3306
      ],
    
      // flat notation
      'auth.user' => 'dbuser'
    ];
    
  • Reference lookup delegation and config chaining, (*39)

    • Config delegation

    Reference lookup delegation is similar to the delegation idea of Interop Container Delegate Lookup, (*40)

    • Calls to the get() method should only return an entry if the entry is part of the config registry. If the entry is not part of the registry, a NULL will be returned as described in ConfigInterface., (*41)

    • Calls to the has() method should only return true if the entry is part of the config registry. If the entry is not part of the registry, false should be returned., (*42)

    • If the fetched entry has dependencies (references), instead of performing the reference lookup in this config registry, the lookup is performed on the delegator., (*43)

    • Important By default, the lookup SHOULD be performed on the delegator only, not on the config registry itself., (*44)

      $config1 = new Config();
      $config2 = new Config();
      $delegator = new Delegator();
      
      // add some values
      $config1['db.user'] = '${system.user}';
      $config2['system.user'] = 'root';
      
      // reference unresolved in $config1
      var_dump($config1['db.user'] === '${system.user}'); // true
      
      // add both configs to the delegator
      $delegator->addConfig($config1);
      $delegator->addConfig($config2);
      
      // reference resolved thru the delegator
      var_dump($config1['db.user'] === 'root'); // true
      

    Delegator class implements the ConfigInterface and ArrayAccess interfaces, thus can be used just like a normal config., (*45)

    $dbUser = $delegator['db.user'];
    
    • Config chaining

    Config chaining can be achieved via multiple-level delegations. For example,, (*46)

    // configs
    $config1 = new Config();
    $config2 = new Config();
    
    // delegators
    $delegator1 = new Delegator();
    $delegator2 = new Delegator();
    
    // register $config1 with $delegator1
    $delegator1->addConfig($config1);
    
    // chaining
    $delegator2->addConfig($delegator1);
    $delegator2->addConfig($config2);
    
    // get from the chain
    $db = $delegator2->get('db');
    

APIs

  • ConfigInterface API, (*47)

    • get(string $id, $default = null): mixed

    $default is used if no config value is found., (*48)

    The return value might be a string, array or even object., (*49)

    • has(string $id): bool

    Test if $id exists or not. Returns a boolean value., (*50)

  • WritableInterface API, (*51)

    • set(string $id, mixed $value): bool

    Set the configuration manually in this session. The value will NOT be reflected in any config files unless you modify config file manually., (*52)

    $value may be a string, array or object., (*53)

    This feature can be disabled by, (*54)

    // disable writing to the $config
    $config->setWritable(false);
    
    • setWritable(bool $writable): bool

    Enable or disable the set() functionality. Returns true on success., (*55)

    • isWritable(): bool

    Test to see if current config writable or not., (*56)

  • ReferenceInterface API, (*57)

    • setReferencePattern(string $start, string $end): $this

    Reset the reference start chars and ending chars. The default are '${' and '}', (*58)

    • hasReference(string $string): bool

    Test to see if there are references in the $string, (*59)

    • deReference(string $string): mixed

    Dereference all the references in the $string. The result might be string, array or even object., (*60)

    • deReferenceArray(mixed &$data): $this

    Recursively dereference everything in the $data. $data might be string or array. Other data type will be ignored and untouched., (*61)

  • DelegatorInterface API, (*62)

    • addConfig(ConfigInterface $config): $this

    Added one Phossa2\Config\Interfaces\ConfigInterface instance to the delegator., (*63)

  • Others, (*64)

    • setErrorType(int $type): $this

    Set either Config::ERROR_IGNORE, Config::ERROR_WARNING or Config::ERROR_EXCEPTION for the config., (*65)

Change log

Please see CHANGELOG from more information., (*66)

Testing

$ composer test

Contributing

Please see CONTRIBUTE for more information., (*67)

Dependencies

  • PHP >= 5.4.0, (*68)

  • phossa2/shared >= 2.0.21, (*69)

License

MIT License, (*70)

The Versions

04/10 2016

dev-master

9999999-dev https://github.com/phossa2/config

A simple, easy to use, yet powerful configuration management library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa phossa2

18/07 2016

2.0.12

2.0.12.0 https://github.com/phossa2/config

A simple, easy to use, yet powerful configuration management library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

17/07 2016

2.0.11

2.0.11.0 https://github.com/phossa2/config

A simple, easy to use, yet powerful configuration management library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

17/07 2016

2.0.10

2.0.10.0 https://github.com/phossa2/config

A simple, easy to use, yet powerful configuration management library for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

16/07 2016

2.0.9

2.0.9.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

16/07 2016

2.0.8

2.0.8.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

15/07 2016

2.0.7

2.0.7.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

14/07 2016

2.0.6

2.0.6.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

12/07 2016

2.0.5

2.0.5.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

12/07 2016

2.0.4

2.0.4.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa2

10/07 2016

2.0.3

2.0.3.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

configuration config configure phossa phossa2

04/07 2016

2.0.2

2.0.2.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

configuration config configure phossa phossa2

28/06 2016

2.0.1

2.0.1.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

configuration config configure phossa phossa2

28/06 2016

2.0.0

2.0.0.0 https://github.com/phossa2/config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

configuration config configure phossa phossa2