lav45/yii2-settings 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lav45/yii2-settings

Composer 安装命令:

composer require lav45/yii2-settings

包简介

This extension helps you to easily store and retrieve data for your application.

README 文档

README

Latest Stable Version License Total Downloads Test Status Code Coverage Scrutinizer Code Quality

This extension is very useful for storing any settings, for your application.

Installation

The preferred way to install this extension through composer.

You can set the console

~$ composer require lav45/yii2-settings

in require section in composer.json file.

Migrate

Apply with the console command:

~$ yii migrate/up --migrationPath=vendor/lav45/yii2-settings/migrations

or add it to your console config file ( console/config/main.php )

return [
    'controllerMap' => [
        'migrate' => [
            'class' => yii\console\controllers\MigrateController::class,
            'migrationPath' => [
                '@app/migrations',
                '@vendor/lav45/yii2-settings/migrations',
            ],
        ],
    ],
];

Component Setup

To use the Setting Component, you need to configure the components array in your application configuration:

return [
    'components' => [
        'settings' => [
            'class' => 'lav45\settings\Settings',

            // You can add an arbitrary prefix to all keys
            // 'keyPrefix' => 'key_',

            // DbStorage use as default storage
            // 'storage' => [
            //    'class' => 'lav45\settings\storage\DbStorage',
            //    'tableName' => '{{%settings}}',
            //    'db' => 'db',
            // ],

            'as cache' => [
                'class' => 'lav45\settings\behaviors\CacheBehavior',
                // 'cache' => 'cache',
                // 'cacheDuration' => 3600,
            ],
            'as access' => [
                'class' => 'lav45\settings\behaviors\QuickAccessBehavior',
            ],
            'as context' => [
                'class' => 'lav45\settings\behaviors\ContextBehavior',
            ],
        ],

        /**
         * FileStorage this is the adapter allows you to store your settings in a simple file
         */
        'configFile' => [
            'class' => 'lav45\settings\Settings',

            // Be used for data serialization
            'serializer' => ['serialize', 'unserialize'],

            'storage' => [
                'class' => 'lav45\settings\storage\FileStorage',
                // it is desirable to determine the storage location 
                // of your configuration files in a convenient place
                // 'path' => '@runtime/settings',
                // 'dirMode' => 0755,
                // 'fileSuffix' => '.bin',
            ],
        ],

        /**
         * PhpFileStorage this is an adapter to store data in php file
         * the serializer can be disabled to increase performance
         */
        'configPhpFile' => [
            'class' => 'lav45\settings\Settings',
            'serializer' => false,
            'storage' => [
                'class' => 'lav45\settings\storage\PhpFileStorage',
                // 'path' => '@runtime/settings',
                // 'fileSuffix' => '.php',
            ],
        ],
    ]
];

Using

Can default

$settings = Yii::$app->settings;

// Get not exist key
$settings->get('key'); // => null

// Get default value if key exist
$settings->get('key', []); // => []

// Get default value from the callback function
$settings->get('enable', function () {
    return true;
}); // => true

// Save and get data
$settings->set('array', ['data']); // => true
$settings->get('array'); // => [0 => 'data']

$settings->set('object', new User()); // => true
$settings->get('object'); // => User

$settings->set('float', 123.5); // => true
$settings->get('float'); // => 123.5

$settings->set('integer', 0); // => true
$settings->get('integer'); // => 0

$settings->set('bool', false); // => true
$settings->get('bool'); // => false

$settings->set('string', 'text'); // => true
$settings->get('string'); // => text

$settings->set('null', null); // => true
$settings->get('null'); // => null

// delete data by key
$settings->delete('array'); // => true

// Use as array
$settings['array'] = ['data'];

print_r($settings['array']); // => [0 => 'data']

isset($settings['array']) // => true

unset($settings['array']);

CacheBehavior

The extension, which will help to speed up data loading by caching. If the data changes, the cache will be updated automatically.

To clean the cache, you can use this method

Yii::$app->settings->cache->flush(); // => true

QuickAccessBehavior

This extension allows you to quickly obtain the necessary data from a multidimensional array

// Getting data from multidimensional array
$data = [
    'options' => [
        'css' => ['bootstrap.css'],
        'js' => ['jquery', 'bootstrap.js']
    ]
];
$settings = Yii::$app->settings;

// Save data
$settings->set('array', $data); // => true

// Get data by key
$settings->get('array.options.js'); // => ['jquery', 'bootstrap.js']

// Use as array
print_r($settings['array.options.js']); // => ['jquery', 'bootstrap.js']
print_r($settings['array']['options']['js']); // => ['jquery', 'bootstrap.js']

// Get not exist key
$settings->get('array.options.img'); // => null

// Get default value if key exist
$settings->get('array.options.imgs', []); // => []

// Replace value by path key 
$settings->replace('array', 'options.js', ['jquery']);
$settings->replace('array', 'options.img', ['icon.jpg', 'icon.png']);
$settings->get('array.options.js'); // => ['jquery']
$settings->get('array');
/**
 * [
 *     'options' => [
 *         'css' => ['bootstrap.css'],
 *         'js' => ['jquery'],                  // rewrite
 *         'img' => ['icon.jpg', 'icon.png'],   // added
 *     ]
 * ]
 */

ContextBehavior

This extension allows to retrieve data depending on the context. For example, depending on the selected language.

$settings = Yii::$app->settings;

$settings->context('en-US')->set('key', ['data']); // => true

$settings->context('en-US')->get('key'); // => ['data']

$settings->context('ru-RU')->get('key'); // => null

Practical use: you can see in

License

yii2-settings it is available under a BSD 3-Clause License. Detailed information can be found in the LICENSE.md.

lav45/yii2-settings 适用场景与选型建议

lav45/yii2-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.18k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2016 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「config」 「extension」 「Settings」 「yii2」 「quick access」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lav45/yii2-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lav45/yii2-settings 我们能提供哪些服务?
定制开发 / 二次开发

基于 lav45/yii2-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 11
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 6
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2016-09-20