niirrty / niirrty.config
配置库。
Requires
- php: >=8.1
- ext-json: *
- ext-simplexml: *
- ext-xmlwriter: *
- niirrty/niirrty.date: ^0.6
- niirrty/niirrty.io: ^0.6
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: 10.*
README
一个配置帮助库
配置值必须始终是某个部分的组成部分,并且配置可以包含多个部分
支持的配置格式
目前支持以下格式
- JSON
- XML
- PHP
但你可以通过扩展Niirrty\Config\Provider\BaseConfigProvider
并实现Niirrty\Config\Provider\IConfigProvider
接口轻松创建自己的格式。
提供者用于从特定格式读取配置数据并将配置数据写入特定格式。
安装
这是一个通过composer提供的包
composer require niirrty/niirrty.config ^0.4
或者在composer.json
的require
区域
{ "require": { "php": ">=8.0", "niirrty/niirrty.config": "^0.4" } }
使用方法
要从特定的JSON配置文件中获取配置数据
<?php use Niirrty\Config\Provider\JSONConfigProvider; try { // Read configuration from JSON file $config = JSONConfigProvider::Init( __DIR__ . '/data/config.json', // JSON config file path [ 'json' ], // Allowed file extension(s) 'JSON' // The provider name ) ->read(); // Output the config data, converted to a array print_r( $config->toArray() ); // Save config (after changes) to an other file if ( ! \file_exists( __DIR__ . '/data/config-saved.json' ) ) { // Set/Change value for section 'default' and item 'foo' $config[ 'default::foo' ] = true; // You can do the same by: # $config[ 'default' ][ 'foo' ] = true; // or by: # $config->setValue( 'default', 'foo', true ); // assign new config file path to provider $config->getProvider()->setOption( 'file', __DIR__ . '/data/config-saved.json' ); // Save the config with the owning provider $config->getProvider()->write( $config ); } } catch ( \Throwable $ex ) { // Handle errors echo $ex; }
其他支持的文件格式可以通过\Niirrty\Config\Provider\PHPConfigProvider
和\Niirrty\Config\Provider\JSONConfigProvider
类访问。
格式转换
不需要特殊的东西。例如:您可以使用JSON提供者进行读取,并使用XML提供者进行写入
<?php use Niirrty\Config\Provider\JSONConfigProvider; use Niirrty\Config\Provider\XMLConfigProvider; try { // Read configuration from JSON file $config = JSONConfigProvider::Init( __DIR__ . '/data/config.json', // JSON config file path [ 'json' ], // Allowed file extension(s) 'JSON' // The provider name ) ->read(); // Output the config data, converted to a array print_r( $config->toArray() ); // Set/Change value for section 'default' and item 'foo' $config[ 'default::foo' ] = true; // Save the config with a new XML provider XMLConfigProvider::Init( __DIR__ . '/data/config.json', // XML config file path [ 'xml' ], // Allowed file extension(s) 'XML' // The provider name ) ->write( $config ); } catch ( \Throwable $ex ) { // Handle errors echo $ex; }
JSON配置格式
JSON配置文件必须按照以下格式定义
[ { "name": "Section name", "description": "A optional section description…", "items": [ { "name": "config item name 1", "description": "A optional item description…", "nullable": false, "type": "bool", "value": false } ] } ]
部分必须通过'name'和'items'定义。'description'是可选的。
项目必须通过'name'和'value'属性定义。'description'是可选的。'nullable'默认为false,'type'默认为"string"。
配置可以定义0个或多个部分。部分必须定义0个或多个项目。
PHP配置格式
PHP配置文件必须按照以下格式定义
<?php return [ [ 'name' => 'Section name', 'description' => 'A optional section description…', 'items' => [ [ 'name' => 'config item name 1', 'description' => 'A optional item description…', 'nullable' => false, 'type' => 'bool', 'value' => false ], // more items… ], // more sections… ] ];
部分必须通过'name'和'items'定义。'description'是可选的。
项目必须通过'name'和'value'属性定义。'description'是可选的。'nullable'默认为false,'type'默认为string。
配置可以定义0个或多个部分。部分必须定义0个或多个项目。
关联数组格式
您还可以使用关联数组。键必须是部分和项目名称
<?php return [ 'Section name' => [ 'description' => 'A optional section description…', 'items' => [ 'config item name 1' => [ 'description' => 'A optional item description…', 'nullable' => false, 'type' => 'bool', 'value' => false ], // more items… ], // more sections… ] ];
XML配置格式
XML配置文件必须按照以下格式定义
<?xml version="1.0" encoding="UTF-8" ?> <Config> <Section name="Section name"> <Description>'A optional section description…</Description> <Item name="config item name 1" type="bool" nullable="false" value="false" description="A optional item description…"/> <!-- More items <Item…/> --> </Section> </Config>
部分必须通过'name'和'items'定义。'description'是可选的。
项目必须通过'name'和'value'属性定义。'description'是可选的。'nullable'默认为false,'type'默认为string。
配置可以定义0个或多个部分。部分必须定义0个或多个项目。
如果项目的描述和/或值包含多个行或其他特殊意义字符,它们也可以定义为单独的元素
<?xml version="1.0" encoding="UTF-8" ?> <Config> <Section name="Section name"> <Description>'A optional section description…</Description> <Item name="config item name 1" type="bool" nullable="false"> <Description>A optional item description…</Description> <Value>false</Value> </Item> </Section> </Config>