theiconic/config
Composer 安装命令:
composer require theiconic/config
包简介
configuration handler for file based configuration
README 文档
README
General purpose config manager for file-based configuration.
Setup
composer require theiconic/config
Concepts
Spaces
Configuration is grouped into configuration spaces. Spaces are managed by the configuration factory.
Multi-file & multi-section configuration
A configuration space can read configuration from several files. The configuration is merged and then flattened by section.
Placeholders
During parsing, pre-defined placeholders will be replaced with the configured replacement values.
Example
So the basic setup looks something like this (modified example based on alice):
// get the factory instance
$factory = Factory::getInstance();
// set the current environment
$factory->setEnvironment(APPLICATION_ENV);
// configure cache path
$factory->setCachePath(sprintf('%s/config', APPLICATION_TMP));
// instanciate and configure the application config space
$factory->getSpace('application')
->setPaths([
'/etc/iconic.ini',
$basePath . 'application.ini',
$basePath . 'application.local.ini',
])
->setSections([
'default',
'production',
])
->setPlaceholders([
'%APPLICATION_ENV%' => APPLICATION_ENV,
'%APPLICATION_ROOT%' => APPLICATION_ROOT,
'%APPLICATION_TMP%' => APPLICATION_TMP,
]);
Multi-section config format
Configuration files must contain configuration sections. This is the sections in .ini files and the first level array/object items in .php or .json config files.
No sections are pre-selected by default. You will need to explicitly state the sections in code, like so:
$factory->getSpace('myConfig')
->setSections(['main', 'development', 'testing']);
Sections will be merged in the order specified, i.e. entries in later sections will override those in earlier sections.
Caching
All configuration is parsed into multidimensional PHP arrays. The arrays are then stored in cache files so that expensive parsing is bypassed.
Cache keys are determined based on
- the list of source files names
- the list on section names.
Cache is automatically validated based on file modification timestamps. Hence, the cache will automatically update itself whenever any of the source configuration files changes.
Parsing
Extendable parsers are used to parse different file formats. Currently implemented parsers are:
- Ini (for .ini files)
- Json (for .json files)
- Php (for .php files)
- Autodetect (automatically picks the right parser based on extension)
- Dummy (for unit tests etc.)
Accessing configuration values
Configuration can be accessed via dot-paths. These paths are dynamically resolved against the internal array-representation of configuration. This allows retrieving individual entries as well as collections of entries.
Factory::getInstance()->getSpace('application')->get('redis.retries', 3);
Factory::getInstance()->getSpace('application')->get('redis.hosts', 'localhost');
Factory::getInstance()->getSpace('application')->get('redis', ['retries': 3, 'hosts': 'localhost]);
You can also retrieve the configuration as a flat array of dot-path to value mappings:
Factory::getInstance()->getSpace('application')->flatten();
Using environment variables
There is no explicit functionality to handle environment variables, however they can be dynamically used in the configurations via the placeholders mechanism:
$space = Factory::getInstance()->getSpace('application');
foreach ($_ENV as $key => $value) {
$placeholder = sprintf('%%ENV_%s%%', strtoupper(str_replace('%', '_', $key)));
$space->addPlaceholder($placeholder, $value);
}
With these few lines in place, a configuration file could look like this:
[main]
user.name = %ENV_USER%
user.home = %ENV_HOME%
License
THE ICONIC config library for PHP is released under the MIT License.
theiconic/config 适用场景与选型建议
theiconic/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.09k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 theiconic/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 theiconic/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-26