kleinrich/overloadconf

本包最新版本(1.0)没有提供许可证信息。

基于上下文(环境、语言等)简化配置加载的库

1.0 2017-12-06 14:02 UTC

This package is auto-updated.

Last update: 2024-09-08 13:57:27 UTC


README

OverloadConf 是一个简化基于上下文配置文件加载的库。
原理是在同一个文件或多个文件中定义公共变量(或不需要)和上下文变量(或不需要)。
此库能够加载单个文件、目录中的所有文件或一组文件/目录。

安装

推荐通过 Composer 安装 OverloadConf。

  $ composer require kleinrich/overloadConf ~1.0

或者,可以从这个 Git 仓库克隆代码

  $ git clone https://bitbucket.org/Kleinrich/OverloadConf.git

配置文件

该库接受 php、json 和 yml 扩展名。

以下是一个数据库配置的示例。对于所有环境,除了模式、用户和密码根据环境而变化,主机都是本地机器。

Php (database.php)

<?php
use Kleinrich\OverloadConf\OverloadConf;

return array(
    OverloadConf::COMMON => array(
        'host' => 'http://127.0.0.1/',
    ),
    'dev' => array(
        'schema' => 'foo',
        'user' => 'foo',
        'password' => 'foo'
    ),
    'prod' => array(
        'schema' => 'bar',
        'user' => 'bar',
        'password' => 'bar',
    ),
);

Json (database.json)

{
  "common": {
    "host": "http://127.0.0.1/"
  },
  "dev": {
    "schema": "foo",
    "user": "foo",
    "password": "foo"
  },
  "prod": {
    "schema": "bar",
    "user": "bar",
    "password": "bar"
  }
}

Yaml (database.yml)

common:
    host: "http://127.0.0.1/"
dev:
    schema: "foo"
    user: "foo"
    password: "foo"
prod:
    schema: "bar"
    user: "bar"
    password: "bar"

基本用法

OverloadConf 作为服务运行,可能(应该)只实例化一次。例如,在你的 DI 容器中。

<?php
  // May be in your DIContainer
  $oConfigLoader = new \Kleinrich\OverloadConf\OverloadConf();
  $oConfigLoader->setParser(
    new \Kleinrich\OverloadConf\Parser()
  );

然后,当你需要获取配置时,必须调用 OverloadConf 服务的 get 函数

<?php
  /**
   * Let's try with the example seen above
   * NB: It may be interresting to create an extention of OverloadConf if your 
   * configuration files are all in the same folder and have the same extention. Then 
   * you will be able to call $oConfigLoader->get('database', $sEnv);
   */
  $sPath = '../config/database.yml';
  /**
   * I choosed the environment, but it could be anything else (lang, country, etc...)
   * It depends on your needs
   */
  $sEnv = 'dev';

  $aConfig = $oConfigLoader->get('./config/database.json', $sEnv);

  $sHost = $aConfig['database.host'];
  $sSchema = $aConfig['database.schema'];
  $sUser = $aConfig['database.user'];
  $sPassword = $aConfig['database.password '];

高级用法

OverloadConf 服务能够使用 glob 模式加载配置。

<?php
  /**
   * This example will load all JSON files in config folder that begins with database
   * ie : databasedev.json, dabaseprod.json, ...
   */
  $aConfig = $oConfigLoader->get('./config/database*.json', $sContext);

它还可以加载单个目录中的所有配置文件...

<?php
  /**
   * In this example, you imagine that database is a folder
   * ie :
   *      -config
   *          -database
   *              -dev.json
   *              -prod.json
   */
  $aConfig = $oConfigLoader->get('./config/database/', $sContext);

...或者递归地。

<?php
  /**
   * In this example, you imagine that database is a folder
   * ie :
   *      -config
   *          -dev
   *              -database.json
   *          -prod
   *              -database.json
   *          -database.json
   */
  $aConfig = $oConfigLoader->get(
      './config',
      $sContext,
      OverloadConf::OPTION_RECURSIVE
  );