ghostwriter/config
最新稳定版本:2.0.2
Composer 安装命令:
composer require ghostwriter/config
包简介
Provides an object that maps configuration keys to values.
关键字:
README 文档
README
Provides an object that maps configuration keys to values.
Installation
You can install the package via composer:
composer require ghostwriter/config
Star ⭐️ this repo if you find it useful
You can also star (????) this repo to find it easier later.
Features
- ???? Dot Notation: Access nested configuration using dot, forward slash, or backslash separators
- ???? Flexible Merging: Merge arrays, files, or directories
- ???? Array Operations: Append and prepend values to configuration arrays
- ???? Scoped Configuration: Wrap nested configuration into isolated instances
API
/** * @template T of (array<non-empty-string,T>|bool|float|int|null|string) */ interface ConfigurationInterface { /** @throws ConfigurationExceptionInterface */ public function append(string $key, mixed $value): void; /** @throws ConfigurationExceptionInterface */ public function get(string $key, mixed $default = null): mixed; /** @throws ConfigurationExceptionInterface */ public function has(string $key): bool; /** * @param array<non-empty-string,T> $options * @throws ConfigurationExceptionInterface */ public function merge(array $options): void; /** @throws ConfigurationExceptionInterface */ public function mergeDirectory(string $directory): void; /** @throws ConfigurationExceptionInterface */ public function mergeFile(string $file, ?string $key = null): void; /** @throws ConfigurationExceptionInterface */ public function prepend(string $key, mixed $value): void; public function reset(): void; /** @throws ConfigurationExceptionInterface */ public function set(string $key, mixed $value): void; /** @return array<non-empty-string,T> */ public function toArray(): array; /** @throws ConfigurationExceptionInterface */ public function unset(string $key): void; /** * @param array<non-empty-string,T> $default * @throws ConfigurationExceptionInterface */ public function wrap(string $key, array $default = []): self; }
Usage
Given the following configuration directory structure
path/to/config/directory/app.phppath/to/config/directory/database.phppath/to/config/directory/file.php
$directory = 'path/to/config/directory'; $file = 'path/to/config/directory/file.php'; $options = [ 'settings' => [ 'enable' => true, ], ]; $configuration = Configuration::new($options); $configuration->has('settings.disabled'); // false $configuration->get('settings.disabled'); // null $configuration->get('settings.disabled', 'default'); // 'default' $configuration->set('settings.disabled', false); $configuration->has('settings.disabled'); // true $configuration->get('settings.disabled'); // false $configuration->toArray(); // ['settings' => ['enable'=>true,'disabled'=>false]] $configuration->unset('settings.disabled'); $configuration->get('settings.disabled'); // null $configuration->get('settings.disabled', 'default'); // 'default' $configuration->toArray(); // ['settings' => ['enable'=>true]]
// from an array $configuration = Configuration::new($options); $configuration->toArray(); // ['settings' => ['enable'=>true]]
// merge additional config options $additionalOptions = [ 'settings' => [ 'disabled' => false, ], ]; $configuration = Configuration::new($options); $configuration->merge($additionalOptions); $configuration->toArray(); // ['settings' => ['enable'=>true,'disabled'=>false]]
// from an array with dot notation $configuration = Configuration::new($options); $configuration->toArray(); // ['settings' => ['enable'=>true]] $configuration->has('settings'); // true $configuration->has('settings.enable'); // true $configuration->get('settings.enable'); // true
// from a directory $configuration = Configuration::new(); $configuration->mergeDirectory($directory); $configuration->toArray(); // output below // [ // 'app' => ['name'=>'App','version'=>'1.0.0'], // 'database' => ['host'=>'localhost','port'=>3306], // 'file' => ['path'=>'/path/to/file'] // ]
// from a file $configuration = Configuration::new(); $configuration->mergeFile($file); $configuration->toArray(); // ['path'=>'/path/to/file'] // from a file with a namespace key $configuration = Configuration::new(); $configuration->mergeFile($file, 'custom'); $configuration->toArray(); // ['custom' => ['path'=>'/path/to/file']]
// append values $configuration = Configuration::new($options); $configuration->append('settings', ['key' => 'value']); $configuration->toArray(); // ['settings' => ['enable'=>true,'key'=>'value']]
// prepend values $configuration = Configuration::new($options); $configuration->prepend('settings', ['key' => 'value']); $configuration->toArray(); // ['settings' => ['key'=>'value','enable'=>true]]
// wrap configuration into a new scoped instance $configuration = Configuration::new([ 'database' => [ 'host' => 'localhost', 'port' => 3306, ], ]); $dbConfig = $configuration->wrap('database'); $dbConfig->get('host'); // 'localhost' $dbConfig->toArray(); // ['host' => 'localhost', 'port' => 3306] // wrap with default values when key doesn't exist $cacheConfig = $configuration->wrap('cache', ['driver' => 'redis']); $cacheConfig->toArray(); // ['driver' => 'redis']
// reset configuration $configuration = Configuration::new(['key' => 'value']); $configuration->toArray(); // ['key' => 'value'] $configuration->reset(); $configuration->toArray(); // []
// mixed separators support (dot, forward slash, backslash) $configuration = Configuration::new(); $configuration->set('app.name', 'MyApp'); $configuration->get('app.name'); // 'MyApp' $configuration->get('app/name'); // 'MyApp' (forward slash) $configuration->get('app\name'); // 'MyApp' (backslash) $configuration->has('app.name'); // true $configuration->has('app/name'); // true $configuration->has('app\name'); // true
// append/prepend to nested keys $configuration = Configuration::new(); $configuration->append('list.items', 'first'); $configuration->append('list.items', 'second'); $configuration->toArray(); // ['list' => ['items' => ['first', 'second']]] $configuration->prepend('list.items', 'zero'); $configuration->toArray(); // ['list' => ['items' => ['zero', 'first', 'second']]]
// automatic array promotion $configuration = Configuration::new(); $configuration->set('parent', 'scalar'); $configuration->set('parent.child', 'value'); // 'parent' is promoted to array $configuration->toArray(); // ['parent' => ['child' => 'value']]
Changelog
Please see CHANGELOG.md for more information on what has changed recently.
Credits
License
Please see LICENSE for more information on the license that applies to this project.
Security
Please see SECURITY.md for more information on security disclosure process.
ghostwriter/config 适用场景与选型建议
ghostwriter/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.39M 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「config」 「ghostwriter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ghostwriter/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ghostwriter/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ghostwriter/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides Environment Variables derived from `$_ENV` and `$_SERVER` super-globals
Additional assertions for PHPUnit
Provides a framework for testing Psalm plugins
HTTP Client and Server abstraction for PHP.
PHP code formatter
A Zend Framework module to quickly and easily set PHP settings.
统计信息
- 总下载量: 3.39M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: BSD-4-Clause
- 更新时间: 2026-01-04