salesrender/plugin-component-settings
Composer 安装命令:
composer require salesrender/plugin-component-settings
包简介
SalesRender plugin settings model
README 文档
README
Settings model for the SalesRender plugin ecosystem. Provides per-plugin-instance settings storage based on plugin-component-db and plugin-component-form. Each plugin instance has exactly one Settings record (singleton per plugin) that holds serialized form data (FormData). Includes an integrity guard to verify that settings are properly filled before allowing critical operations.
Installation
composer require salesrender/plugin-component-settings
Requirements
| Requirement | Version |
|---|---|
| PHP | >= 7.4.0 |
| ext-json | * |
| salesrender/plugin-component-db | ^0.3.0 |
| salesrender/plugin-component-form | ^0.10.0 | ^0.11.0 |
Key classes
Settings
The main model class. Declared as final -- it is not intended for subclassing.
Namespace: SalesRender\Plugin\Components\Settings
Extends: SalesRender\Plugin\Components\Db\Model
Implements: SalesRender\Plugin\Components\Db\SinglePluginModelInterface
| Method | Signature | Description |
|---|---|---|
getData |
getData(): FormData |
Returns the stored FormData instance (dot-notation accessor for form field values) |
setData |
setData(FormData $data): void |
Replaces the stored form data |
find |
static find(): Settings |
Loads the settings for the current plugin instance (by Connector::getReference()->getId()). Returns a new empty Settings if none exist |
getForm |
static getForm(array $context = []): Form |
Returns the configured settings Form. Throws RuntimeException if setForm() was not called |
setForm |
static setForm(Form|callable $form): void |
Configures the settings form. Accepts a Form instance or a callable that returns one (lazy loading) |
guardIntegrity |
static guardIntegrity(): void |
Validates current settings data against the form. Throws IntegritySettingsException (HTTP 424) if data is empty or incomplete |
schema |
static schema(): array |
Database schema: single data column of type TEXT |
Inherited from Model: save(), delete(), getId(), findById(), findByCondition(), addOnSaveHandler(), removeOnSaveHandler(), tableName(), freeUpMemory().
IntegritySettingsException
Thrown by guardIntegrity() when settings validation fails.
Namespace: SalesRender\Plugin\Components\Settings\Exceptions
Extends: \Exception
- HTTP status code: 424 (Failed Dependency)
- Default message:
"Settings data empty or incomplete"
FormData
Provided by salesrender/plugin-component-form. Extends Adbar\Dot -- a dot-notation array accessor. Used to store and retrieve individual setting values.
Namespace: SalesRender\Plugin\Components\Form
Key methods (from Adbar\Dot):
get(string $key, $default = null)-- get a value by dot-notation keyset(string $key, $value)-- set a valuehas(string $key): bool-- check if a key existstoArray(): array-- convert to plain array
Usage
1. Configure the settings form in bootstrap.php
Every plugin must register a settings form during bootstrap. The form defines the fields that appear on the plugin settings page.
From plugin-logistic-cdek:
use SalesRender\Plugin\Components\Settings\Settings; use SalesRender\Plugin\Instance\Logistic\Settings\SettingsForm; // Using a callable for lazy loading (recommended) Settings::setForm(fn() => new SettingsForm());
From plugin-pbx-example:
use SalesRender\Plugin\Components\Settings\Settings; use SalesRender\Plugin\Instance\Pbx\Forms\SettingsForm; Settings::setForm(fn() => new SettingsForm());
2. Define a SettingsForm class
The form class extends Form from plugin-component-form and defines the field groups and their validation.
From plugin-logistic-example:
use SalesRender\Plugin\Components\Form\FieldDefinitions\PasswordDefinition; use SalesRender\Plugin\Components\Form\FieldDefinitions\StringDefinition; use SalesRender\Plugin\Components\Form\FieldGroup; use SalesRender\Plugin\Components\Form\Form; use SalesRender\Plugin\Components\Translations\Translator; class SettingsForm extends Form { public function __construct() { parent::__construct( Translator::get('settings', 'Settings'), null, [ 'main' => new FieldGroup( Translator::get('settings', 'Main settings'), null, [ 'login' => new StringDefinition( Translator::get('settings', 'Login'), null, function () { return []; } ), 'password' => new PasswordDefinition( Translator::get('settings', 'Password'), null, function () { return []; } ), ] ), ], Translator::get('settings', 'Save'), ); } }
3. Read settings values in plugin code
The most common pattern is Settings::find()->getData(), then access individual values with dot notation.
From plugin-logistic-cdek:
use SalesRender\Plugin\Components\Settings\Settings; $settings = Settings::find()->getData(); $login = $settings->get('main.login'); $password = $settings->get('main.password');
From plugin-chat-eskiz:
use SalesRender\Plugin\Components\Settings\Settings; $settings = Settings::find()->getData(); $token = $settings->get('main.token');
4. Guard integrity before critical operations
Use guardIntegrity() to ensure settings are valid before performing operations that depend on them. If settings are not filled, an IntegritySettingsException is thrown with HTTP code 424.
From plugin-macros-example:
use SalesRender\Plugin\Components\Settings\Settings; class ExampleHandler implements BatchHandlerInterface { public function __invoke(Process $process, Batch $batch) { Settings::guardIntegrity(); $fields = Settings::find()->getData()->get('group_1.fields'); // proceed with processing... } }
From plugin-logistic-example (inside form value provider):
use SalesRender\Plugin\Components\Settings\Settings; $values = new CallableValues(function () { Settings::guardIntegrity(); $data = Settings::find()->getData(); $senders = []; for ($i = 1; $i <= 3; $i++) { if ($data->get("sender_{$i}.use", false)) { $senders[$i] = [ 'title' => $data->get("sender_{$i}.name"), 'group' => 'Sender', ]; } } return $senders; });
5. Register save handlers
Use addOnSaveHandler() (inherited from Model) to execute logic whenever settings are saved. This is useful for triggering side effects like syncing configuration to external services.
From plugin-pbx-example:
use SalesRender\Plugin\Components\Settings\Settings; Settings::addOnSaveHandler(function (Settings $settings) { $builder = new ConfigBuilder($settings); $sender = new ConfigSender($builder); $sender(); });
From plugin-core-logistic (named handler):
use SalesRender\Plugin\Components\Settings\Settings; Settings::addOnSaveHandler(function (Settings $settings) { if (LogisticHelper::isFulfillment()) { $handler = FulfillmentContainer::getBindingHandler(); $handler($settings)->sync(); } }, 'ffSync');
How it works with plugin-core
The plugin-core framework provides built-in HTTP routes for settings management via SettingsHandler:
| Route | Method | Description |
|---|---|---|
/forms/settings |
GET | Returns the settings form definition (calls Settings::getForm()) |
/data/settings |
GET | Returns the current settings data (calls Settings::find()->getData()) |
/data/settings |
PUT | Saves new settings data (validates against the form, then calls setData() + save()) |
These routes are registered automatically by the core framework. Plugin developers only need to configure the form via Settings::setForm() in bootstrap.php.
API reference
Database schema
The Settings table has a single data column. The id, companyId, pluginAlias, and pluginId columns are managed automatically by Model and SinglePluginModelInterface.
| Column | Type | Description |
|---|---|---|
id |
(auto) | Plugin instance ID (= Connector::getReference()->getId()) |
companyId |
(auto) | Company ID from plugin reference |
pluginAlias |
(auto) | Plugin alias from plugin reference |
pluginId |
(auto) | Plugin ID from plugin reference |
data |
TEXT |
JSON-serialized FormData |
Data serialization
- On write (
beforeWrite):FormDatais serialized to JSON viajson_encode() - On read (
afterRead): JSON is deserialized back toFormDatavianew FormData(json_decode($data, true))
Singleton behavior
Settings implements SinglePluginModelInterface. This means:
- There is exactly one
Settingsrecord per plugin instance (identified bypluginId) Settings::find()returns the record for the current plugin context, or a new emptySettingsif none exists- The
idfield is automatically set toConnector::getReference()->getId()
Dependencies
| Package | Purpose |
|---|---|
| salesrender/plugin-component-db | Database ORM: Model, SinglePluginModelInterface, Connector |
| salesrender/plugin-component-form | Form, FormData (dot-notation data container based on adbario/php-dot-notation) |
See also
- salesrender/plugin-component-db -- database layer (Model, Connector, SinglePluginModelInterface)
- salesrender/plugin-component-form -- form definitions and FormData
- salesrender/plugin-core -- core framework with SettingsHandler routes
salesrender/plugin-component-settings 适用场景与选型建议
salesrender/plugin-component-settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.1k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 salesrender/plugin-component-settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 salesrender/plugin-component-settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2023-11-09