2017 © Pedro Peláez
 

library mini-config

A mini config library

image

abreksa4/mini-config

A mini config library

  • Tuesday, April 12, 2016
  • by abreksa4
  • Repository
  • 0 Watchers
  • 0 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

mini-config

mini-config takes a list of files and directories, and becomes an ArrayAccess object with the parsed config data., (*1)

Currently supports JSON*, INI, PHP arrays, and XML** out of the box., (*2)

**JSON must be in the following format, though you can register a custom JSON parser to override this:*, (*3)

{
  "cat1": {
    "key1": "value1",
    "key2: "value2"
  },
  "cat2": {
    "key1": 0
  }
}

**XML must be in the following format, though you can register a custom XML parser to override this:, (*4)

<root>
    <key>value</key>
    <cat>
        <key>value</key>
    </cat>
</root>

Documentation is available at: http://abreksa4.github.io/mini-config-docs/, (*5)

mini-config merges the config data recursively. (Meaning that if two sources (files) share keys, the values will be merged as an array as well.), (*6)

Installation

Add this to your composer.json, (*7)

{
    "require": {
        "abreksa4/mini-config": "0.2"
    }
}

Usage

Create a new Config instance

Create a new Config instance, passing in the optional $options array, which currently supports the keys 'targets' which should contain an array of targets, and 'handlers' an array of handlers in the format [$extension => $handler]., (*8)

$config = new Config([
    'targets' => [
        'module/config.xml',
        'config',
        ]
    ],
    'handlers' => array(
        'yml' => function ($file) { return yaml_parse_file($file); }
    )
));

At this point the $config object is up and running, with the data from the files in config, and in module/config.xml. (Note, we'd need to have the YAML PHP extension installed to use yaml_parse.), (*9)

Add more targets

We can add more targets by calling addTarget. As you can see we can either add an array of targets or just one., (*10)

$config->addTarget('/anothermodule/config');
$config->addTarget(['config_ini', '../config/local']);

Custom handlers

You can register a custom handler for any file extension. For example:, (*11)

$config->registerHandler(['yml'], 
       function($file){
            return yaml_parse_file($file);
       }
);

Notice we can register an array of extensions to one handler, we can also specify a single extension as a string. Extensions are case-sensitive., (*12)

Merge array into config

$config->merge([
    'cat3'=> [
        'key1' => 'value1';
    ]
]);

Refreshing the config

Instead of re-scanning and importing all the data every time we add a target, we call the refresh() method to re-import the data:, (*13)

$config->refresh();

Data access

We can access the data by treating the $config object as an array, i.e., (*14)

$config['database']['password'];

The Versions

12/04 2016

dev-master

9999999-dev

A mini config library

  Sources   Download

GPL-2.0

by Andrew Breksa

12/11 2015

0.2

0.2.0.0

A mini config library

  Sources   Download

proprietary

by Andrew Breksa

11/11 2015

0.1.1

0.1.1.0

A mini config library

  Sources   Download

proprietary

by Andrew Breksa