corex/config
Composer 安装命令:
composer require corex/config
包简介
Config Component with adapter support (array, array-files, env, server, etc...)
README 文档
README
Breaking changes - this package has been rewritten from scratch to be more strict and flexible to use. Adapters are supported in favor of loaders. Breaking changes can be found in CHANGELOG.
Getting configuration values works by creating instance of Config::class and use methods to get values. Multiple adapters are supported.
Example:
$adapter = new ArrayAdapter([ 'actor' => [ 'name' => 'James Bond', ], ]); $config = new Config([$adapter]); $actorName = $config->getString('actor.name');
A method section() is added for convenience when section and rest of key is separated eg. section = "actor" and key = "name".
Example of using section():
$adapter = new ArrayAdapter([ 'actor' => [ 'name' => 'James Bond', ], ]); $config = new Config([$adapter]); $actorName = $config->section('actor')->getString('name');
A method getConfigClassObject() is added for getting array and pass to config-class on constructor.
Example of using getConfigClassObject():
class MyConfigClass implements ConfigClassInterface { public function __construct(array $data) { } public static function getSection(): string { return 'actor'; } } $adapter = new ArrayAdapter([ 'actor' => [ 'name' => 'James Bond', ], ]); $config = new Config([$adapter]); /** @var MyConfigClass $actorConfig */ $myConfigClass = $config->getConfigClassObject(MyConfigClass::class);
ConfigFactory
A ConfigFactory exists to make it easier to create instances of Config::class.
Example:
$config = (new ConfigFactory()) ->createWithServerAndEnvAndProjectConfigArrayFileAdapter();
The above example exposes 3 adapters ServerAdapter, EnvAdapter and ProjectConfigArrayFileAdapter.
From above example, when getting value, the process is following.
- ServerAdapter is checked for key. If found, value is returned.
- EnvAdapter is checked for key. If found, value is returned.
- ProjectConfigArrayFileAdapter is checked for key. If found, value is returned.
Based on various methods to get values, a null is returned or an exception is thrown.
More methods exists, but in the situation where they does not fit in, instantiate Config::class with your own order of adapters.
Keys
Every key must be specified as dot notation e.g. "actor.name".
"_" and "-" will not be treated as separators.
When using ServerAdapter and EnvAdapter, the key will be converted to shoutcase e.g. ACTOR_NAME. This makes it easy to override values in e.g. cloud environments.
Key object is passed to adapter and multiple methods exists to get key in various formats. Use custom() to build your own.
Config
Various methods exists on config-class for getting values in correct format. There exists methods for getting specific type with or without null eg. "getString()" or "getStringOrNull()" . Following types are supported: string, int, bool translated-bool, double, array, list.
For all type methods, value from adapters will be checked if they are correct type, otherwise an exception is thrown.
"translated-bool" translates/converts following values to boolean.
Values for true : ['true', true, 'yes', 'on', 1, '1'].
Values for false : ['false', false, 'no', 'off', 0, '0'].
"list" means an array where keys are numeric keys 0..n.
Adapters
It is possible to write your own adapter by extending AbstractAdapter or implementing AdapterInterface.
Following standard adapters expose arrays as the basis for configuration values.
ArrayAdapter
Serve simple array.
$adapter = new ArrayAdapter([ 'actor' => [ 'name' => 'James Bond', ], ]);
EnvAdapter
Serve $_ENV global array.
$adapter = new EnvAdapter();
ServerAdapter
Serve $_SERVER global array.
$adapter = new ServerAdapter();
ArrayFileAdapter
Serve php array files outside project root.
$adapter = new ArrayFileAdapter(new Filesystem(), '/config-dir-outside-project-root');
ProjectPathArrayFileAdapter - Serve
Serve php array files in project root from relative directory.
$adapter = new ProjectPathArrayFileAdapter(new Filesystem(), 'my-config-dir');
ProjectConfigArrayFileAdapter
Serve php array files in project root from relative directory called "config".
$adapter = new ProjectConfigArrayFileAdapter(new Filesystem());
Array files.
Example of an array-file.
Name of file "bond.php".
<?php declare(strict_types=1); return [ 'actor1' => [ 'firstname' => 'Roger', 'lastname' => 'Moore' ], 'actor3' => [ 'firstname' => 'Daniel', 'lastname' => 'Craig' ] ];
These type of files can be loaded via ArrayFileAdapter, ProjectPathArrayFileAdapter and ProjectConfigArrayFileAdapter.
Example of a config-key: "bond.actor1.firstname" which will return "Roger".
First section of key "bond" indicates the section and on these adapters the filename.
corex/config 适用场景与选型建议
corex/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 94 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 corex/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 corex/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 94
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-28