v-dem/queasy-config
Composer 安装命令:
composer require v-dem/queasy-config
包简介
Configuration classes (currently supports PHP, INI, XML and JSON configs), part of QuEasy PHP Framework
README 文档
README
QuEasy PHP Framework - Configuration
Package v-dem/queasy-config
This package contains a set of the classes intended for reading configuration files. Formats currently supported are:
- PHP
- INI
- JSON
- XML
- CLI (command-line)
Features
- Easy to use - just like nested arrays or objects. Also it's possible to use
foreach()with config instances. - Support for default option values.
- Support for multi-file configurations. You can split your config into many files as you wish without changing program code.
- Options inheritance. If an option is missing at current config level, it will look for this option on upper levels.
- Unified config interface. You can switch between config formats without changing your code.
- Easy to extend with other config formats.
- Regular expressions support (it's possible to get config options by regular expression).
Requirements
- PHP version 5.3 or higher
Reference
See Wiki page.
Installation
> composer require v-dem/queasy-config:master-dev
Usage
Let's imagine we have the following config.php:
return [ 'connection' => [ 'driver' => 'mysql', 'host' => 'localhost', 'name' => 'test', 'user' => 'root', 'password' => 'secret' ] ];
Or config.ini:
[connection] driver = mysql host = localhost name = test user = root password = secret
Or config.json:
{
"connection": {
"driver": "mysql",
"host": "localhost",
"name": "test",
"user": "root",
"password": "secret"
}
}
Or config.xml:
<?xml version="1.0"> <config> <connection driver="mysql" host="localhost" name="test" user="root" password="secret" /> </config>
You can mix different config types, for example top-level config of PHP type can refer to config files of other types.
Creating config instance
Include Composer autoloader:
require_once('vendor/autoload.php');
Create config instance (config file type will be detected by file name extension):
$config = new queasy\config\Config('config.php'); // Can be also '.ini', '.json' or '.xml'
Accessing config instance
Now you can address config sections and options these ways:
$databaseName = $config->database->name;
Or:
$databaseName = $config['database']['name'];
It's possible to use a default value if an option is missing:
// If 'host' is missing in config, 'localhost' will be used by default $databaseHost = $config['database']->get('host', 'localhost');
A bit shorter way:
// If 'host' is missing in config, 'localhost' will be used by default $databaseHost = $config['database']('host', 'localhost');
It's also possible to point that an option is required, and to throw ConfigException if this option is missing:
// Throw ConfigException if 'name' is missing $databaseName = $config['database']->need('name');
How to check if a section or an option is present in config:
$hasDatabaseName = isset($config['database']); $hasDatabaseName = isset($config['database']['name']);
If you don't want to check each section for presence when accessing a very nested option, you can use this trick:
// $databaseName will contain 'default' if 'name' and/or 'database' options are missing $databaseName = $config->get('database', [])->get('name', 'default');
A bit shorter way:
// $databaseName will contain 'default' if 'name' and/or 'database' options are missing $databaseName = $config('database', [])('name', 'default');
Multi-file configs
config.php:
return [ 'connection' => [ 'driver' => 'mysql', 'host' => 'localhost', 'name' => 'test', 'user' => 'root', 'password' => 'secret' ], 'queries' => new queasy\config\Config('queries.php') // Can be config of another type (INI, JSON etc) ];
queries.php:
return [ 'selectActiveUsers' => 'SELECT * FROM `users` WHERE `is_active` = 1' ];
Accessing:
$config = new queasy\config\Config('config.php'); $query = $config['queries']['selectActiveUsers'];
Almost the same for other config formats:
config.ini:
[connection]
driver = mysql
host = localhost
name = test
user = root
password = secret
queries = "@queasy:new queasy\config\Config('queries.ini')"
There can be any PHP code after
@queasy:so it's possible to use PHP constants etc. Be careful,eval()function is used to execute this expression.
Different config formats can be mixed this way.
Merging configs
You can use Config's merge() method to merge two configs. For example, you can have a default configuration and allow users to add or override some options:
$defaultConfig = new queasy\config\Config('defaults.php'); $optionalConfig = new queasy\config\Config($arrayWithOptionsToAddOrOverride); $defaultConfig->merge($optionalConfig);
Using CLI config type
As an addition it's possible to use command-line arguments as config options source for CLI scripts (just use .cli extension, it will create appropriate loader):
$config = new queasy\config\Config('.cli');
Options should be passed this way (unfortunately only this is supported currently):
> php test.php option1=123 option2="some text"
I think it's useful to utilize merge() method there - default config file and optional arguments from command line.
Testing
Tests can be run with miminum PHP 7.2 version due to PHPUnit requirements. To run them use
> composer test
v-dem/queasy-config 适用场景与选型建议
v-dem/queasy-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 442 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「json」 「xml」 「configuration」 「ini」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 v-dem/queasy-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 v-dem/queasy-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 v-dem/queasy-config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Get wordpress nav menus and share the main site menus with the child sites.
统计信息
- 总下载量: 442
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 2
- 依赖项目数: 2
- 推荐数: 2
其他信息
- 授权协议: LGPL-3.0-only
- 更新时间: 2017-09-03