2017 © Pedro Peláez
 

library config

A small, PHP, configuration library consisting mostly of functions.

image

microlib/config

A small, PHP, configuration library consisting mostly of functions.

  • Thursday, September 18, 2014
  • by jeremeamia
  • Repository
  • 2 Watchers
  • 14 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

MicroLib — Config

A small, PHP, configuration library consisting mostly of functions. Yep, functions, because working with configuration arrays should be simple and fast., (*1)

Features

  • Small library. No bloat. Easy-to-use functions.
  • Supports PHP array, JSON, INI, YAML, and XML config file formats.
  • Easy to apply defaults, enforce required settings, and filter out extraneous data.
  • Can specify a schema to recursively validate the data. Uses callables for validations and transformations.
  • Easily fetch values from nested config structure.

Usage

Validating Against a Schema

Let's say you have a config file that looks like this:, (*2)

{
    "first_name": "Jeremy ",
    "last_name": "Lindblom",
    "phone": {"number": "5559997777"},
    "gender": "whocares"
}

In your PHP script…, (*3)

Include the Config library., (*4)

require '/path/to/vendor/autoload.php';

use MicroLib\Config as cfg;

Load the config file., (*5)

$config = cfg\load('config.json');

Define a schema for the data. Using the schema function is optional but is useful for validating that the schema is defined correctly. (Note: The schema can be placed in a separate file and loaded with the load function like other config data.), (*6)

$schema = cfg\schema([
    'first_name' => [
        'required'  => true,
        'transform' => 'trim',
        'validate'  => 'ctype_alpha'
    ],
    'last_name' => [
        'required'  => true,
        'transform' => 'trim',
        'validate'  => 'ctype_alpha'
    ],
    'phone' => [
        'schema' => [
             'type' => [
                 'default'   => 'mobile',
                 'transform' => 'trim',
                 'validate'  => 'ctype_alpha',
             ],
             'number' => [
                 'required' => true,
                 'validate' => 'ctype_digit',
             ]
        ]
    ],
]);

Validate the configuration data with the schema. If no exception is thrown, then the data is valid., (*7)

$config = cfg\validate($config, $schema);

Fetch data from the config using get., (*8)

echo cfg\get($config, 'phone.number');
#> 5559997777

echo cfg\get($config, 'phone.type');
#> mobile

Simple Check for Required Keys

If you don't need the functionality of the schemas, there is still an easy way to enforce required settings and apply default values., (*9)

Let's say you have the following array of configuration data:, (*10)

$config = [
    'class'  => 'rogue',
    'race'   => 'half-elf',
    'level'  => 5,
    'weapon' => 'short sword',
];

If you aren't concerned about all of the items, you can use the keep function to create a new array of only the items you want., (*11)

$config = cfg\keep($config, ['class', 'race', 'level']);

Then, using the create function, you can check requirements and apply defaults., (*12)

$config = cfg\create(
    $config,
    ['class', 'race', 'level'],
    ['status' => 'normal']
);

print_r($config);
#> Array
#> (
#>     [class] => rogue
#>     [race] => half-elf
#>     [level] => 5
#>     [status] => normal
#> )

The Versions

18/09 2014

dev-master

9999999-dev

A small, PHP, configuration library consisting mostly of functions.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

micro function schema config validation configurations

18/09 2014

0.1.1

0.1.1.0

A small, PHP, configuration library consisting mostly of functions.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

micro function schema config validation configurations

02/09 2014

0.1.0

0.1.0.0

A small, PHP, configuration library consisting mostly of functions.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

micro function schema config validation configurations