g4mr/configs 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

g4mr/configs

最新稳定版本:1.1.0

Composer 安装命令:

composer require g4mr/configs

包简介

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

README 文档

README

#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.

Composer

Install via composer using composer require g4mr/configs

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:

    <?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.

统计信息

  • 总下载量: 41
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-17