raphhh/puppy-config 问题修复 & 功能扩展

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

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

raphhh/puppy-config

Composer 安装命令:

composer require raphhh/puppy-config

包简介

Config manager

关键字:

README 文档

README

Latest Stable Version Build Status Scrutinizer Quality Score Code Coverage Dependency Status Total Downloads Reference Status License

Puppy Config is a config manager that loads your config from files according to your env.

Config basic logic:

  • env config management (dev, test, prod, ...)
  • multi file formats support (php, ini, json, yaml)
  • dynamic config values
  • visibility restriction

Installation

Run composer:

$ composer require raphhh/puppy-config

Read your config

Add the main config file /config/global.php:

// /config/global.php
return [
    'key' => 'value',
];

Launch the Config class:

use Puppy\Config\Config;

$config = new Config();
$config['key']; //'value'

Dynamic values

You can retrieve dynamically a previous defined value with it key.

// /config/global.php
return [
    'key1' => 'value1',
    'key2' => '%key1%_b',
];
use Puppy\Config\Config;

$config = new Config();
$config['key2']; //'value1_b'

Set new values

You can set new values on the fly, which will be available in the Config object during all the script (and not saved into a file config).

use Puppy\Config\Config;

$config = new Config();
$config['new_key'] = 'new_value';

You can also use the dynamic mapping.

$config['new_key2'] = '%new_key%';
$config['new_key2']; //'new_value'

Visibility restriction

You can determine some namespaces in your config, and restrict the visibility of each namespace.

For example, imagine you set this config, with two namespaces: 'a' and 'b'.

//namespace a
$globalConfig['a.a'] = 'a.a';
$globalConfig['a.b'] = 'a.b';

//namespace b
$globalConfig['b.a'] = 'b.a';
$globalConfig['b.b'] = 'b.b';

With the global scope config you can access to all the values.

$globalConfig['a.a']; //'a.a'
$globalConfig['b.a']; //'b.a'

Now, restrict the config to the namespace 'a':

$restrictedConfig = $globalConfig->restrict('a');

With this restricted scope config, you can only have access to the namespace 'a' values:

$restrictedConfig['a']; //'a.a'
$restrictedConfig['b']; //'a.b'

The original keys are not visible anymore:

isset($restrictedConfig['a.a']); //false
isset($restrictedConfig['b.a']); //false

But, the global scope config is still bound to your restricted scope. So, if you modify one, you modify the other.

$restrictedConfig['a'] = 'new value';
$globalConfig['a.a']; //'new value'

Files format

By default, Config load 'php' files. This kind of file must return a PHP array.

// /config/global.php
return [
    'key' => 'value',
];

But you can specify or add specific file formats.

new Config('', null, new YmlFileReader()); //will load config/global.yml

Available formats:

  • php (default)
  • ini
  • json
  • yaml

Multi environment

Your config is the merge of the values coming from three kind of files:

type file name loading overloading usage
global config global.php always loaded prod & test
env config e.i. dev.php according to the env override global config dev
local config local.php specific for each machine override global and env config prod & test - dev

When is loaded the global config?

In all the cases, Config will load the file 'global.php'. (You can easily change this default file.)

By default, this is your prod config.

When is loaded the env config?

If you specify an env in the constructor, it will load also the associated file.

For example:

new Config('dev'); //will load dev.php (in addition to global.php)

The env config will override the global config. So, use it for your dev env, which will override your prod params.

How env can change dynamically?

Set an environment variable in your server virtual host configuration, and retrieve it with the php getenv() method.

In your httpd.conf or a .htaccess file of your dev Apache server, put:

SetEnv APP_ENV "dev"

In your PHP file, retrieve the env:

new Config(getenv('APP_ENV')); //will load dev.php only in your dev server

What is the local config?

The config will load also a local config, if the file config/local.php exists.

This config will override the global and the env configs. Be careful: this file must be NOT versioned.

So, it is an individual config, where your can put temporary or specific config. Your can also put config you do not want to version, like the passwords.

raphhh/puppy-config 适用场景与选型建议

raphhh/puppy-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 807 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「config」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 raphhh/puppy-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 raphhh/puppy-config 我们能提供哪些服务?
定制开发 / 二次开发

基于 raphhh/puppy-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-23