tobento/service-config
Composer 安装命令:
composer require tobento/service-config
包简介
Loading and managing configuration data for PHP applications.
README 文档
README
The Config Service provides a way for managing configuration data in an application.
Table of Contents
Getting started
Add the latest version of the config service running this command.
composer require tobento/service-config
Requirements
- PHP 8.4 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
- Translation support
Simple Example
Here is a simple example of how to use the config service:
use Tobento\Service\Config\Config; use Tobento\Service\Config\PhpLoader; use Tobento\Service\Collection\Translations; use Tobento\Service\Dir\Dirs; // create config: $config = new Config(new Translations()); // adding a loader: $dirs = new Dirs()->dir('home/private/config'); $config->addLoader(new PhpLoader($dirs)); // loading data from a file: $config->load(file: 'app.php', key: 'app'); // or set data directly: $config->set('database', ['name' => 'db_name']); // Get config data: $appName = $config->get('app.name'); $dbName = $config->get('database.name');
Documentation
Create Config
use Tobento\Service\Config\Config; use Tobento\Service\Config\ConfigInterface; use Tobento\Service\Collection\Translations; $trans = new Translations(); $config = new Config($trans); var_dump($config instanceof ConfigInterface); // bool(true)
Set Data
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $config = new Config(new Translations()); $config->set('database', [ 'host' => 'localhost', 'name' => 'db_name', ]); $config->set('sitename', 'A sitename');
By dot notation
You might set or add new data by using dot notation:
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $config = new Config(new Translations()); $config->set('database', [ 'host' => 'localhost', 'name' => 'db_name', ]); // Add new data: $config->set('database.driver', 'mysql'); // Set data (overwrites existing): $config->set('database.name', 'db_name');
Load Data
You might use loaders to load data from files.
PHP Loader
The php loader, loads data from php files returning an array of data.
use Tobento\Service\Config\Config; use Tobento\Service\Config\PhpLoader; use Tobento\Service\Collection\Translations; use Tobento\Service\Dir\Dirs; // create config: $config = new Config(new Translations()); // add loader: $dirs = new Dirs()->dir('home/private/config'); $config->addLoader(new PhpLoader($dirs)); // loading data: $config->load(file: 'database.php', key: 'database');
database.php config file
return [ 'host' => 'localhost', 'name' => 'db_name', ];
Only loading data
You may omit the key: to load data without storing.
use Tobento\Service\Config\Config; use Tobento\Service\Config\PhpLoader; use Tobento\Service\Config\DataInterface; use Tobento\Service\Collection\Translations; use Tobento\Service\Dir\Dirs; // create config: $config = new Config(new Translations()); // add loader: $dirs = new Dirs()->dir('home/private/config'); $config->addLoader(new PhpLoader($dirs)); // just loading data: $data = $config->load(file: 'database.php'); var_dump($data); // array(3) { ... } // or by the data method: $data = $config->data(file: 'database.php'); var_dump($data instanceof DataInterface); // bool(true)
JSON Loader
The json loader, loads data from json files.
use Tobento\Service\Config\Config; use Tobento\Service\Config\JsonLoader; use Tobento\Service\Collection\Translations; use Tobento\Service\Dir\Dirs; // create config: $config = new Config(new Translations()); // add loader: $dirs = new Dirs()->dir('home/private/config'); $config->addLoader(new JsonLoader($dirs)); // loading data: $config->load(file: 'database.json', key: 'database');
Get Data
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $config = new Config(new Translations()); $config->set('sitename', 'A sitename'); $config->set('app.name', 'An app name'); $sitename = $config->get('sitename'); // using dot notation: $appname = $config->get('app.name');
Default Value
You might set a default value to return if the data requested doesn't exist, otherwise a ConfigNotFoundException is thrown.
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; use Tobento\Service\Config\ConfigNotFoundException; $config = new Config(new Translations()); $sitename = $config->get('sitename', 'Default Sitename'); // would throw ConfigNotFoundException: $sitename = $config->get('sitename');
A note on default value and data type
You can use the default value to ensure the right data type is returned.
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $config = new Config(new Translations()); $sites = $config->get('sites', 'Sites'); // returns the default value: $sites = $config->get('sites', ['first', 'second']); // returns the value set as the same data type: $sites = $config->get('sites', 'Default Sites');
Has Data
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $config = new Config(new Translations()); $config->set('sitename', 'A sitename'); $config->set('app.name', 'An app name'); $config->set('app.name', 'App Name', 'de'); var_dump($config->has('sitename')); // bool(true) // using dot notation: var_dump($config->has('app.name')); // bool(true) // translated: var_dump($config->has('app.name', 'de')); // bool(true)
Translations
You might want to load, set and get translated config data.
Create Config
You might configure the translations based your needs. For more info visit Collection Service - Translations
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $trans = new Translations(); $trans->setLocaleFallbacks(['it' => 'en']); $trans->setLocaleMapping(['en-Us' => 'en']); $config = new Config($trans);
Set Data
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $config = new Config(new Translations()); // default locale: $config->set('sitename', 'Sitename'); // de-CH locale: $config->set('sitename', 'Seitenname', 'de-CH');
Load Data
use Tobento\Service\Config\Config; use Tobento\Service\Config\PhpLoader; use Tobento\Service\Collection\Translations; use Tobento\Service\Dir\Dirs; // create config: $config = new Config(new Translations()); // add loader: $dirs = new Dirs()->dir('home/private/config'); $config->addLoader(new PhpLoader($dirs)); // loading default data: $config->load(file: 'site.php', key: 'site'); // loading de locale data: $config->load( file: 'de/site.php', key: 'site', locale: 'de' );
Get Data
use Tobento\Service\Config\Config; use Tobento\Service\Collection\Translations; $config = new Config(new Translations()); $config->set('sitename', 'Sitename'); $config->set('sitename', 'Seitenname', 'de'); var_dump($config->get('sitename')); // string(8) "Sitename" var_dump($config->get(key: 'sitename', locale: 'de')); // string(10) "Seitenname" // returns default as locale does not exist // and no other fallback is set: var_dump($config->get(key: 'sitename', locale: 'it')); // string(8) "Sitename" // returns default value set as locale does not exist: var_dump($config->get(key: 'sitename', default: 'Site It', locale: 'it')); // string(7) "Site It"
Credits
tobento/service-config 适用场景与选型建议
tobento/service-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 282 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「configuration」 「Settings」 「tobento」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tobento/service-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tobento/service-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tobento/service-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.
统计信息
- 总下载量: 282
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-31