1 Star 2 Fork 0

K. / Agi Config Storage

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

#Agi Config Storage

该类库的设计目的主要为了解决不同底层环境和框架环境的项目,可以共享配置数据和信息。

一个项目中会有各种的配置数据,这些配置的数据,一般会呈现 2 层的 Map 的结构,举个例子:

$config = array(
    // 数据库配置
    'db' => array(
        'default' => array(
            'host' => '127.0.0.1',
            // .. other settings
        ),
        'db2' => array(
            'host' => 'any-host',
            // .. other settings
        )
    ),
    // 发送邮件的服务器配置
    'mail' => array(
        'default' => array(
            'host' => 'smtp.gmail.com',
            'port' => 465,
            'username' => 'username',
            'password' => 'password',
        )
    ),
    // memcached配置
    'memcached' => array(
        'default' => array(
            'hosts' => array( '127.0.0.1:11211', '127.0.0.1:11212' ),
        )
    ),
);

这种配置方式,有几个问题:

  1. 基于 2 层或以上的 Map 结构,读取数据,需要做的判断、和操作都比较繁琐。
  2. 不同的用处的配置,有自己的配置规格,比如db和mail。
  3. 即使是相同用处的配置,不同的框架、应用程序,会有不同的命名,比如同样是 PDO_MYSQL 的 username 字段,一个项目可能会叫 username ,而另外一个项目可能会叫 user ,彼此之间的数据共享有很多问题。
  4. 保证配置的数据的稳定性和私密性,并且不允许被后续的写入和更新。

Agi Config Config 就是为了解决上述的几个问题。

  1. 配置的存储容器使用单层的 Map 。
  2. 配置数据和具体的应用程序、框架解耦(也是为何将这个类库作为一个独立的项目)。
  3. 配置在用时为只读。
  4. 添加配置时,无需使用实例的方法,只需添加数组格式的数据即可,配置数据到用时方转换为具体的配置数据实例(节省内存)。
  5. 添加配置时,并不补完该数据的规格,而是到转换为具体的配置数据实例时,方才补完配置的数据规格(到用时才补完)。

##实际使用例子


use Agi\Config\Db;
use Agi\Config\Mail;

// 数据库配置
// 添加一个名为 mysql 配置
Db::define('mysql', array(
    'adapter' => Db::MYSQL_PDO,
	'host' => 'any-host',
));

// 添加多个配置
Db::defineMulti(array(
    // 全局默认的配置
    array(
        'adapter' => Db::MYSQL_PDO,
        'host' => 'any-host',
    ),
    // 添加一个名为 pgsql 的数据库
    'pgsql' => array(
        'adapter' => Db::PGSQL_PDO,
        'host' => 'any-host',
    ),
));

// 邮箱配置
// 第一个参数 $name 为空,表示为全局默认配置
Mail::define(null, array(
    'host' => 'smtp.gmail.com',
    'smtp' => Mail::USE_SSL,
    'username' => 'any@gmail.com',
    'password' => 'any-password',
));

// 取出默认的配置 default
$db = Db::get();
$pgsql = Db::get('pgsql');

// 输出配置的信息
var_dump($db->host, $db->port, $db->name);
var_dump($pgsql->isPdo, $pgsql->isNative);
// 如此种种……
// 等等……
var_dump(Mail::get()->host);
var_dump(Db::get('mysql')->username);
The MIT License (MIT) Copyright (c) 2015 曾建凯 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

配置存储容器 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/janpoem/Agi-Config-Storage.git
git@gitee.com:janpoem/Agi-Config-Storage.git
janpoem
Agi-Config-Storage
Agi Config Storage
master

搜索帮助