fluix / fconfig
FConfig 管理
3.0.1
2022-03-31 13:20 UTC
Requires
- php: >=8.0.2
- ext-json: *
- readdle/crypto: ^2.0
- symfony/yaml: ^6.0
Requires (Dev)
README
PHP 配置管理库
安装
使用 Composer
$ composer require fluix/fconfig
{ "require": { "fluix/fconfig": "^1.0" } }
配置示例
default.json
{ "values": { "option1": "value1", "database": "schema" } }
config.json
- 使用键
base
管理嵌套配置 - 使用键
required
定义所需选项列表 - 使用
//
注释配置选项 - 可以使用环境变量通过
${ENV_VARIABLE1}
。 - 支持安全选项,例如
secret_value
。 (使用bin/fconfig encrypt|decrypt {secret} {value}
)
{ "base": "default.json", "values": { "database": "schema-overridden", "boolean": false, "null": null, "secret_value": "-CRYPT-b7139469f60891ae1670b4a2c61777faa1ee84e070f3a541ed9cdf5a61f3ebe5", "int": 397, "// commented-key": "val", "object": { "key": 21 }, "array": [ { "option31": "${ENV_VARIABLE1}", "option32": "${ENV_VARIABLE2}" } ], "nested": { "child1": { "child2": { "env": "test_env_value_json2" } } } }, "extra-config1": {}, "extra-config2": {}, "required": [ "option1", "database" ] }
示例
完整使用
假设我们有一个文件夹结构
your-app
├── composer.json
├── composer.lock
├── config
│ ├── _generated
│ ├── config.json
│ └── default.json
├── dump-config.php
└── vendor
├── autoload.php
├── bin
├── composer
├── fluix
├── readdle
└── symfony
<?php require_once "vendor/autoload.php"; // Secret is a random string with length of 16 characters, which is used for encrypting/decrypting your secure configuration. // Example of generating secret via openssl: openssl rand -hex 8 $secret = "ff7f8dc665734d9d"; // don't use this secret in your application, generate a new one and store it in a safe place. $config = \Fluix\Config\Factory::config( $secret, null, "postProcessor" ); $pathToConfig = __DIR__ . "/config/config.json"; $configFolder = __DIR__ . "/config/_generated"; $config->dump( \Fluix\Config\Template::fromPath($pathToConfig), $const = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::const()), $yaml = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::yaml()), $array = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::php()), $json = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::json()) ); echo "Config has been successfully dumped to {$const}" . PHP_EOL; echo "Config has been successfully dumped to {$yaml}" . PHP_EOL; echo "Config has been successfully dumped to {$array}" . PHP_EOL; echo "Config has been successfully dumped to {$json}" . PHP_EOL; function postProcessor(\Fluix\Config\ParserResult $result) { // extra actions could be performed here // e.g. nginx.conf could be generated withing the parsed data }
cd your-app
ENV_VARIABLE1=test_env ENV_VARIABLE2=test_env_2 php dump-config.php
输出
Config has been successfully dumped to your-app/config/_generated/config.const.php
Config has been successfully dumped to your-app/config/_generated/config.parameters.yml
Config has been successfully dumped to your-app/config/_generated/config.array.php
Config has been successfully dumped to your-app/config/_generated/config.json
使用回退机制的使用方法
您还可以使用 JSON 配置文件作为值源。
假设我们在文件夹结构的根目录下有 fallback.json
your-app
├── composer.json
├── composer.lock
├── fallback.json
├── config
│ └── ...
└── ...
<?php require_once "vendor/autoload.php"; // Secret is a random string with length of 16 characters, which is used for encrypting/decrypting your secure configuration. // Example of generating secret via openssl: openssl rand -hex 8 $secret = "ff7f8dc665734d9d"; // don't use this secret in your application, generate a new one and store it in a safe place. $pathToFallbackFile = __DIR__ . "/fallback.json"; $pathToConfig = __DIR__ . "/config/config.json"; $configFolder = __DIR__ . "/config/_generated"; $config = \Fluix\Config\Factory::config( $secret, Fluix\Config\File::fromPath($pathToFallbackFile), "postProcessor" ); $config->dump( \Fluix\Config\Template::fromPath($pathToConfig), $const = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::const()), $yaml = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::yaml()), $array = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::php()), $json = \Fluix\Config\Dump\Destination::create($configFolder, \Fluix\Config\Dump\Format::json()) ); function postProcessor(\Fluix\Config\ParserResult $result) { // extra actions could be performed here // e.g. nginx.conf could be generated withing the parsed data }
echo "{\"ENV_VARIABLE2\":\"test_env_2\"}" > your-app/fallback.json cd your-app ENV_VARIABLE1=test_env php dump-config.php
结果文件夹结构
your-app
├── composer.json
├── composer.lock
├── {fallback.json}
├── config
│ ├── _generated
│ │ ├── config.array.php
│ │ ├── config.const.php
│ │ ├── config.json
│ │ └── config.parameters.yml
│ ├── config.json
│ └── default.json
├── dump-config.php
└── vendor
├── autoload.php
├── bin
├── composer
├── fluix
├── readdle
└── symfony
config.const.php
的内容将如下
<?php // generated by Fluix\Config\Dump\PhpConstDumper, do not edit define('option1', 'value1'); define('database', 'schema-overridden'); define('boolean', false); define('null', NULL); define('secret_value', 'secret-value-here'); define('int', 397); define('object', array ( 'key' => 21, )); define('array', array ( 0 => array ( 'option31' => 'test_env', ), )); define('nested', array ( 'child1' => array ( 'child2' => array ( 'env' => 'test_env_value_json2', ), ), ));
config.array.php
的内容将如下
<?php // generated by Fluix\Config\Dump\PhpDumper, do not edit return array ( 'option1' => 'value1', 'database' => 'schema-overridden', 'boolean' => false, 'null' => NULL, 'secret_value' => 'secret-value-here', 'int' => 397, 'object' => array ( 'key' => 21, ), 'array' => array ( 0 => array ( 'option31' => 'test_env', ), ), 'nested' => array ( 'child1' => array ( 'child2' => array ( 'env' => 'test_env_value_json2', ), ), ), );
config.parameters.yml
的内容将如下
parameters: option1: value1 database: schema-overridden boolean: false 'null': null secret_value: secret-value-here int: 397 object: { key: 21 } array: [{ option31: test_env }] nested: { child1: { child2: { env: test_env_value_json2 } } }
config.json
的内容将如下
{ "option1": "value1", "database": "schema-overridden", "boolean": false, "null": null, "secret_value": "secret-value-here", "int": 397, "object": { "key": 21 }, "array": [ { "option31": "test_env" } ], "nested": { "child1": { "child2": { "env": "test_env_value_json2" } } } }