承接 werx/config 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

werx/config

Composer 安装命令:

composer require werx/config

包简介

Use environment-specific configuration files in your app.

README 文档

README

Build Status Total Downloads Latest Stable Version

Use environment-specific configuration files in your app.

Features:

  • Multiple Environment Support (example: local/test/prod)
  • Extensible Configuration Providers
    • Includes an ArrayProvider and JsonProvider out of the box
    • Create your own providers by implementing \werx\config\Providers\ProviderInterface
  • Load multiple config files with a single call

Basic Usage

# Get an instance of the ArrayProvider
$provider = new \werx\Config\Providers\ArrayProvider('/path/to/config/directory');

# Create a Config\Container Instance from this provider.
$config = new \werx\Config\Container($provider);

# Load config/config.php
$config->load('config');

# Get an item.
$item = $config->get('foo');

Recommended Directory Structure

For the default config providers, you'll need to create a directory structure somewhere in your project to hold your configuration files.

Recommended structure:

config/
    config.php
    another_config.php
    /local/
        config.php
    /test/
        config.php
    /prod/
        config.php

Default configs go in the root config directory. There must also sub-directories for each environment (local/test/prod) if you want to override the default setting when running in different environments.

Using ArrayProvider

Create a .php file in your config directory that returns an array of config values.

<?php
return [
	'foo' => 'Foo',
	'bar' => 'Bar'
];

Get an instance of the ArrayProvider class, passing the path to your config directory to the constructor.

$provider = new \werx\Config\Provider\ArrayProvider('/path/to/config/directory');

Using JsonProvider

Create a .json file in your config directory that returns an array of config values.

{
    "foo" : "Foo",
    "bar" : "Bar"
}

Get an instance of the JsonProvider class, passing the path to your config directory to the constructor.

$provider = new \werx\Config\Provider\JsonProvider('/path/to/config/directory');

Loading A Configuration Group

In this example, you would be loading the array from config.php in your config directory.

$config = new \werx\Config\Container($provider);
$config->load('config');

Get A Configuration Value

$item = $config->get('foo');

print $item;
// Foo

Get A Default Value

If a configuration item doesn't exist, $config->get() will return null. You can override the default return value by passing the new default as the 2nd parameter.

$item = $config->get('doesnotexist', false);
var_dump($item);
// false;

Loading Environment-Specific Configuration Group

In this example, you would be loading the array from config.php and test/config.php. Keys from test/config.php will merge with the keys from config.php.

$config->setEnvironment('test');
$config->load('config');

Note: The merge strategy is similar to that of PHP's native array_merge strategy in that keys in the environment-specific config with the same name as those in the default config replace the ones in the default. This merge is recursive, so nested keys follow the same logic as well.

Loading Multiple Configuration Groups

If you have more than one configuration group to load, you can call load() multiple times, or you can pass an array of config groups.

$config->load(['config', 'another_config']);

Avoiding Collisions On Config Property Names

By default, if a configuration property name is found in multiple config files, the config value will be replaced each time that property name is found in a config file. If you prefer, you can tell the loader to index the config container with the name of the config group to prevent name collisions. This is accomplished by passing true as the second parameter to load().

$config->load(['config', 'email'], true);

Then to retrieve your indexed property name, call the "magic" method named the same as the config file you loaded.

// Get the value for the 'host' property from the 'email' configuration group.
$item = $config->email('host', 'smtp.mailgun.org');

As with the get() method, the seond parameter above is the default value if the config item doesn't exist.

Or you can return all of the items in the 'email' config group as an array by not passing any parameters.

$items = $config->email();

Installation

This package is installable and autoloadable via Composer as werx/config. If you aren't familiar with the Composer Dependency Manager for PHP, you should read this first.

$ composer require werx/config --prefer-dist

Contributing

Unit Testing

$ vendor/bin/phpunit

Coding Standards

This library uses PHP_CodeSniffer to ensure coding standards are followed.

I have adopted the PHP FIG PSR-2 Coding Standard EXCEPT for the tabs vs spaces for indentation rule. PSR-2 says 4 spaces. I use tabs. No discussion.

To support indenting with tabs, I've defined a custom PSR-2 ruleset that extends the standard PSR-2 ruleset used by PHP_CodeSniffer. You can find this ruleset in the root of this project at PSR2Tabs.xml

Executing the codesniffer command from the root of this project to run the sniffer using these custom rules.

$ ./codesniffer

werx/config 适用场景与选型建议

werx/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.14k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-11