simplecomplex / config
类似Simple Cache的配置接口;ini文件作为源,缓存作为存储。
Requires
- php: >=7.0
- simplecomplex/cache: ^1.3 || dev-develop
- simplecomplex/utils: ^1.8 || ^2.0 || dev-develop
Suggests
- simplecomplex/inspect: CLI features do NOT work without Inspect. And great for logging; better variable dumps and traces.
This package is auto-updated.
Last update: 2024-08-29 04:20:03 UTC
README
简单且分节的配置接口
ConfigInterface
基本上是PSR-16 Simple Cache,但没有生存时间。
有一个键和一个值。
这个原则在一般使用中并不理想,除非你喜欢非常长和复杂的键名。
SectionedConfigInterface
将项目键拆分为 'section' 和 'key',便于更组织化的结构。
直接的好处是,你可以对属于特定库的所有项目进行命名空间。
分区-键模式可以有两种实现和利用方式
- 连接:配置设置器和获取器只是简单地将分区和键连接起来
- 列表:分区是键的列表,你可以访问整个分区以及特定的键
SectionedConfigInterface
还允许通过remember()
和forget()
方法在内存中临时保存项目。
实现
基于环境变量
EnvConfig
是服务器环境变量的简单抽象。
EnvSectionedConfig
是一个分区实现,使用连接。
ini文件作为源,缓存作为存储
基于ini文件的类解析ini文件,并保存到缓存存储。
它们从它们的ini文件路径递归读取。这允许你克隆和使用来自多个版本控制仓库的ini文件。
ini文件是如此的老式...
是的,但ini格式比JSON、YAML等更不易出错。
语法如此简单,几乎不可能出错。而且操作人员习惯于使用ini文件。
缓存层
是 SimpleComplex Cache PersistentFileCache
。
缓存存储名称以 'config.'
前缀。
注意冲突;不要以这种方式前缀其他缓存存储。
基于ini的配置类型
IniConfig
不是分区的。简单但可能不是非常有用。
$value = $config->get('key')
IniSectionedConfig
是一种强大的通用实现。
$value = $config->get('section', 'key')
从基本路径和覆盖路径读取ini文件。
将开发/生产不变变量(ini文件)保存在基本路径中。
让操作人员在覆盖路径中保持生产变量。
使用列表原则 - 并完全支持remember()
和forget()
- IniSectionedConfig
在有限程序中访问一个分区的许多/所有键是最优的。
IniSectionedFlatConfig
是一个分区实现,使用连接。
对于期望以不可预测的方式访问不同分区键的配置类型,但仍然希望有分区的组织优势;许多但精确的缓存读取。
SimpleComplex Locale使用此配置类进行本地化文本。
抽象
Config
类是分区配置的抽象。
在这个包中,它扩展了IniSectionedConfig
。
在扩展包中,它可以是其他分区配置实现。
CLI接口
CliConfig
提供CLI命令来设置、获取和删除配置项。
并提供刷新和导出完整配置存储的命令。
它通过Config
类公开IniSectionedConfig
实例。
其他配置类不能通过CLI访问。
全局配置
Config
默认提供名为'global'的实例。
典型的系统可能从单个配置实例中受益,用于大部分项目。
由于整个系统的运行时都是基于缓存的,因此使用多个实例没有性能上的理由。
依赖注入容器ID:config
建议:通过DI容器ID 'config' 访问(并实例化)全局配置。
参见SimpleComplex Utils Dependency
。
示例
// 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命令
# 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
安装
在文档根目录旁边创建一个'conf'目录。
如下
/var/www/my-host/
http
/var/www/my-host/
conf
在'conf'目录内创建'base'和'override'路径,例如
conf/
ini/base
conf/
ini/override
对于'全局'配置存储,将系统
.ini配置文件放在'base'和'override'路径下
使用文件扩展名global.ini
(即[store name].ini
)。
如下
conf/ini/base/
some-ding.global.ini
conf/ini/override/
some-ding.prod.global.ini
如果该目录结构不合适,则可
- 向
Config
构造函数提供'paths'参数 - 扩展
Config
并重写其类常量PATH_DEFAULTS
要求
- PHP >=7.0
- SimpleComplex Cache
- SimpleComplex Utils