2017 © Pedro Peláez
 

library phossa-config

Configuration management libraray for PHP

image

phossa/phossa-config

Configuration management libraray for PHP

  • Sunday, June 12, 2016
  • by phossa
  • Repository
  • 1 Watchers
  • 1 Stars
  • 85 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

phossa-config [ABANDONED]

Build Status Latest Stable Version License, (*1)

See new lib at phoole/config, (*2)

Introduction

phossa-config is a configuration management library for PHP. The design idea is inspired by another github project mrjgreen/config but with lots of cool features., (*3)

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

Features

  • One central place for all config files, (*5)

    config/
    |
    |____ production/
    |        |
    |        |____ host1/
    |        |       |___ redis.php
    |        |       |___ db.php
    |        |
    |        |____ db.php
    |
    |____ system.php
    |____ db.php
    |____ redis.php
    
  • Use an environment value, e.g. production or production/host1 for switching between development, staging or production., (*6)

  • Support .php, .json, .ini and .xml type of configuration files., (*7)

  • Reference is possible, such as ${system.tmpdir} in configuration file and environment file., (*8)

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

  • Able to load all configuration files in one shot with $config->get(null), (*10)

  • Configuration cache., (*11)

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

    // returns an array
    $db_config = $config->get('db');
    
    // returns a string
    $db_host = $config->get('db.auth.host');
    
  • Both flat notation and array notation fully supported and co-exist at the same time., (*13)

    // db config file
    return [
      // array notation
      'auth' => [
          'host' => 'localhost',
          'port' => 3306
      ],
    
      // flat notation
      'auth.user' => 'dbuser'
    ];
    
  • Un*x shell style environment file '.env' is supported with dereferencing feature and magic environment values like ${__DIR__} and ${__FILE__}, (*14)

Installation

Install via the composer utility., (*15)

composer require "phossa/phossa-config=1.*"

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

{
    "require": {
      "phossa/phossa-config": "1.*"
    }
}

Usage

  • Running Environment, (*17)

    Usually running environment is different for different servers. A good practice is setting environment in a .env file in the installation root and put all configuration files in the config/ directory., (*18)

    Sample .env file,, (*19)

    # debugging true|false, change to 'false' ON production server
    APP_DEBUG=true
    
    # App environment, change to 'prod' ON production server
    APP_ENV=dev
    
    # app root directory, default to current dir
    APP_ROOT=${__DIR__}
    
    # central configuration directory
    CONFIG_DIR=${APP_ROOT}/config
    

    In the bootstrap.php file,, (*20)

    // load environment
    (new Phossa\Config\Env\Environment())->load(__DIR__.'/.env');
    
    // create config object
    $config = new Phossa\Config\Config(
      getenv('CONFIG_DIR'), // loaded from .env file
      getenv('APP_ENV')     // loaded from .env file
    );
    
    // load all configs in one shot
    $conf_data = $config->get(null);
    
  • Grouping, (*21)

    Configurations are grouped into groups, namely files. For example, the system.php holds all system.* configurations, (*22)

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

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

    $dir = $config->get('system.tmpdir');
    

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

  • Caching, (*25)

    A cache pool can be passed to the config constructor to have it read all configs from the cache or save all configs to cache., (*26)

    // create config object
    $config = new Phossa\Config\Config(
      dirname(__DIR__) . '/config',     // the config dir
      'staging/server2',                // config env
      'php',                            // file type
      new Phossa\Config\Cache\Cache(__DIR__ . '/cache') // cache location
    );
    
    // if cache exists, this will read all configs from the cache
    $conf_data = $config->get(null);
    
    // ...
    
    // write to cache
    $config->save();
    
    • Pros of using caching, (*27)

    • Speed up. Read from one file instead of lots of configuration files., (*28)

    • References like ${system.tmpdir} are done already., (*29)

    • Cons of using caching, (*30)

    • Config data might be stale. need to using $config->save() to overwrite or $cache->clear() to clear the cache., (*31)

    • Need write permission to a cache directory., (*32)

    • Might expose your configuration if you are not careful with cache data., (*33)

  • Reference, (*34)

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

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

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

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

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

    You may reset the reference start and ending chars,, (*38)

    // now reference is something like '%system.tmpdir%'
    $config = (new Config())->setReferencePattern('%', '%');
    

    Or even disable the reference feature,, (*39)

    // now reference is not recognised
    $config = (new Config())->setReferencePattern('', '');
    
  • Overwriting, (*40)

    If the environment is set to production/host1, the precedence order is,, (*41)

    • config/production/host1/db.php over, (*42)

    • config/production/db.php over, (*43)

    • config/config/db.php, (*44)

  • Config API, (*45)

    • get($key, $default = null)

    $key is the a flat notation like db.auth.host or NULL to get all of the configurations. $default is used if no configs found., (*46)

    Return value might be a string or array base on the $key., (*47)

    • set($key, $value)

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

    $value can be a string or array., (*49)

    • has($key)

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

    • save()

    Save current full configurations into a cache., (*51)

Changes

  • 1.0.6 added setReferencePattern(), hasReference() and deReference()

Dependencies

  • PHP >= 5.4.0, (*52)

  • phossa/phossa-shared ~1.0.10, (*53)

License

MIT License, (*54)

The Versions

12/06 2016

dev-master

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

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

10/06 2016

1.0.7

1.0.7.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

08/06 2016

1.0.6

1.0.6.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

08/06 2016

1.0.5

1.0.5.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

07/06 2016

1.0.4

1.0.4.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

07/06 2016

1.0.3

1.0.3.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

25/04 2016

1.0.2

1.0.2.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

25/04 2016

1.0.1

1.0.1.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa

25/04 2016

1.0.0

1.0.0.0 https://github.com/phossa/phossa-config

Configuration management libraray for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

configuration config configure phossa