2017 © Pedro Peláez
 

library configs

Configuration loader class for loading different types of files into array data

image

g4mr/configs

Configuration loader class for loading different types of files into array data

  • Friday, December 18, 2015
  • by G4MR
  • Repository
  • 1 Watchers
  • 2 Stars
  • 30 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

G4MR\Configs

This is a simple library which allows you to load config files as array data and accessing the data using dot-notation. I made this library to simplify the process of loading YAML config files with the ease of implementing your own config loader., (*1)

Composer

Install via composer using composer require g4mr/configs, (*2)

Example 1

    <?php
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //loads ./config/database.yml as an array block
    $db_config = $config->get('database', false);
    if($db_config !== false) {
        echo $db_config['dbname'];
    }

Example 2 - using the item object

    <?php
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //example using stash (http://www.stashphp.com) caching
    $pool = new Stash\Pool();

    $db_stash   = $pool->getItem('db/config');
    $db_config  = $db_stash->get();

    if($db_stash->isMiss()) {
        $db_config = $config->getItem('database');
        $db_stash->set($db_config, 60 * 5); //cache data for 5 minutes
    }

    $dbname = $db_config->get('dbname', null);
    $dbuser = $db_config->get('username', null);
    $dbpass = $db_config->get('password', null);
    $dbhost = $db_config->get('host', 'localhost');

Example 3 - Updating an item array

    <?php
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //loads ./config/database.yml as an item object
    $db_config = $config->getItem('database');
    $db_config->set('db.user', 'root');
    $db_config->set('db.pass', 'root');
    $db_config->set('db.host', 'localhost');
    $db_config->set('db.name', 'mydatabase');

    $dbhost = $db_config->get('db.user'); // 'root'

You can also do something like:, (*3)

    <?php
    use G4MR\Configs\Config;
    use G4MR\Configs\Loaders\YamlLoader;

    $config = new Config(new YamlLoader(__DIR__ . '/config'));

    //loads ./config/connection.yml as an item object
    $conn = $config->getItem('connection');
    $conn->db = [
        'host' => 'localhost',
        'user' => 'root',
        'pass' => 'root',
        'name' => 'dbname'
    ];

    print_r($conn->get('db'));
    //echo $conn->get('db.user');

    //or

    print_r($conn->db);

Custom Loader

Check the tests folder for examples on how to implement your own config loader., (*4)

The Versions

18/12 2015

dev-master

9999999-dev

Configuration loader class for loading different types of files into array data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Lamonte Harris

config config loader g4mr

15/12 2015

1.1.0

1.1.0.0

Configuration loader class for loading different types of files into array data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Lamonte Harris

config config loader g4mr

25/11 2015

1.0.1

1.0.1.0

Configuration loader class for loading different types of files into array data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Lamonte Harris

config config loader g4mr

18/10 2015

1.0.0

1.0.0.0

Configuration loader class for loading different types of files into array data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Lamonte Harris

config config loader g4mr