league/config
Composer 安装命令:
composer require league/config
包简介
Define configuration arrays with strict schemas and access values with dot notation
README 文档
README
league/config helps you define nested configuration arrays with strict schemas and access configuration values with dot notation. It was created by Colin O'Dell.
📦 Installation
This project requires PHP 7.4 or higher. To install it via Composer simply run:
composer require league/config
🧰️ Basic Usage
The Configuration class provides everything you need to define the configuration structure and fetch values:
use League\Config\Configuration; use Nette\Schema\Expect; // Define your configuration schema $config = new Configuration([ 'database' => Expect::structure([ 'driver' => Expect::anyOf('mysql', 'postgresql', 'sqlite')->required(), 'host' => Expect::string()->default('localhost'), 'port' => Expect::int()->min(1)->max(65535), 'ssl' => Expect::bool(), 'database' => Expect::string()->required(), 'username' => Expect::string()->required(), 'password' => Expect::string()->nullable(), ]), 'logging' => Expect::structure([ 'enabled' => Expect::bool()->default($_ENV['DEBUG'] == true), 'file' => Expect::string()->deprecated("use logging.path instead"), 'path' => Expect::string()->assert(function ($path) { return \is_writeable($path); })->required(), ]), ]); // Set the values, either all at once with `merge()`: $config->merge([ 'database' => [ 'driver' => 'mysql', 'port' => 3306, 'database' => 'mydb', 'username' => 'user', 'password' => 'secret', ], ]); // Or one-at-a-time with `set()`: $config->set('logging.path', '/var/log/myapp.log'); // You can now retrieve those values with `get()`. // Validation and defaults will be applied for you automatically $config->get('database'); // Fetches the entire "database" section as an array $config->get('database.driver'); // Fetch a specific nested value with dot notation $config->get('database/driver'); // Fetch a specific nested value with slash notation $config->get('database.host'); // Returns the default value "localhost" $config->get('logging.path'); // Guaranteed to be writeable thanks to the assertion in the schema // If validation fails an `InvalidConfigurationException` will be thrown: $config->set('database.driver', 'mongodb'); $config->get('database.driver'); // InvalidConfigurationException // Attempting to fetch a non-existent key will result in an `InvalidConfigurationException` $config->get('foo.bar'); // You could avoid this by checking whether that item exists: $config->exists('foo.bar'); // Returns `false`
📓 Documentation
Full documentation can be found at config.thephpleague.com.
💭 Philosophy
This library aims to provide a simple yet opinionated approach to configuration with the following goals:
- The configuration should operate on arrays with nested values which are easily accessible
- The configuration structure should be defined with strict schemas defining the overall structure, allowed types, and allowed values
- Schemas should be defined using a simple, fluent interface
- You should be able to add and combine schemas but never modify existing ones
- Both the configuration values and the schema should be defined and managed with PHP code
- Schemas should be immutable; they should never change once they are set
- Configuration values should never define or influence the schemas
As a result, this library will likely never support features like:
- Loading and/or exporting configuration values or schemas using YAML, XML, or other files
- Parsing configuration values from a command line or other user interface
- Dynamically changing the schema, allowed values, or default values based on other configuration values
If you need that functionality you should check out other libraries like:
- symfony/config
- symfony/options-resolver
- hassankhan/config
- consolidation/config
- laminas/laminas-config
🏷️ Versioning
SemVer is followed closely. Minor and patch releases should not introduce breaking changes to the codebase.
Any classes or methods marked @internal are not intended for use outside this library and are subject to breaking changes at any time, so please avoid using them.
🛠️ Maintenance & Support
When a new minor version (e.g. 1.0 -> 1.1) is released, the previous one (1.0) will continue to receive security and critical bug fixes for at least 3 months.
When a new major version is released (e.g. 1.1 -> 2.0), the previous one (1.1) will receive critical bug fixes for at least 3 months and security updates for 6 months after that new release comes out.
(This policy may change in the future and exceptions may be made on a case-by-case basis.)
👷️ Contributing
Contributions to this library are welcome! We only ask that you adhere to our contributor guidelines and avoid making changes that conflict with our Philosophy above.
🧪 Testing
composer test
📄 License
league/config is licensed under the BSD-3 license. See the LICENSE.md file for more details.
🗺️ Who Uses It?
This project is used by league/commonmark.
league/config 适用场景与选型建议
league/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 328.02M 次下载、GitHub Stars 达 565, 最近一次更新时间为 2021 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「schema」 「config」 「array」 「dot」 「nested」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 league/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 league/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 league/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
Extension for Opis JSON Schema
A Zend Framework module to quickly and easily set PHP settings.
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
Build forms from schema
统计信息
- 总下载量: 328.02M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 574
- 点击次数: 15
- 依赖项目数: 24
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2021-05-30