simplecomplex/config
Composer 安装命令:
composer require simplecomplex/config
包简介
Simple Cache like configuration interfaces; ini-files as source, cache as store.
README 文档
README
Simple and sectioned configuration interfaces
ConfigInterface
is basically PSR-16 Simple Cache without time-to-live.
There's a key and a value.
That principle isn't optimal for general use, unless you like real long and complex key names.
SectionedConfigInterface
Splits the item key into 'section' plus 'key', facilitating a more organized structure.
The immediate benefit is that you can namespace all items that belong to a particular library.
The section-key pattern can be implemented and utilized in two manners:
- concatenation: the configuration setters and getters simply concatenate section and key
- listing: the section is a list of keys, you can access the whole section as well as a particular key
SectionedConfigInterface also allows for short-term keeping items in memory,
via methods remember() and forget().
Implementations
Environment variable based
EnvConfig
is a simple abstraction of server environment variables.
EnvSectionedConfig
a sectioned implementation, using concatenation.
Ini-files as source, cache as store
The ini-file based classes parse ini-files, and save to cache stores.
They read recursively from their ini-file paths. That allows one to clone and use ini-files from multiple version control repositories.
Ini-files are so old-school...
Aye, but the ini format is less error-prone than JSON, YAML, what-not.
The syntax is so simple it's hard to make mistakes. And operations people are used to ini-files.
Cache layer
is SimpleComplex Cache PersistentFileCache.
Cache store names are prefixed with 'config.'
Beware of conflict; do not prefix other cache stores that way.
Types of ini-based configuration
IniConfig
is not sectioned. Simple but probably not that useful.
$value = $config->get('key')
IniSectionedConfig
is a powerful general usage implementation.
$value = $config->get('section', 'key')
Reads ini-files from a base path and an override path.
Keep development/production invariant variables (ini-files) in the base path.
Let operations keep production variables in the override path.
Using the list-principle - and fully supporting remember() and forget() -
IniSectionedConfig is optimal for accessing many/all keys of a section within a limited procedure.
IniSectionedFlatConfig
a sectioned implementation, using concatenation.
Optimal for types of configuration one expects to access keys of diverse sections in an unpredictable manner,
but still want the organisational benefit of sections; many but exact cache reads.
SimpleComplex Locale uses this config class for localized texts.
Abstraction
The Config class is an abstraction of sectioned configuration.
In this package it extends IniSectionedConfig.
In an extending package it could be some other sectioned config implementation.
CLI interface
CliConfig delivers CLI commands for setting, getting and deleting config items.
And commands for refreshing and exporting full configuration stores.
It exposes IniSectionedConfig instances, via the Config class.
The other config classes are not accessible via CLI.
Global config
Config defaults to deliver an instance named 'global'.
A typical system could probably benefit from a single config instance for the bulk of items.
Since the whole thing runtime is cache based, there's no performance reason for using multiple instances.
Dependency injection container ID: config
Recommendation: access (and thus instantiate) the global config via DI container ID 'config'.
See SimpleComplex Utils Dependency.
Example
// Bootstrap. Dependency::genericSetMultiple([ 'cache-broker' => function () { return new \SimpleComplex\Cache\CacheBroker(); }, 'config' => function() { return new \SimpleComplex\Config\Config('global'); }, ]); // ... // Use. /** @var \Psr\Container\ContainerInterface $container */ $container = Dependency::container(); /** * Create or re-initialize the 'global' config store; * based on ini-files placed in base and override paths, * cached by a PSR-16 Simple Cache cache store. * * @var \SimpleComplex\Config\IniSectionedConfig $config */ $config = $container->get('config'); /** @var mixed $whatever */ $whatever = $config->get('some-section', 'some-key', 'the default value');
CLI commands
# List all config commands and their help. php cli.php config -h # One command's help. php cli.php config-xxx -h # List existing config stores. php cli.php config-list-stores # Display/get value of a config item. php cli.php config-get store section key # Set a config item. php cli.php config-set store section key value # Delete a config item. php cli.php config-delete store section key # Refresh a config store from .ini file sources. # The fresh store gets applied atomically, when fully built. php cli.php config-refresh store # Export a config store as JSON to a file. php cli.php config-export store target-path-and-file
Installation
Create a 'conf' directory alongside the document root dir.
Like:
/var/www/my-host/http
/var/www/my-host/conf
Create 'base' and 'override' paths within the 'conf', like:
conf/ini/base
conf/ini/override
For the 'global' config store, place or symlink or git clone your system's
.ini configuration files under the 'base' and 'override' paths
using file extension global.ini (= [store name].ini).
Like:
conf/ini/base/some-ding.global.ini
conf/ini/override/some-ding.prod.global.ini
If that directory structure isn't suitable, do either:
- supply
Configconstructor with a 'paths' argument - extend
Configand override it's class constantPATH_DEFAULTS
Requirements
- PHP >=7.0
- SimpleComplex Cache
- SimpleComplex Utils
Suggestions
simplecomplex/config 适用场景与选型建议
simplecomplex/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 251 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「configuration」 「simple cache」 「psr-16」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simplecomplex/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simplecomplex/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simplecomplex/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
A simple library for make Lexer and Parsers to build a language
A simple library that allows transform any kind of data to native php data or whatever
A simple library that uses the hydration pattern to fill objects given different sources of data.
Micro Active Record library in PHP, support chain calls, events, and relations.
A simple content
统计信息
- 总下载量: 251
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 2
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-12