borschphp/config
Composer 安装命令:
composer require borschphp/config
包简介
A minimal configuration manager.
README 文档
README
A lightweight and easy to use configuration library.
Features
- Simple and intuitive API
- Support for multiple file formats (JSON, YAML, INI, DOTENV)
- Aggregate multiple configuration sources
- Caching for improved performance in production
Installation
Via composer :
composer require borschphp/config
Usage
The Config class implements the ContainerInterface from PSR-11, therefore you have access to all its methods to
check if a key exists and to retrieve its value.
A getOrDefault() method is also available to provide a default value if the key does not exist.
Internally, readers are used to parse configuration files of different formats. The following readers are available:
Borsch\Config\Reader\Jsonfor JSON filesBorsch\Config\Reader\Yamlfor YAML files (viasymfony/yaml)Borsch\Config\Reader\Inifor INI filesBorsch\Config\Reader\DotEnvfor DOTENV files (viavlucas/phpdotenv)
The Aggregator class allows you to merge multiple configuration sources into a single Config instance. You can also
provide a cache file path to store the merged configuration for faster access in production environments.
A simple example with just one reader :
<?php require_once __DIR__ . '/vendor/autoload.php'; use Borsch\Config\Config; use Borsch\Config\Reader\DotEnv; $env = new DotEnv(); $env_data = $env->fromFile(__DIR__ . '/.env'); $config = new Config($env_data); $key = $config->has('key') ? $config->get('key') : 'other_value'; $other = $config->getOrDefault('other', 'default_value');
A more complete example with multiple readers :
<?php require_once __DIR__ . '/vendor/autoload.php'; use Borsch\Config\Aggregator; use Borsch\Config\Config; use Borsch\Config\Reader\DotEnv; use Borsch\Config\Reader\Ini; use Borsch\Config\Reader\Json; use Borsch\Config\Reader\Yaml; $ini = new Ini(); $ini_data = $ini->fromFile(__DIR__ . '/config.ini'); $env = new DotEnv(); $env_data = $env->fromFile(__DIR__ . '/.env'); $json = new Json(); $json_data = $json->fromFile(__DIR__ . '/config.json'); $yaml = new Yaml(); $yaml_data = $yaml->fromFile(__DIR__ . '/config.yaml'); $aggregator = new Aggregator( [ $ini_data, $env_data, $json_data, $yaml_data, [ 'key' => 'value' ] ] ); /** @var Config $config */ $config = $aggregator->getMergedConfig(); $key = $config->has('key') ? $config->get('key') : 'other_value'; $other = $config->getOrDefault('other', 'default_value');
Providers
Providers allow you to specify file paths, internally it uses the glob() function to find matching files so you can use
glob() patterns.
Because the providers are callable, configuration loading can be deferred until actually needed, this is the recommended
way to build your configuration in order to avoid unnecessary file parsing in production.
There is no interface for providers, they just need to be classes implementing the __invoke() method and returning an
array.
Therefore, it is possible to only provide the provider FQDN if there is no constructor arguments. Example :
$aggregator = new Aggregator( [ Application\Config\MyConfigProvider::class ] );
A complete example with providers and caching enabled :
<?php require_once __DIR__ . '/vendor/autoload.php'; use Borsch\Config\Aggregator; use Borsch\Config\Config; use Borsch\Config\Provider\DotEnvProvider; use Borsch\Config\Provider\IniProvider; use Borsch\Config\Provider\JsonProvider; use Borsch\Config\Provider\PhpFileProvider; use Borsch\Config\Provider\YamlProvider; $aggregator = new Aggregator( [ new PhpFileProvider('config/production.*.config.php'), new JsonProvider('config/production.*.config.json'), new YamlProvider('config/production.*.config.y{a,}ml'), new IniProvider('config/production.*.config.ini'), new DotEnvProvider('config/.env.production.*'), ], cache_file: __DIR__.'/storage/cache/config.cache.php', use_cache: true ); /** @var Config $config */ $config = $aggregator->getMergedConfig(); $key = $config->has('key') ? $config->get('key') : 'other_value'; $other = $config->getOrDefault('other', 'default_value');
Binding Configuration to Objects
The bind() method allows you to map a configuration array directly into a typed object. It uses
cuyz/valinor under the hood, which means it supports constructor injection,
type casting, and nested objects out of the box.
<?php require_once __DIR__ . '/vendor/autoload.php'; use Borsch\Config\Aggregator; use Borsch\Config\Config; use Borsch\Config\Provider\IniProvider; class DatabaseConfiguration { private string $username; private string $password; private string $database; public function getUsername(): string { return $this->username; } public function setUsername(string $username): void { $this->username = $username; } public function getPassword(): string { return $this->password; } public function setPassword(string $password): void { $this->password = $password; } public function getDatabase(): string { return $this->database; } public function setDatabase(string $database): void { $this->database = $database; } } $aggregator = new Aggregator([ new IniProvider('config/config.ini'), ]); /** @var Config $config */ $config = $aggregator->getMergedConfig(); /** @var DatabaseConfiguration $db */ $dbConfig = $config->bind('database', DatabaseConfiguration::class); echo $dbConfig->getUsername();
The first argument is the configuration key to read from, and the second is the fully qualified class name to map the values into. The keys in the configuration array must match the property names of the target class.
License
The package is licensed under the MIT license. See License File for more information.
borschphp/config 适用场景与选型建议
borschphp/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「config」 「Settings」 「borschphp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 borschphp/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 borschphp/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 borschphp/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings package for Laravel framework.
Configuration component
A Zend Framework module to quickly and easily set PHP settings.
Settings Card for Laravel Nova.
Utils to load, parse and work with configuration on Mezzio projects
Extends basic Wordpress features.
统计信息
- 总下载量: 73
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-03