chemezov/config
Composer 安装命令:
composer require chemezov/config
包简介
Provides support for Yii2 application runtime configuration
README 文档
README
Use original yii2tech/config. This repo created for fix our shitty CRM bug.
Application Runtime Configuration Extension for Yii 2
This extension provides support for application runtime configuration, loading config from database.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/config
or add
"yii2tech/config": "*"
to the require section of your composer.json.
Usage
This extension allows reconfigure already created Yii application instance using config composed from external storage like relational database, MongoDB and so on. It allows to reconfigure any application property, component or module. Configuration is performed by [[\yii2tech\config\Manager]] component, which should be added to the application configuration. For example:
[
'bootstrap' => [
'configManager',
// ...
],
'components' => [
'configManager' => [
'class' => 'yii2tech\config\Manager',
'items' => [
'appName' => [
'path' => 'name',
'label' => 'Application Name',
'rules' => [
['required']
],
],
'nullDisplay' => [
'path' => 'components.formatter.nullDisplay',
'label' => 'HTML representing not set value',
'rules' => [
['required']
],
],
],
],
...
],
];
[[\yii2tech\config\Manager]] implements [[\yii\base\BootstrapInterface]] interface, thus being placed under 'bootstrap' section it will apply runtime configuration during application bootstrap. You can apply config manually to the application or any [[\yii\base\Module]] descendant, using following code:
$configManager = Yii::$app->get('configManager'); $configManager->configure(Yii::$app);
Configuration items specification
Application parts, which should be reconfigured are determined by [[\yii2tech\config\Manager::$items]], which is a list
of [[\yii2tech\config\Item]]. Each configuration item determines the configuration path - a list of keys in application
configuration array, which leads to the target value. For example: path 'components.formatter.nullDisplay' (or
['components', 'formatter', 'nullDisplay']) points to the property 'nullDisplay' of [[\yii\i18n\Formatter]] component,
path 'name' points to [[\yii\base\Application::name]] and so on.
Note: if no path is specified it will be considered as a key inside [[\yii\base\Module::$params]] array, which matches configuration item id (name of key in [[\yii2tech\config\Manager::$items]] array).
Configuration item may also have several properties, which supports creation of web interface for configuration setup. These are:
- 'label' - string, input label.
- 'description' - string, configuration parameter description or input hint.
- 'rules' - array, value validation rules.
- 'inputOptions' - array, list of any other input options.
Here are some examples of item specifications:
'appName' => [ 'path' => 'name', 'label' => 'Application Name', 'rules' => [ ['required'] ], ], 'nullDisplay' => [ 'path' => 'components.formatter.nullDisplay', 'label' => 'HTML representing not set value', 'rules' => [ ['required'] ], ], 'adminEmail' => [ 'label' => 'Admin email address', 'rules' => [ ['required'], ['email'], ], ], 'adminTheme' => [ 'label' => 'Admin interface theme', 'path' => ['modules', 'admin', 'theme'], 'rules' => [ ['required'], ['in', 'range' => ['classic', 'bootstrap']], ], 'inputOptions' => [ 'type' => 'dropDown', 'items' => [ 'classic' => 'Classic', 'bootstrap' => 'Twitter Bootstrap', ], ], ],
Tip: since runtime configuration may consist of many items and their declaration may cost a lot of code, it can be moved into a separated file and specified by this file name.
Configuration storage
Declared configuration items may be saved into persistent storage and then retrieved from it. The actual item storage is determined via [[\yii2tech\config\Manager::storage]].
Following storages are available:
- [[\yii2tech\config\StoragePhp]] - stores configuration inside PHP files
- [[\yii2tech\config\StorageDb]] - stores configuration inside relational database
- [[\yii2tech\config\StorageMongoDb]] - stores configuration inside MongoDB
- [[\yii2tech\config\StorageActiveRecord]] - finds configuration using ActiveRecord
Please refer to the particular storage class for more details.
Creating configuration web interface
The most common use case for this extension is creating a web interface, which allows control of application configuration in runtime. [[\yii2tech\config\Manager]] serves not only for applying of the configuration - it also helps to create an interface for configuration editing.
The web controller for configuration management may look like following:
use yii\base\Model; use yii\web\Controller; use Yii; class ConfigController extends Controller { /** * Performs batch updated of application configuration records. */ public function actionIndex() { /* @var $configManager \yii2tech\config\Manager */ $configManager = Yii::$app->get('configManager'); $models = $configManager->getItems(); if (Model::loadMultiple($models, Yii::$app->request->post()) && Model::validateMultiple($models)) { $configManager->saveValues(); Yii::$app->session->setFlash('success', 'Configuration updated.'); return $this->refresh(); } return $this->render('index', [ 'models' => $models, ]); } /** * Restores default values for the application configuration. */ public function actionDefault() { /* @var $configManager \yii2tech\config\Manager */ $configManager = Yii::$app->get('configManager'); $configManager->clearValues(); Yii::$app->session->setFlash('success', 'Default values restored.'); return $this->redirect(['index']); } }
The main view file can be following:
<?php use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $models yii2tech\config\Item[] */ ?> <?php $form = ActiveForm::begin(); ?> <?php foreach ($models as $key => $model): ?> <?php $field = $form->field($model, "[{$key}]value"); $inputType = ArrayHelper::remove($model->inputOptions, 'type'); switch($inputType) { case 'checkbox': $field->checkbox(); break; case 'textarea': $field->textarea(); break; case 'dropDown': $field->dropDownList($model->inputOptions['items']); break; } echo $field; ?> <?php endforeach;?> <div class="form-group"> <?= Html::a('Restore defaults', ['default'], ['class' => 'btn btn-danger', 'data-confirm' => 'Are you sure you want to restore default values?']); ?> <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?>
Standalone configuration
[[\yii2tech\config\Manager]] can be used not only for application configuration storage: it may hold any abstract configuration set for any standalone task. You can configure manager as an application component, which stores some user settings as use it to retrieve it. For example:
[
'components' => [
// no application boostap!
'userInterfaceConfig' => [
'class' => 'yii2tech\config\Manager',
'storage' => [
'class' => 'yii2tech\config\StorageDb',
'autoRestoreValues' => true, // restore config values from storage at component initialization
'filter' => function () {
return [
'userId' => Yii::$app->user->id // vary storage records by user
];
},
],
'items' => [
'sidebarEnabled' => [
'value' => true, // default value
'rules' => [
['boolean']
],
],
'backgroundColor' => [
'value' => '#101010', // default value
'rules' => [
['required']
],
],
// ...
],
],
...
],
];
Then you can retrieve any user setting via created component:
```php
if (Yii::$app->userInterfaceConfig->getItemValue('sidebarEnabled')) {
// render sidebar
}
echo Yii::$app->userInterfaceConfig->getItemValue('backgroundColor');
Note that you should enable [[\yii2tech\config\Manager::$autoRestoreValues]] to make configuration values to be restored from persistent storage automatically, otherwise you'll have to invoke [[\yii2tech\config\Manager::restoreValues()]] method manually. Also do not forget to specify default value for each configuration item, otherwise it will be picked up from current application.
You may also use [[\yii2tech\config\Manager]] to configure particular component. For example:
use yii\base\Component; use yii2tech\config\Manager; class SomeComponent extends Component { // fields to be configured: public $isEnabled = true; public $color = '#101010'; // config manager ; private $_configManager; public function getConfigManager() { if ($this->_configManager === null) { $this->_configManager = new Manager([ 'source' => $this, 'storage' => [ 'class' => 'yii2tech\config\StorageDb', 'table' => 'SomeComponentConfig', ], 'items' => [ 'isEnabled' => [ 'path' => 'isEnabled', 'rules' => [ ['boolean'] ], ], 'color' => [ 'path' => 'color', 'rules' => [ ['required'] ], ], // ... ], ]); } return $this->_configManager; } public function init() { parent::init(); $this->getConfigManager()->configure($this); // populate component with config from DB } // ... }
In above example fields isEnabled and color will always be configured from persistent storage.
chemezov/config 适用场景与选型建议
chemezov/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.02k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「configuration」 「config」 「runtime」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chemezov/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chemezov/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chemezov/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Configuration component
Store your language lines in the database, yaml or other sources
A Zend Framework module to quickly and easily set PHP settings.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 16.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-10-15