phoole/config
Composer 安装命令:
composer require phoole/config
包简介
A slim configuration loader library for PHP.
README 文档
README
A slim configuration loader library for PHP. It loads PHP, JSON, YAML config files, easy to use, yet powerful. It requires PHP 7.2+ and is compliant with PSR-1, PSR-4, PSR-12.
Installation
Install via the composer utility.
composer require "phoole/config=1.*"
or add the following lines to your composer.json
{
"require": {
"phoole/config": "1.*"
}
}
Features
-
Simple interface,
get($id),has($id),with($id, $value). -
One central place for all config files for ease of management.
config/ | |___ production/ | | | |___ host1/ | | |___ db.php | | |___ redis.php | | | |___ db.php | |___ dev/ | | | |___ redis.php | |___ db.php | |___ db.php |___ redis.php |___ system.php -
May use an environment value, such as
productionorproduction/host1for switching between different configurations. -
Use of references in configuration value is fully supported, such as
${system.tmpdir}. -
Get environment values using
$config->get('ENV.USER')or${ENV.USER} -
Hierarchy configuration structure with dot notation like
db.auth.host. -
Support
.php,.json,.yml(need yaml extension installed) type of config files.
Usage
-
Usually application running environment is different on different servers. A good practice is setting environment in a
.envfile somewhere on the host, and put all configuration files in one centralconfig/directory.A sample
.envfile,# installation base BASE_DIR=/www # app directory APP_DIR=${BASE_DIR}/app # config directory CONFIG_DIR=${APP_DIR}/config # app env for current host APP_ENV=production/host1
In a sample
bootstrap.phpfile,use Phoole\Config\Config; use Phoole\Env\Environment; // load server environment from '.env' file (new Environment())->load(__DIR__ . '/.env'); // create config instance with the config file loader $config = new Config(getenv('CONFIG_DIR'), getenv('APP_ENV')); // object access of $config $db_config = $config->get('db');
-
Central config directory and configuration grouping
-
Configuration grouping
Configurations are gathered into one directory and are grouped into files and subdirectories for ease of management.
For example, the
config/system.phpholdssystem.*configurations// system.php return [ 'tmpdir' => '/usr/local/tmp', // ... ];
Later,
systemrelated configs can be retrieved as$dir = $config->get('system.tmpdir');
Or being used in other configs as references.
-
Configuration files loading order
If the environment is set to
production/host1, the config files loading order are (assume config files are*.php),-
config/config/*.php -
config/production/*.php -
config/production/host1/*.php
Configuration values are overwritten and replaced those from later loaded files.
-
-
-
References make your configuration easy to manage.
For example, in the
system.phpreturn [ 'tmpdir' => '/var/local/tmp', ... ];
In your
cache.phpfile,return [ // a local filesystem cache driver 'local' => [ 'driver' => 'filesystem', 'params' => [ 'root_dir' => '${system.tmpdir}/cache', // use reference here 'hash_level' => 2 ] ], ... ];
You may reset the reference start and ending matching pattern as follows,
// now reference is something like '%{system.tmpdir}%' $config->setReferencePattern('%{', '}%');
-
Environment values can be accessed through special node
'ENV'. e.g.$tmpdir = $config->get('ENV.APP_TMPDIR');
or in reference format,
return [ 'tmpdir' => '${ENV.APP_TMPDIR}' ];
-
Hierarchy configuration structure with dot notation like
db.auth.host.// returns the db config array $db_config = $config->get('db'); // returns a string $db_host = $config->get('db.auth.host');
Both flat notation and array notation are supported and can co-exist at the same time.
// db config file return [ // array notation 'auth' => [ 'host' => 'localhost', 'port' => 3306 ], // flat notation 'auth.user' => 'dbuser' ];
-
After initial loading,
$configis immutable. If you want to add new conf values. You may use,$newconf = $config->with('redis', ['host' => 'localhost']);
where
$newconfis a new configuration object.
APIs
-
-
get(string $id): mixedThe return value might be a
string,array, or evenobject. Returnsnullif not found. -
has(string $id): boolTest if
$idexists or not. Returns abooleanvalue. -
with(string $id, mixed $value): ConfigReturns a new config object with
$idset.
-
-
-
setReferencePattern(string $start, string $end): $thisReset the reference start chars and ending chars. The default are
'${'and'}'
-
Testing
$ composer test
Dependencies
- PHP >= 7.2.0
License
phoole/config 适用场景与选型建议
phoole/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 134 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「library」 「config」 「phoole」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phoole/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phoole/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phoole/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Zend Framework module to quickly and easily set PHP settings.
Inbox pattern process implementation for your Laravel Applications
Server environment detection based on config array-file
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
统计信息
- 总下载量: 134
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2019-10-07