rasuvaeff/yii3-settings 问题修复 & 功能扩展

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

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

rasuvaeff/yii3-settings

Composer 安装命令:

composer require rasuvaeff/yii3-settings

包简介

Typed runtime settings for Yii3 applications

README 文档

README

Stable Version Total Downloads Build Static Analysis PHP License

Typed runtime settings for Yii3: typed getters, multiple providers, cache decorator, encryption contract, inspector.

Using an AI coding assistant? llms.txt has a compact API reference you can give to the LLM to help it work with this package.

Requirements

  • PHP 8.3+
  • psr/simple-cache ^3.0

Installation

composer require rasuvaeff/yii3-settings

Usage

Typed getters

use Rasuvaeff\Yii3Settings\Settings;

$currency = $settings->string(key: 'billing.currency');
$limit = $settings->int(key: 'orders.max_items');
$enabled = $settings->bool(key: 'mail.enabled');
$features = $settings->array(key: 'app.features');

Setting keys and values

use Rasuvaeff\Yii3Settings\SettingKey;
use Rasuvaeff\Yii3Settings\SettingType;
use Rasuvaeff\Yii3Settings\SettingValue;

$key = new SettingKey('billing.currency');
$value = SettingValue::fromRaw(type: SettingType::String, value: 'USD');

Configuration

return [
    'rasuvaeff/yii3-settings' => [
        'definitions' => [
            'billing.currency' => ['type' => 'string', 'default' => 'USD'],
            'orders.max_items' => ['type' => 'int', 'default' => 100],
            'mail.enabled' => ['type' => 'bool', 'default' => true],
        ],
    ],
];

Provider wiring (Yii3 config-plugin)

The core wires only the Settings facade. The SettingsProvider implementation is supplied by exactly one source — a storage backend or, for config-array settings, the application. This keeps backends drop-in (install one and it is wired automatically) with no Duplicate key config conflict.

Setup How SettingsProvider is bound
Database-backed install rasuvaeff/yii3-settings-db — it binds it automatically
Config-only bind it once in your app config (snippet below)

For a config-only setup, bind SettingsProvider to ConfigSettingsProvider in config/common/di/*.php:

use Rasuvaeff\Yii3Settings\ConfigSettingsProvider;
use Rasuvaeff\Yii3Settings\SettingsProvider;

/** @var array $params */

return [
    SettingsProvider::class => [
        'class' => ConfigSettingsProvider::class,
        '__construct()' => [
            'definitions' => $params['rasuvaeff/yii3-settings']['definitions'],
            'values' => $params['rasuvaeff/yii3-settings']['values'],
        ],
    ],
];

Bind SettingsProvider from a single source — a backend plus a manual binding reintroduces the Duplicate key conflict.

Providers

Provider Description
ConfigSettingsProvider Reads from PHP config arrays
EnvSettingsProvider Reads from environment variables
ChainSettingsProvider Chains multiple providers (first match wins)
CachedSettingsProvider PSR-16 cache decorator (write-through since 1.1.0)

Chain providers

$chain = new ChainSettingsProvider(providers: [
    $envProvider,
    $configProvider,
]);

Cache decorator

CachedSettingsProvider is a PSR-16 read cache. Since 1.1.0 it is also write-through: when the inner provider implements WritableSettingsProvider, set()/remove() delegate to it and invalidate the cached entry for the key — so reads never observe a stale value after a write. Bind it as the single WritableSettingsProvider/SettingsProvider and the cache stays coherent automatically.

$cached = new CachedSettingsProvider(
    inner: $writableProvider, // write-through: writes delegate + clear the cache key
    cache: $psr16Cache,
    definitions: $definitions,
    ttl: 60,
    cacheNamespace: 'yii3-settings',
    cacheVersion: 1,
);

Strict mode

Unknown settings return type defaults in non-strict mode. Enable strict mode to throw:

$settings = new Settings(
    provider: $provider,
    definitions: $definitions,
    strictMode: true,
);

Type safety

