phlak / config
配置加载和管理
Requires
- php: ^8.0 || ^8.1 || ^8.2
- symfony/yaml: ^5.0 || ^6.0
- yosymfony/toml: ^1.0
Requires (Dev)
- phlak/coding-standards: ^2.2
- phpstan/phpstan: ^1.10
- psy/psysh: ^0.11
- yoast/phpunit-polyfills: ^2.0
README
PHP 简单配置管理库 -- 由 Chris Kankiewicz (@PHLAK) 创建
简介
Config 是一个简单的 PHP 配置管理库,支持多种配置文件格式和表达式 API。
支持的文件格式
- PHP
- INI
- JSON
- TOML
- YAML
- XML
要求
- PHP >= 7.1
使用 Composer 安装
composer require phlak/config
初始化客户端
首先,导入 Config
use PHLAK\Config\Config;
然后实例化类
$config = new Config($context, $prefix = null);
其中 $context
是以下之一
- 支持文件类型的路径
- 包含一个或多个支持文件类型的目录路径
- 配置选项的数组
$prefix
是用作此 Config 对象选项键前缀的字符串。
配置文件格式
PHP
PHP 配置文件必须有 .php
文件扩展名,是有效的 PHP 文件,并返回有效的 PHP 数组。
示例 PHP 文件
<?php return [ 'driver' => 'mysql', 'drivers' => [ 'sqlite' => [ 'database' => 'database.sqlite', 'prefix' => '' ], 'mysql' => [ 'host' => 'localhost', 'database' => 'blog', 'username' => 'blogger', 'password' => 'hunter2', 'charset' => 'utf8', 'prefix' => '' ] ] ];
INI
INI 配置文件必须有 .ini
文件扩展名,是有效的 INI 文件。
示例 INI 文件
driver = mysql [drivers] sqlite[database] = database.sqlite sqlite[prefix] = mysql[host] = localhost mysql[database] = blog mysql[username] = blogger mysql[password] = hunter2 mysql[charset] = utf8 mysql[prefix] =
JSON
JSON 配置文件必须有 .json
文件扩展名,包含有效的 JSON 对象。
示例 JSON 文件
{ "driver": "mysql", "drivers": { "sqlite": { "database": "database.sqlite", "prefix": "" }, "mysql": { "host": "localhost", "database": "blog", "username": "blogger", "password": "hunter2", "charset": "utf8", "prefix": "" } } }
TOML
TOML 配置文件必须有 .toml
文件扩展名,是有效的 TOML 文件。
示例 TOML 文件
driver = 'mysql' [drivers.sqlite] database = 'database.sqlite' prefix = '' [drivers.mysql] host = 'localhost' database = 'blog' username = 'blogger' password = 'hunter2' charset = 'utf8' prefix = ''
YAML
YAML 配置文件必须有 .yaml
文件扩展名,是有效的 YAML 文件。
示例 YAML 文件
driver: mysql drivers: sqlite: database: database.sqlite prefix: mysql: host: localhost database: blog username: blogger password: hunter2 charset: utf8 prefix:
XML
XML 配置文件必须有 .xml
文件扩展名,包含有效的 XML。
示例 XML 文件
<?xml version='1.0'?> <database> <driver>mysql</driver> <drivers> <sqlite> <database>database.sqlite</database> <prefix></prefix> </sqlite> <mysql> <host>localhost</host> <database>blog</database> <username>blogger</username> <password>hunter2</password> <charset>utf8</charset> <prefix></prefix> </mysql> </drivers> </database>
用法
__construct
创建一个新的 Config 对象。
Config::__construct( mixed $context [, string $prefix = null ] ) : Config
$context
- 配置选项的原始数组或配置文件的路径或包含一个或多个配置文件的目录的路径
$prefix
- 加载配置将被嵌套的键
示例
从 YAML 文件创建新的 Config 对象。
$config = new Config('path/to/conifg.yaml');
从配置文件目录创建新的 Config 对象。
$config = new Config('path/to/conifgs/');
从数组创建新的 Config 对象。
$config = new Config([ 'hostname' => 'localhost', 'port' => 12345 ]);
set
使用指定的键存储配置值。
Config::set( string $key, mixed $value ) : bool
$key
- 唯一的配置选项键
$value
- 配置项目值
示例
$config->set('hostname', 'localhost'); $config->set('port', 12345);
get
通过提供的键检索配置选项。
Config::get( string $key [, mixed $default = null ] ) : mixed
$key
- 唯一的配置选项键
$value
- 配置项目值
示例
// Return the hostname option value or null if not found. $config->get('hostname');
定义如果选项未设置时返回的默认值。
// Returns 'localhost' if hostname option is not set $config->get('hostname', 'localhost');
has
检查配置项目是否存在。
Config::has( string $key ) : bool
$key
- 唯一的配置选项键
示例
$config = new Config([ 'hostname' => 'localhost' ]); $config->has('hostname'); // Returns true $config->has('port'); // Returns false
append
将值附加到现有的数组配置选项。
Config::append( string $key, mixed $value ) : bool
$key
- 唯一的配置选项键
$value
- 配置项目值
示例
将 baz
附加到 tags
配置项目数组。
$config->set('tags', ['foo', 'bar']) $config->append('tags', 'baz'); // ['foo', 'bar', 'baz']
prepend
将值附加到现有的数组配置选项的开头。
Config::append( string $key, mixed $value ) : bool
$key
- 唯一的配置选项键
$value
- 配置项目值
示例
将 baz
附加到 tags
配置项目数组的开头。
$config->set('tags', ['foo', 'bar']) $config->append('tags', 'baz'); // ['baz', 'foo', 'bar']
unset
通过提供的键取消设置配置选项。
Config::unset( string $key ) : bool
$key
- 唯一的配置选项键
示例
$config->unset('hostname');
load
从文件或目录加载配置选项。
Config::load( string $path [, string $prefix = null [, bool $override = true ]] ) : self
$path
- 配置文件或目录的路径
$prefix
- 加载配置将被嵌套的键
$override
- 是否用加载的文件中的值覆盖现有选项
示例
加载单个附加文件。
$conifg->load('path/to/config.php');
使用前缀加载附加文件。
$config->load('databaes.php', 'database');
加载附加文件,不覆盖现有值。
$config->load('additional-options.php', null, false);
合并
将另一个 Config 对象合并到当前对象中。
Config::merge( Config $config [, bool $override = true ] ) : self
$config
- Config 实例
$override
- 是否使用合并的配置对象中的值覆盖现有选项
示例
将 $anotherConfig 合并到 $config 中,并使用 $anotherConfig 中的值覆盖 $config
中的值。
$anotherConfig = new Config('some/config.php'); $config->merge($anotherConfig);
将 $anotherConfig
合并到 $config
中,不覆盖任何值。在 $anotherConfig
中的重复值将丢失。
$anotherConfig = new Config('some/config.php'); $config->merge($anotherConfig, false);
拆分
将配置选项的子数组拆分到它自己的 Config 对象中。
Config::split( string $key ) : Config
$key
- 唯一的配置选项键
示例
$config = new Config([ 'foo' => 'foo', 'bar' => [ 'baz' => 'barbaz' ], ]); $barConfig = $config->split('bar'); $barConfig->get('baz'); // Returns 'barbaz'
toArray
返回整个配置作为一个数组。
Config::toArray( void ) : array
示例
$config = new Config(['foo' => 'foo']); $config->toArray(); // Returns ['foo' => 'foo']
故障排除
有关一般帮助和支持,请加入我们的 GitHub 讨论区 或在 Twitter 上联系我们。
请向 GitHub 问题跟踪器 报告错误。
版权
本项目受 MIT 许可证 许可。