2017 © Pedro PelĂĄez
 

library slimpower-config

Config is a file configuration loader that supports PHP, INI, XML, JSON, and YML files.

image

matiasnamendola/slimpower-config

Config is a file configuration loader that supports PHP, INI, XML, JSON, and YML files.

  • Thursday, June 15, 2017
  • by matiasamendola
  • Repository
  • 1 Watchers
  • 0 Stars
  • 26 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

SlimPower - Config

Latest version ![Total Downloads][ico-downloads], (*1)

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads composer.lock available, (*2)

Config is a file configuration loader that supports PHP, INI, XML, JSON, and YML files., (*3)

Installation

In terminal:, (*4)

composer require matiasnamendola/slimpower-config

Or you can add use this as your composer.json:, (*5)

{
    "require": {
        "matiasnamendola/slimpower-config": "dev-master"
    }
}

Requirements

Config requires PHP 5.3+, and suggests using the Symfony Yaml component., (*6)

Config is designed to be very simple and straightforward to use. All you can do with it is load, get, and set., (*7)

Loading files

The Config object can be created via the factory method load(), or by direct instantiation:, (*8)

<?php

use SlimPower\Config\Config;

// Load a single file
$conf = Config::load('config.json');
$conf = new Config('config.json');

// Load values from multiple files
$conf = new Config(array('config.json', 'config.xml'));

// Load all supported files in a directory
$conf = new Config(__DIR__ . '/config');

// Load values from optional files
$conf = new Config(array('config.dist.json', '?config.json'));

Files are parsed and loaded depending on the file extension. Note that when loading multiple files, entries with duplicate keys will take on the value from the last loaded file., (*9)

When loading a directory, the path is globed and files are loaded in by name alphabetically., (*10)

Getting values

Getting values can be done in three ways. One, by using the get() method:, (*11)

// Get value using key
$debug = $conf->get('debug');

// Get value using nested key
$secret = $conf->get('security.secret');

// Get a value with a fallback
$ttl = $conf->get('app.timeout', 3000);

The second method, is by using it like an array:, (*12)

// Get value using a simple key
$debug = $conf['debug'];

// Get value using a nested key
$secret = $conf['security.secret'];

// Get nested value like you would from a nested array
$secret = $conf['security']['secret'];

The third method, is by using the all() method:, (*13)

// Get all values
$data = $conf->all();

Setting values

Although Config supports setting values via set() or, via the array syntax, any changes made this way are NOT reflected back to the source files. By design, if you need to make changes to your configuration files, you have to do it manually., (*14)

$conf = Config::load('config.json');

// Sample value from our config file
assert($conf['secret'] == '123');

// Update config value to something else
$conf['secret'] = '456';

// Reload the file
$conf = Config::load('config.json');

// Same value as before
assert($conf['secret'] == '123');

// This will fail
assert($conf['secret'] == '456');

Using with default values

Sometimes in your own projects you may want to use Config for storing application settings, without needing file I/O. You can do this by extending the AbstractConfig class and populating the getDefaults() method:, (*15)

<?php

namespace ...;

use SlimPower\Config\AbstractConfig;

class MyConfig extends AbstractConfig
{
    protected function getDefaults()
    {
        return array(
            'host' => 'localhost',
            'port'    => 80,
            'servers' => array(
                'host1',
                'host2',
                'host3'
            ),
            'application' => array(
                'name'   => 'configuration',
                'secret' => 's3cr3t'
            )
        );
    }
}

Credits

License

The MIT License (MIT). Please see License File for more information., (*16)

The Versions

15/06 2017

dev-master

9999999-dev https://github.com/matiasnamendola/slimpower-config

Config is a file configuration loader that supports PHP, INI, XML, JSON, and YML files.

  Sources   Download

MIT

The Requires

 

php json xml ini config yaml yml

07/12 2016

v0.0.1-alpha

0.0.1.0-alpha https://github.com/matiasnamendola/slimpower-config

Config is a file configuration loader that supports PHP, INI, XML, JSON, and YML files.

  Sources   Download

MIT

The Requires

 

php json xml ini config yaml yml