Calling a getter with a wrong type throws SettingTypeMismatchException:

// Definition: billing.currency = string
$settings->int('billing.currency'); // throws

Public API

Class Description
Settings Facade: string(), int(), float(), bool(), array(), has()
SettingDefinition Typed setting definition: key, type, default, cast, secret flag, and optional presentation/policy metadata (label, group, help, choices, readonly)
SettingKey Validated setting key value object
SettingValue Typed normalized setting value
SettingType Enum: string, int, float, bool, array
SettingsProvider Read-only provider interface
WritableSettingsProvider Read-write provider interface
SettingsInspector Admin-facing read-model: describe() returns SettingState
SettingState Value object: key, value, source, stored override, secret, writable
ConfigSettingsProvider Provider from config arrays
EnvSettingsProvider Provider from environment variables
ChainSettingsProvider Provider chain (first match wins)
CachedSettingsProvider PSR-16 cache decorator (write-through since 1.1.0)
Cipher Encryption interface (AEAD with associated data)
DecryptionException Decryption failure (tampered data)
UnknownEncryptionKeyException Key ID in envelope not found in KeyRing

Secret settings

$def = new SettingDefinition(
    key: 'billing.stripe_key',
    type: SettingType::String,
    secret: true,
);

From config:

'billing.stripe_key' => ['type' => 'string', 'secret' => true],

Presentation & policy metadata

A definition can carry optional UI/policy hints used by admin tooling (e.g. rasuvaeff/yii3-settings-ui). They are inert for the core providers, except readonly, which writable providers reject and describe() reflects via SettingState::isWritable.

$def = new SettingDefinition(
    key: 'orders.status',
    type: SettingType::String,
    default: 'new',
    label: 'Default order status',
    group: 'Orders',
    help: 'Status assigned to freshly created orders',
    choices: ['new', 'paid', 'shipped'],
    readonly: false,
);

From config:

'orders.status' => [
    'type' => 'string',
    'default' => 'new',
    'label' => 'Default order status',
    'group' => 'Orders',
    'help' => 'Status assigned to freshly created orders',
    'choices' => ['new', 'paid', 'shipped'],
    'readonly' => false,
],
Rule Detail
secret=true Only allowed for SettingType::String
secret=false Default — existing definitions unchanged

Encryption contract

use Rasuvaeff\Yii3Settings\Crypto\Cipher;

// Implementations encrypt/decrypt with AAD binding to the setting key
$ciphertext = $cipher->encrypt(plaintext: 'sk_live_xxx', aad: 'billing.stripe_key');
$plaintext = $cipher->decrypt(ciphertext: $ciphertext, aad: 'billing.stripe_key');

SettingsInspector

use Rasuvaeff\Yii3Settings\SettingsInspector;

$state = $inspector->describe(key: 'billing.currency');
$state->key;               // 'billing.currency'
$state->effectiveValue;    // 'USD' (or null for masked secrets)
$state->hasStoredOverride; // true
$state->source;            // 'db', 'config', or 'default'
$state->isSecret;          // false
$state->isWritable;        // true (false for readonly definitions)

$states = $inspector->describeAll(); // list<SettingState> for every declared key

Security

  • Setting keys are validated against /^[a-z][a-z0-9_.-]*$/.
  • Type coercion is centralized in SettingDefinition::cast().
  • Type mismatches throw SettingTypeMismatchException; getters do not return raw untyped values.
  • Cache failures in CachedSettingsProvider are treated as cache misses and do not bypass type checks.
  • Cache keys include namespace and version: yii3-settings:v1:<key>.
  • secret=true is only allowed for SettingType::String — enforced at construction.
  • Secret plaintext must never be logged or included in exception messages (enforced by implementations).

Examples

See examples/ for runnable scripts.

Development

make install && make build

License

BSD-3-Clause. See LICENSE.md.

rasuvaeff/yii3-settings 适用场景与选型建议

rasuvaeff/yii3-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 264 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 264
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 42
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-06-13