userfrosting / config
此包已被弃用且不再维护。未建议替代包。
UserFrosting 的配置模块
4.5.0
2021-04-21 00:50 UTC
Requires
- php: >=7.1
- userfrosting/support: ~4.5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- phpunit/phpunit: ^7.5 | ^8.5
README
分支 | 构建 | 覆盖率 | 风格指南 |
---|---|---|---|
master | |||
develop | |
使用说明
在 /path/to/core/config/
目录下创建一个名为 default.php
的文件
default.php
return [
'contacts' => [
'housekeeper' => [
'name' => 'Alex',
'email' => 'alex@cleansthetoilet.com'
]
]
];
假设您还有一个可以覆盖此基础配置文件值的配置文件。例如,在 /path/to/plugin/config/
中,您有
default.php
return [
'contacts' => [
'housekeeper' => [
'name' => 'Alex "the man" Weissman'
]
]
];
您可以使用 ConfigPathBuilder
类生成这些配置文件的有序列表,并使用 UserFrosting\Support\Respository\Loader\ArrayFileLoader
实例将它们合并。
路径构建器
创建 ResourceLocator
和 ConfigPathBuilder
类以构建配置文件列表
$locator = new ResourceLocator(__DIR__);
$locator->registerLocation('core', 'path/to/core');
$locator->registerLocation('plugin', 'path/to/plugin');
$locator->registerStream('config', '', 'config/');
$builder = new ConfigPathBuilder($locator, 'config://');
$paths = $builder->buildPaths();
// Generates a list of paths:
[
'/core/config/default.php'
'/plugin/config/default.php'
]
数据加载器
然后,您可以使用 ArrayFileLoader
类从该路径列表加载并合并所有配置数据
$loader = new \UserFrosting\Support\Respository\Loader\ArrayFileLoader($builder->buildPaths());
$config = new \UserFrosting\Support\Respository\Repository($loader->load());
指定多个路径中的配置文件将按路径指定的顺序合并。您现在可以通过标准的 Repository
方法访问您的配置数据
echo $config->get('contacts.housekeeper.name');
// Prints 'Alex'
您还可以在每个路径中指定特定环境的配置文件。如果将环境名称传递给 buildPaths()
,则 ConfigPathBuilder
将在合并 default.php
后立即合并路径中的特定环境文件
development.php
return [
'database' => [
'password' => 'sup3rC-cr3t'
]
];
要合并此文件,您将调用
$paths = $builder->buildPaths('development');