mayoturis / properties-ini
获取和设置 ini (或 java .properties) 文件中的配置值
1.0
2015-11-16 21:30 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.1
This package is not auto-updated.
Last update: 2024-09-14 18:26:25 UTC
README
允许获取和设置存储在类似 ini(或 java .properties)文件中的配置变量
使用 composer 安装
composer require mayoturis/properties-ini
或
{ "require": { "mayoturis/properties-ini": "1.0" } }
用法
####基本示例
$config = Mayoturis\Properties\RepositoryFactory::make('path_to_configuration_file'); $config->get('DB_PASSWORD'); $config->set('DB_STRICT_MODE', true);
文件格式
读取器可以加载这些类型的行
DB_HOST=localhost // loaded as string 'localhost'
DB_USERNAME="user" // string 'user'
DB_PASSWORD = 'password' // string 'password'
DB_STRICT_MODE=true // boolean true
FLOAT_VALUE=1.1 // float 1.1
INT_VALUE=1 // int 1
NULL_VALUE=null // null
# comment // won't be loaded
; comment // won't be loaded
//comment // won't be loaded
注释必须位于行的开头
文件加载示例
name="John"
surname=Doe
age=25
salary=12.5
married=false
wife=null
将被加载为
[ "name" => "John", "surname" => "Doe", "age" => 25, "salary" => 12.5 "married" => false, "wife" => null ]
保存配置
保存配置时,将保留文件格式。因此,空行和注释将被保留。新值(不仅是更改的值)将被放置在文件的末尾
其他示例
获取所有配置值
$config = Mayoturis\Properties\RepositoryFactory::make('path_to_configuration_file'); $configurationArray = $config->all();
设置值
// file .env
name=John
surname="Doe"
# In 2015
age=25
// file index.php $config = Mayoturis\Properties\RepositoryFactory::make(__DIR__ . '/.env'); $config->set('name', 'Johny'); $config->set('age', 35);
// file .env after
name=Johny
surname="Doe"
# In 2015
age=35
保存所有配置值
$configArray = [ "username" => "user", "password" => "password" ]; $config = Mayoturis\Properties\RepositoryFactory::make('path_to_configuration_file'); $config->setAll($configArray);
注意:setAll 函数将覆盖所有配置值。之前的值将丢失。