mizmoz / config
Mizmoz Config
1.0.0
2024-09-27 01:31 UTC
Requires
- php: >=8.3
- ext-mbstring: *
Requires (Dev)
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11.0
README
目标
- 轻量级
- 配置只是返回数组的php文件
- 使用 .environment 文件设置环境
- 可由环境变量覆盖
- 懒加载配置文件
入门指南
Composer 安装
composer require mizmoz/config
基本用法
设置环境
在项目的根目录中创建一个名为 .environment 的文件,其中应包含当前平台
设置为开发模式
echo 'development' > /my/project/root/.environment
或者使用系统环境
export ENVIRONMENT=development
从数组中加载配置
$config = new Config(['app' => ...]); $config->get('app...');
从配置目录加载配置
$config = Config::fromDirectory('./config', '.php'); $config->get('app...');
在处理不同环境的同时从目录加载配置
# In a config db.php return [ 'type' => 'mongo', 'hostname' => 'db.servers.com', ]; # In another config file for development db.development.php return \Mizmoz\Config\Extend::production('db', [ 'host' => 'localhost', ]); # Setup the config from directory $config = Config::fromEnvironment(Environment::create(__DIR__)); $config->get('db.type'); // mongo $config->get('db.hostname'); // localhost
覆盖配置值
# Using the envrionment override to ensure any values that come from the environment variables are treated as priority # Assuming we do something like: # export MM_DB_PORT=3333 $config = new Config([ 'db' => [ 'default' => 'mysql', 'port' => 3306, ] ]); $config->addOverride(new Env); # Access the value of 3333 $config->get('db.port');
# The same can be done for cli arguments so: # php my-script.php MM_DB_PORT=5555 $config = new Config([ 'db' => [ 'default' => 'mysql', 'port' => 3306, ] ]); $config->addOverride(new Args); # Access the value of 5555 $config->get('db.port');
# These can be chained with last priority so using the above example: # Returns 5555 $config->addOverride(new Env) ->addOverride(new Args) ->get('db.port');
访问配置
$config = new Config([ 'db' => [ 'default' => 'mysql', 'mysql' => 3306, ] ]); # Basic accessing using dot notation $config->get('db.default'); # Using the __invoke magic method $config('db'); # Accessing with other config values referenced $config->get('db.${db.default}'); # Accessing with relative references $config->get('db.${.default}');
路线图
- 添加对复制值的支持,例如网站地址,这可能会被用于多个参数。
- 需要找出一种方法,在不引入任何显著开销的情况下,在不返回配置树时完成此操作