tobento / service-config
加载和管理PHP应用程序的配置数据。
1.0.4
2023-06-12 14:38 UTC
Requires
- php: >=8.0
- tobento/service-collection: ^1.0
- tobento/service-dir: ^1.0
- tobento/service-filesystem: ^1.0.5
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.0
README
配置服务提供了一种管理应用程序配置数据的方式。
目录
入门
运行此命令添加配置服务的最新版本。
composer require tobento/service-config
要求
- PHP 8.0或更高版本
亮点
- 框架无关,适用于任何项目
- 解耦设计
- 支持翻译
简单示例
以下是如何使用配置服务的一个简单示例
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');
文档
创建配置
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)
设置数据
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');
通过点表示法
您可以使用点表示法设置或添加新数据
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');
加载数据
您可以使用加载器从文件中加载数据。
PHP加载器
PHP加载器,从PHP文件加载数据,返回一个数据数组。
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配置文件
return [ 'host' => 'localhost', 'name' => 'db_name', ];
仅加载数据
您可能省略 key: 以加载不存储的数据。
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加载器
JSON加载器,从JSON文件加载数据。
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');
获取数据
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');
默认值
如果您请求的数据不存在,您可能设置一个默认值来返回,否则会抛出ConfigNotFoundException异常。
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');
关于默认值和数据类型的说明
您可以使用默认值来确保返回正确的数据类型。
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');
包含数据
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)
翻译
您可能想加载数据、设置和获取翻译后的配置数据。
创建配置
您可以根据需要配置翻译。有关更多信息,请访问集合服务 - 翻译
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);
设置数据
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');
加载数据
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' );
获取数据
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"