jan-di / config
此包已被废弃且不再维护。没有建议的替代包。
从环境变量和 .env 文件中加载、验证和缓存配置的库
0.3.0
2021-01-06 00:32 UTC
Requires
- php: >=7.4
Requires (Dev)
- dg/bypass-finals: ^1.3
- friendsofphp/php-cs-fixer: ^2.17
- josegonzalez/dotenv: ^3.2
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.15.0
- symfony/dotenv: ^5.2
- vimeo/psalm: ^4.3
- vlucas/phpdotenv: ^5.2
Suggests
- josegonzalez/dotenv: Optional for loading .env files
- symfony/dotenv: Optional for loading .env files
- vlucas/phpdotenv: Optional for loading .env files
README
从环境变量和 .env 文件中加载、验证和缓存配置的库
安装
通过 composer 安装
composer require jan-di/config
用法
基本用法
在创建 Config Builder 时,指定配置值的所有定义。您可以使用某些特定类型的约束来验证值。默认情况下,值从环境变量中读取。可选地,您可以在构建配置之前启用 Dotenv 适配器,以将变量从 .env 文件添加到环境变量中。
use Dotenv\Dotenv; use Jandi\Config\ConfigBuilder; use Jandi\Config\Dotenv\VlucasDotenvAdapter; use Jandi\Config\Entry\StringEntry; // define all config entries via a fluent API: $configBuilder = new ConfigBuilder([ (new StringEntry('APP_ENV', 'development')), (new StringEntry('APP_TOKEN'))->setMinLength(20) ]); // optional: Add a Dotenv loader to load .env files in environment before building config $dotenv = Dotenv::createImmutable(__DIR__, ''); $configBuilder->enableDotEnv(new VlucasDotenvAdapter($dotenv)); // build config array from environment variables // The values will be fetched validated against the rules from the entries. $config = $configBuilder->build();
配置缓存
为了提高性能并防止在每次请求时解析 .env 文件,应缓存配置值。这将创建一个包含所有获取值的编译后的 PHP 文件。此文件应由 Opcache 缓存以进一步优化。
use Dotenv\Dotenv; use Jandi\Config\ConfigBuilder; use Jandi\Config\Dotenv\VlucasDotenvAdapter; use Jandi\Config\Entry\StringEntry; // build config, see above $configBuilder = new ConfigBuilder([ (new StringEntry('APP_ENV'))->setDefaultValue('development'), (new StringEntry('APP_TOKEN'))->setMinLength(20) ]); $configBuilder ->enableCaching('/path/to/cache/file') // Activate caching ->enableDotEnv(new VlucasDotenvAdapter(Dotenv::createImmutable(__DIR__, '.env'))); // Since caching is enabled, the builder will check if the cache file exists. // if the cache file exists, no values are read from .env or the real environment. $config = $configBuilder->build(); // The cache file is not created automatically. Instead you have to provide a condition, // when the cache has to be written. Often this is related to the values inside the config. if (!$config->isCached() && $config->getValue('APP_ENV') === 'production') { $configBuilder->dumpCache($config); }
验证值和默认值
每个 Entrytype 都有一组自己的验证规则,可以使用流畅 API 设置。指定的值必须符合所有规则,否则配置将不会构建。请注意,默认值也会与规则集进行验证,以确保其也是一个有效的值。每个没有默认值的条目都必须指定。否则,容器将无法成功构建。
(new StringEntry('STRING', 'value1')) ->setMinLength(5) ->setMaxLength(10) ->setAllowedValues(['value1','value2']) ->setRegexPattern('/value.*/') (new BoolEntry('BOOL', 'true')); (new IntEntry('INT', '3')) ->setLowerLimit(0) ->setUpperLimit(8); (new FloatEntry('FLOAT', '5.5')) ->setLowerLimit(3.4) ->setUpperLimit(7.8);
捕获错误
容器构建器设计为如果至少有一个变量缺失或具有无效值则停止。这确保了成功构建的配置总是包含所有所需值。BuildException 有几个方法可以以不同格式显示所有缺失/无效值。
try { $builder->build(); } catch(BuildExceptionTest $e) { echo $e->getTextSummary(); exit; } // There where 2 error/s while building configuration: // // APP_ENV [string] Value is invalid: not allowed. Allowed values: abce. // APP_TOKEN [string] Variable is missing and has no default value.
导出到数组
您可以将值/默认值导出到一个简单的数组以进一步处理。
$config = $configBuilder->build(); $values = $config->exportValues(); $defaultValues = $config->exportDefaultValues(); // returns something like: ['APP_ENV' => 'development']
支持的 DotEnv 适配器
目前,以下 Dotenv 库直接支持
或者,您可以通过实现 AdapterInterface 来提供自己的。