fantismic / dynamic-settings
此软件包创建由用户控制的自定义动态设置。
Requires
- php: ^8.2
- spatie/laravel-collection-macros: ^7.0
README
Dynamic Settings 是一个 Laravel / Livewire 软件包,允许您动态管理应用程序的自定义设置。
假设您有多个通知要发送到不同的邮件列表,但您希望能够更改是否发送它们,或者将它们发送到哪个电子邮件地址,而无需修改代码或环境文件。
这就是 Dynamic Settings 发挥作用的地方。
您可以创建任意数量的配置,将它们分组并相互关联,然后在代码中使用它们的值。这样,在发送电子邮件时,例如,$to 将会是 DynSettings::get('admin.mail'),而不是 "admin@mysite.com"。
要求
- Laravel 10/11
可选要求
- Livewire 3 (*)
- Wire UI 2 (**)
(*) Livewire 3 仅用于管理设置组件。您始终可以选择忽略它并自己创建。
(**) 如前所述,WireUI 是一个非常适合这些组件的软件包,我们包含了一个普通视图和一个 WireUI 视图,它们会根据您的应用程序自动设置。
安装
composer require fantismic/dynamic-settings
安装后,您需要发布迁移文件...
php artisan vendor:publish --provider="Fantismic\\DynSettings\\Providers\\DynSettingsProvider" --tag="migrations"
...并运行迁移。
php artisan migrate
就这样。您已经准备好了!
可选
您可以将配置文件发布以自定义行为,例如全页组件模式或首选 blade。
php artisan vendor:publish --provider="Fantismic\\DynSettings\\Providers\\DynSettingsProvider" --tag="config"
使用内置组件
组件模式
我们提供了一个用于管理设置的 livewire 组件。您只需将其包含在任何您想要的位置即可。
<livewire:DynamicSettings />
全页模式
您可以在配置文件中将组件设置为以全页模式渲染。如果您这样做,您应该在 routes/web.php 中添加路由。
<?php
use Fantismic\DynSettings\Livewire\DynamicSettingsComponent;
Route::get('/fullpageSettings',DynamicSettingsComponent::class);
用法
我们在外观提供了一个或两个方法。
use Fantismic\DynSettings\Facades\DynSettings;
假设您有这些键
notifications.send
notifications.alwayscc.send
notificactions.alwayscc.emails
获取数据
设置/更新数据
使用设置
获取所有设置
DynSettings::all(): (array)
> DynSettings::all()
= [
[
"id" => 1,
"key" => "notifications.send",
"value" => "true",
"type" => "bool",
"name" => "Send email notifications",
"description" => "Check this item in order to active all notifications.",
"group" => "Notifications",
"associate_with" => "General",
],
[
"id" => 2,
"key" => "notifications.alwayscc.send",
"value" => "true",
"type" => "bool",
"name" => "Send always CC",
"description" => "Add to all emails a carbon copy",
"group" => "Notifications",
"associate_with" => "Always CC",
],
[
"id" => 3,
"key" => "notifications.alwayscc.emails",
"value" => "["test@mail.com","admin@app.com"]",
"type" => "array",
"name" => "CC email list",
"description" => "List of emails that should recibe a copy for every send mail",
"group" => "Notifications",
"associate_with" => "Always CC",
],
]
>
以数组形式获取所有设置
DynSettings::getArray(): (array)
> DynSettings::getArray()
= [
"notifications" => [
"send" => true,
"alwayscc" => [
"send" => true,
"emails" => [
"test@mail.com",
"admin@app.com",
],
],
],
]
以对象形式获取所有设置
DynSettings::getObject(): (object)
> DynSettings::getObject()
= {#1
+"notifications": {#2
+"send": true,
+"alwayscc": {#3
+"send": true,
+"emails": [
"test@mail.com",
"admin@app.com",
],
},
},
}
以点分隔数组形式获取所有设置
DynSettings::getDot(): (array)
> DynSettings::getDot()
= [
"notifications.send" => true,
"notifications.alwayscc.send" => true,
"notifications.alwayscc.emails" => [
"test@mail.com",
"admin@app.com",
],
]
获取设置值
DynSettings::get( (string) $key): (mixed)
> DynSettings::get('notifications.alwayscc.send')
= true
------------------------------------------------------------
> DynSettings::get('notifications.alwayscc.emails')
= [
"test@mail.com",
"admin@app.com",
]
获取设置数据
DynSettings::getKeyData( (string) $key): (array)
> DynSettings::getKeyData('notifications.alwayscc.send')
= {#5459
+"id": 2,
+"key": "notifications.alwayscc.send",
+"value": "true",
+"type": "bool",
+"name": "Send always CC",
+"description": "Add to all emails a carbon copy",
+"group": "Notifications",
+"associate_with": "Always CC",
}
获取设置模型
DynSettings::getModel( (string) $key): (Eloquent Model)
> DynSettings::getModel('notifications.alwayscc.send')
= Fantismic\DynSettings\Models\DynamicSettings {#
id: 2,
key: "notifications.alwayscc.send",
value: "true",
type: "bool",
name: "Send always CC",
description: "Add to all emails a carbon copy",
group: "Notifications",
associate_with: "Always CC",
}
>
获取所有组
DynSettings::getGroups(): (array)
> DynSettings::getGroups()
= [
"Notifications",
]
按组获取关联
DynSettings::getAssocs( (string) $group): (array)
> DynSettings::getAssocs("Notifications")
= [
"General",
"Always CC",
]
按组获取设置
DynSettings::getByGroup( (string) $group): (array)
> DynSettings::getByGroup("Notifications")
= [
[
"id" => 1,
"key" => "notifications.send",
"value" => "true",
"type" => "bool",
"name" => "Send email notifications",
"description" => "Check this item in order to active all notifications.",
"group" => "Notifications",
"associate_with" => "General",
],
[
"id" => 2,
"key" => "notifications.alwayscc.send",
"value" => "false",
"type" => "bool",
"name" => "Send always CC",
"description" => "Add to all emails a carbon copy",
"group" => "Notifications",
"associate_with" => "Always CC",
],
[
"id" => 3,
"key" => "notifications.alwayscc.emails",
"value" => "["test@mail.com","admin@app.com"]",
"type" => "array",
"name" => "CC email list",
"description" => "List of emails that should recibe a copy for every send mail",
"group" => "Notifications",
"associate_with" => "Always CC",
],
]
------------------------------------------------------------
设置/更新数据
设置一个值
DynSettings::set( (string) $group, (mixed) $value): (bool)
> DynSettings::set('notifications.alwayscc.send',false)
= true
更新组名
DynSettings::updateName( (string) $key, (string) $newName): (bool)
> DynSettings::updateName('notifications.alwayscc.send','Always send carbon copy')
= true
更新描述
DynSettings::updateDescription( (string) $key, (string) $newDescription): (bool)
> DynSettings::updateDescription('notifications.alwayscc.send','Set CC on every outgoing mail')
= true
更新关联
DynSettings::updateAssoc( (string) $key, (string) $newAssoc): (bool)
> DynSettings::updateAssoc('notifications.alwayscc.send','CC')
= true
删除设置
DynSettings::delete( (int) $id)**: (bool)
> DynSettings::delete(3)
= true
按键删除设置
通过键删除 DynSettings::deleteByKey( (字符串) $key)**: (布尔值)
> DynSettings::deleteByKey('notifications.alwayscc.send')
= true
更新组名
更新组名称 DynSettings::updateGroupName( (字符串) $oldName, (字符串) $newName): (布尔值)
> DynSettings::updateGroupName("Notifications", "Mailing")
= true
使用设置
比较设置
检查是否存在 DynSettings::is( (字符串) $key, (混合类型) $value): (布尔值)
> DynSettings::is('notifications.alwayscc.send', false)
= true
布尔比较
是否应该 DynSettings::should( (字符串) $key): (布尔值)
> DynSettings::should('notifications.alwayscc.send')
= false
数组搜索
检查是否有 DynSettings::has( (字符串) $key, (数组) $value, [(布尔值) 严格 = false]): (布尔值)
> DynSettings::has('notifications.alwayscc.email', 'admin@app.com')
= true
> DynSettings::has('notifications.alwayscc.email', 'admin@APP.com')
= false
> DynSettings::has('notifications.alwayscc.email', 'admin@APP.com', false)
= true
> DynSettings::has('notifications.alwayscc.email', 'not-in-list-mail@app.com')
= false