nabeghe/configurator
Composer 安装命令:
composer require nabeghe/configurator
包简介
A PHP configuration management library that supports file-based storage and loads each section lazily.
README 文档
README
A PHP configuration management library that supports file-based storage and loads each section lazily.
Features
- Section-Based Configuration: Organize your config into named sections for better structure and readability.
- ArrayAccess & Magic Methods: Access configuration sections and keys using array syntax or object properties.
- Lazy Loading of Sections: Configuration sections are loaded only when accessed, reducing memory usage and improving performance.
- Type-Safe Access: Supports IDE autocomplete and type hinting via Proxy classes and PHPDoc annotations.
- Dot Notation Support: Easily get or set nested configuration values with intuitive dot paths (e.g.,
database.connections.mysql.host). - Defaults Handling: Define default values for keys, ensuring fallback values when a key is missing.
- File-Based Persistence: Save and load configuration sections to individual PHP files.
- Dynamic Proxy System: Each configuration section is accessible via a Proxy class, providing seamless interaction and encapsulation.
- Caching: Optional APCu or file-based caching to improve performance by storing available file metadata.
- Utility Methods: Easily manage keys, delete sections, iterate over values, or eject sections from memory.
Installation
Install via Composer:
composer require nabeghe/configurator
Usage
- Create a configuration class and extend it from
Configurator. - Define the
DEFAULTSconstant in this class. This constant is an array that can contain arbitrary values. Each key represents a section, and its value is an array of that section’s configuration values (for example:db,log, etc.). - It is recommended to create a separate class for each section. This class is used only for IDE type hinting and has no other functional purpose. In this class, you can define the keys that the section may contain, along with their types, using
@propertyannotations in the docblock comments. This class must extend theProxyclass, because each section is aProxy. - Instantiate your configuration class.
Configurator Constructor
The constructor accepts three parameters:
- First parameter: The path to a directory where each section can be stored as a separate PHP file. Each file must return an array. For example, a section named
db.phpcan be used for database configuration. The default value isnull, meaning no directory is used. - Second parameter: Configuration data. You may inject configuration data for each section at instantiation time. In this case, the data will not be loaded from the directory. The default value is an empty array.
- Third parameter: Specifies whether APCu should be used. It is used only to cache the list of available files and has no other purpose. If not set, a file named
_.cachewill be used in the specified directory instead.
Note 1:
By default, the directory path is null. If it is not set, loading configuration from files and persisting configurations is not possible. Therefore, if your configuration is file-based, you should set this path.
Note 2:
Injecting data together with a directory path is allowed. In this case, the injected sections will no longer be loaded from their corresponding files.
use Nabeghe\Configurator\Configurator; use Nabeghe\Configurator\Proxy; class MyConfig extends Configurator { const DEFAULTS = [ 'db' => [ 'type' => 'mysql', 'host' => 'localhost', 'port' => 3306, ], 'debug' => [ 'enabled' => true, ], ]; } $config = new MyConfig(path: __DIR__ . '/config'); // Accessing sections via Proxy $db = $config->db; echo $db->host; // localhost // Using dot notation $config->dot('db.host', '127.0.0.1'); echo $config->dot('db.host'); // 127.0.0.1 // Adding new keys or editing $config->app->version = '1.0.0'; // Save a section $config->db->save(); // Delete a key $config->db->del('port'); // Iterate through keys $config->database->each(function($key, $value) { echo "$key => $value\n"; }); // Get all configuration data including defaults $allConfig = $config->getAll(addDefaults: true);
Cache file
A file named _.cache is reserved. If APCu is not available, the list of existing configuration files is stored in this file. Although the system automatically updates the list, it is recommended to delete the _.cache file whenever you add a new file or remove an existing one.
Configurator class
| Method | Description |
|---|---|
| __construct($path, $config, $apcuAllowed) | Initialize configurator with path, initial config and cache option |
| __get($name) | Get a Proxy instance for a config section |
| __set($name, $value) | Set or create a config section (via Proxy) |
| getPath() | Get configuration directory path |
| setPath($value) | Set configuration directory path |
| isLoadable() | Check if file loading/saving is enabled |
| hasDefault($section, $key) | Check if a default value exists |
| getDefault($section, $key) | Get default value for a key |
| getDefaults($section) | Get all default values of a section |
| has($section, $key) | Check if a key exists in a section |
| get($section, $key) | Get a config value (falls back to default) |
| set($section, $key, $value) | Set a config value |
| setOnce($section, $key, $value) | Set a value only if it does not exist |
| dot($path, $value = null) | Get/set config using dot-notation |
| del($section, $key) | Delete a key from a section |
| getAll($section = null, $addDefaults = false) | Get all config data |
| setAll($sectionOrArray, $config = []) | Replace section or full config |
| getKeys($section, $addDefaults = false) | Get keys of a section |
| isEmpty($section = null) | Check if section or config is empty |
| generatePath($section) | Generate file path for a section |
| load($proxyOrName) | Load section config from file |
| save($proxyOrName = null) | Save section(s) to file |
| clear($proxyOrName) | Clear config values of a section |
| delete($proxyOrName) | Delete config file of a section |
| eject($proxyOrName) | Remove section from memory |
| ejectAll($excepts = []) | Remove all sections except given ones |
| each($section, $callback, $addDefaults = false) | Iterate over section values |
| mkdirs($path, $permissions) | Recursively create directories |
| getCacheName() | Get cache identifier name |
| hasCache() | Check if cache exists |
| getCache() | Retrieve cached metadata |
| updateCache() | Update cache with file metadata |
| delCache() | Delete cache entry |
| offsetExists($offset) | ArrayAccess: check section exists |
| offsetGet($offset) | ArrayAccess: get section Proxy |
| offsetSet($offset, $value) | ArrayAccess: set section |
| offsetUnset($offset) | ArrayAccess: remove section |
Proxy class
This class is used for each configuration section.
| Method | Description |
|---|---|
| __construct($configurator, $name) | Create a proxy for a configuration section |
| offsetExists($key) | ArrayAccess: check if a key exists |
| offsetGet($key) | ArrayAccess: get a value by key |
| offsetSet($key, $value) | ArrayAccess: set a value by key |
| offsetUnset($key) | ArrayAccess: remove a key |
| __get($key) | Get a config value via property access |
| __set($key, $value) | Set a config value via property access |
| has($key) | Check if a key exists in the section |
| getName() | Get section name |
| getDefaults() | Get default values of the section |
| getKeys() | Get all keys in the section |
| isEmpty() | Check if section is empty |
| getAll($addDefaults = false) | Get all config values |
| setAll($config) | Replace all section values |
| hasDefault($key) | Check if a default exists for key |
| getDefault($key) | Get default value for key |
| load() | Load section config from file |
| save() | Save section config to file |
| clear() | Clear all values in section |
| delete() | Delete section config file |
| eject() | Remove section from memory |
| each($callback, $addDefaults = false) | Iterate over key-value pairs |
📖 License
Licensed under the MIT license, see LICENSE.md for details.
nabeghe/configurator 适用场景与选型建议
nabeghe/configurator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「library」 「config」 「configurator」 「helper」 「Settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nabeghe/configurator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nabeghe/configurator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nabeghe/configurator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
A Zend Framework module to quickly and easily set PHP settings.
Inbox pattern process implementation for your Laravel Applications
Utils to load, parse and work with configuration on Mezzio projects
Server environment detection based on config array-file
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-16