initphp/config
Composer 安装命令:
composer require initphp/config
包简介
Advanced configuration manager for PHP with dotted-path access, file/directory/class loaders and a static facade.
关键字:
README 文档
README
Advanced configuration manager for PHP: a single configuration tree with dotted-path access, case-insensitive keys, and loaders for arrays, PHP files, whole directories and class/object properties — usable either as an injectable object or through a static facade.
Requirements
- PHP 8.1 or higher
- initphp/parameterbag
^2.0
Installation
composer require initphp/config
Key Concepts
- Dotted paths —
db.hostaddresseshostinside thedbarray. Keys can be nested arbitrarily deep. - Case-insensitive keys —
DB.Host,db.hostandDb.HOSTall refer to the same entry. Keys are folded to lower-case internally. - Three entry points that all share the same read/write contract
(
ConfigInterface):
Quick Start
Configuration classes
Declare your configuration as public properties and read it through the shared API:
use InitPHP\Config\Classes; final class MyAppConfig extends Classes { public string $url = 'http://lvh.me'; public string $name = 'LocalHost'; /** @var array<string, string> */ public array $db = [ 'host' => 'localhost', 'user' => 'root', ]; } $config = new MyAppConfig(); $config->get('url'); // "http://lvh.me" $config->get('db.host'); // "localhost" $config->get('details', 'Not Found'); // "Not Found" if ($config->has('name')) { $config->get('name'); // "LocalHost" }
The Library object
use InitPHP\Config\Library; $config = new Library(); $config->set('site.url', 'http://lvh.me') ->set('site.db.host', 'localhost'); $config->get('site.url'); // "http://lvh.me" $config->get('SITE.DB.HOST'); // "localhost" (case-insensitive)
The Config static facade
Every call is forwarded to a lazily created, process-wide Library
singleton:
use InitPHP\Config\Config; Config::setArray('site', ['url' => 'http://lvh.me']); Config::get('site.url'); // "http://lvh.me" Config::has('site.url'); // true Config::reset(); // discard the shared instance (useful in tests)
Reading and writing
The following methods are available on Classes, Library, and the
Config facade alike:
| Method | Description |
|---|---|
get(string $key, mixed $default = null): mixed |
Read a value, or $default when the key is absent. |
set(string $key, mixed $value): self |
Write a value (intermediate arrays are created as needed). |
has(string $key): bool |
Whether the key exists (a stored null still counts as present). |
remove(string $key): self |
Remove a key (a no-op when it is absent). |
all(): array |
The entire configuration tree as a plain array. |
Loaders (Library / Config)
setArray()
public function setArray(?string $name, array $assoc = []): self;
Imports an associative array. When $name is null or '' the array is
merged into the root; otherwise it is stored under $name.
Config::setArray('site', [ 'url' => 'http://lvh.me', 'db' => ['host' => 'localhost', 'user' => 'db_user'], ]); Config::get('site.url'); // "http://lvh.me" Config::get('site.db.host'); // "localhost"
setFile()
public function setFile(?string $name, string $path): self;
Loads a PHP file that returns an associative array.
// config/db.php return [ 'HOST' => 'localhost', 'USER' => 'root', ];
Config::setFile('db', __DIR__ . '/config/db.php'); Config::get('db.host'); // "localhost" (keys are case-insensitive)
setDir()
public function setDir(?string $name, string $path, array $exclude = []): self;
Loads every top-level *.php file in a directory. Each file is stored
under a key derived from its base name; when $name is given it becomes a
common prefix. Files can be skipped via $exclude (with or without the
.php suffix).
// config/db.php -> returns ['HOST' => 'localhost'] // config/site.php -> returns ['URL' => 'http://lvh.me'] Config::setDir('app', __DIR__ . '/config', ['secrets']); Config::get('app.db.host'); // "localhost" Config::get('app.site.url'); // "http://lvh.me"
setClass()
public function setClass(string|object $classOrObject): self;
Imports the public properties of a class or object under the class's short name. A class name imports the property defaults; an instance imports the current values.
namespace App\Config; class AppConfig { public string $url = 'http://lvh.me'; } class Database { public string $host = 'localhost'; }
Config::setClass(\App\Config\AppConfig::class); // by class name Config::setClass(new \App\Config\Database()); // by instance Config::get('appconfig.url'); // "http://lvh.me" Config::get('database.host'); // "localhost"
Object-style access (Library)
A Library exposes top-level entries as read-only nested objects:
$config = new Library(); $config->set('db.host', 'localhost')->set('db.user', 'root'); $config->db->host; // "localhost" $config->db->user; // "root"
Error handling
Loader failures throw
InitPHP\Config\Exceptions\ConfigException
(a RuntimeException): missing files, files that do not return an array,
invalid directories, and unknown class names.
use InitPHP\Config\Exceptions\ConfigException; try { Config::setFile('db', '/path/to/missing.php'); } catch (ConfigException $e) { // "Configuration file "/path/to/missing.php" was not found." }
Documentation
In-depth guides with runnable examples live in docs/.
Testing
composer test # PHPUnit composer analyse # PHPStan (level 8) composer cs:check # PHP-CS-Fixer (dry-run)
Credits
License
Released under the MIT License. Copyright © InitPHP.
initphp/config 适用场景与选型建议
initphp/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 129 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「configuration」 「config」 「Settings」 「dot-notation」 「php-library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 initphp/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 initphp/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 initphp/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
A Zend Framework module to quickly and easily set PHP settings.
Utils to load, parse and work with configuration on Mezzio projects
Server environment detection based on config array-file
Single and multi-dimensional parameter bag with dot-path access for PHP.
Trait to support creating complex nested class structures from JSON/YAML objects.
统计信息
- 总下载量: 129
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-16