MichaelNabil230 / laravel-setting
该包允许您为Laravel项目持久化设置。
v2.0.0
2023-02-22 08:05 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.2
- nunomaduro/collision: ^6.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.6
README
安装
您可以通过composer安装此包
composer require michaelnabil230/laravel-setting
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="setting-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="setting-config"
这是已发布配置文件的内容
use MichaelNabil230\Setting\Models\Setting; return [ /* |-------------------------------------------------------------------------- | Default Settings Store |-------------------------------------------------------------------------- | | This option controls the default settings store that gets used while | using this settings library. | | Supported: "json", "database", "redis" | */ 'default' => 'json', /* |-------------------------------------------------------------------------- | Drivers Stores |-------------------------------------------------------------------------- | | The settings are stored. | */ 'drivers' => [ 'database' => [ 'driver' => \MichaelNabil230\Setting\Stores\DatabaseSettingStore::class, 'options' => [ 'model' => Setting::class, 'table' => 'settings', // name of table in dataBase 'cache' => [ 'enableCache' => false, 'cacheTtl' => 15, // TTL in seconds. ] ], ], 'redis' => [ 'driver' => \MichaelNabil230\Setting\Stores\RedisSettingStore::class, 'options' => [ 'connection' => 'default', 'prefix' => 'setting', ], ], 'json' => [ 'driver' => \MichaelNabil230\Setting\Stores\JsonSettingStore::class, 'options' => [ 'path' => storage_path('settings.json'), ] ], ], /* |-------------------------------------------------------------------------- | Keys |-------------------------------------------------------------------------- | | Your keys are used to insert settings data. | */ 'keys' => [ // ], /* |-------------------------------------------------------------------------- | Default Settings |-------------------------------------------------------------------------- | | Default settings are used when a setting is not found in the store. | */ 'defaults' => [ // ], ];
您可以通过其外观访问设置存储
<?php use MichaelNabil230\Setting\Facades\Setting; Setting::set('foo', 'bar')->save(); Setting::get('foo', 'default value'); Setting::get('nested.element'); Setting::has('foo'); Setting::flip('foo'); Setting::enable('foo'); Setting::disable('foo'); Setting::forget('foo'); Setting::forgetAll(); $settings = Setting::all(); ?>
您还可以使用setting()
辅助函数
// Get the store instance setting(); // Get values setting('foo'); setting('foo.bar'); setting('foo', 'default value'); setting()->get('foo'); setting()->get('foo.bar'); // Set values setting(['foo' => 'bar'])->save(); setting(['foo.bar' => 'baz'])->save(); setting()->set('foo', 'bar')->save(); // Flipping a boolean setting: setting()->set('notifications', true)->save(); // Disable notifications. setting()->flip('notifications')->save(); dd(setting()->get('notifications')); // Returns false. // Enable notifications. setting()->flip('notifications')->save(); dd(setting()->get('notifications')); // Returns true. // Default flip setting: setting()->flip('new-key')->save(); dd(setting()->get('new-key')); // Returns true. // Enabling a boolean setting: setting()->set('notifications', false)->save(); setting()->enable('notifications')->save(); dd(setting()->get('notifications')); // Returns true. // Disabling a boolean setting: setting()->set('notifications', true)->save(); setting()->disable('notifications')->save(); dd(setting()->get('notifications')); // Returns false. // Method chaining setting(['foo' => 'bar'])->save();
您还可以使用@setting() blade指令
@setting('foo')
@setting('foo', 'default value')
命令行辅助工具
php artisan setting:forget foo
php artisan setting:get || php artisan setting:get foo
php artisan setting:set-or-update foo bar
存储缓存
在从存储读取时,您可以使用缓存。
您还可以在写入时配置缓存刷新并配置生存时间。
读取将来自存储,然后来自缓存,这可以减少对存储的负载。
'cache' => [ 'enableCache' => false, 'cacheTtl' => 15, // TTL in seconds. ]
配置
默认
<?php return [ /* |-------------------------------------------------------------------------- | Default Settings Store |-------------------------------------------------------------------------- | | This option controls the default settings store that gets used while | using this settings library. | | Supported: "json", "database", "redis" | */ 'default' => 'json', // ... ];
您可以在此处指定您要使用的默认存储驱动程序。
驱动程序
<?php return [ //... /* |-------------------------------------------------------------------------- | Drivers Stores |-------------------------------------------------------------------------- | | The settings are stored. | */ 'drivers' => [ 'database' => [ 'driver' => \MichaelNabil230\Setting\Stores\DatabaseSettingStore::class, 'options' => [ 'model' => Setting::class, 'table' => 'settings', // name of table in dataBase 'cache' => [ 'enableCache' => false, 'cacheTtl' => 15, // TTL in seconds. ] ], ], 'redis' => [ 'driver' => \MichaelNabil230\Setting\Stores\RedisSettingStore::class, 'options' => [ 'connection' => 'default', 'prefix' => 'setting', ], ], 'json' => [ 'driver' => \MichaelNabil230\Setting\Stores\JsonSettingStore::class, 'options' => [ 'path' => storage_path('settings.json'), ] ], ], ];
这是支持的存储驱动程序的列表。您可以通过添加自定义存储驱动程序来扩展此列表。
存储配置的结构如下
<?php // ... 'custom' => [ 'driver' => App\Stores\CustomStore::class, 'options' => [ // ... ], ],
1. 创建自定义存储类
<?php namespace App\Settings; use MichaelNabil230\Setting\Stores\SettingStore as Store; class CustomStore implements Store { // Implement the contract's methods here }
2. 注册自定义存储
转到config/setting.php
配置文件并编辑drivers
列表
return [ 'drivers' => [ 'custom' => [ 'driver' => App\Settings\CustomStore::class, ], ], ];
如果您使用了抽象的MichaelNabil230\Setting\Contracts\Store
类,您可以传递一个包含凭证密钥、路径等的options
数组
return [ 'drivers' => [ 'custom' => [ 'driver' => App\Settings\CustomStore::class, 'options' => [ // more customize ], ], ], ];
最后但同样重要的是,您可以将其设置为默认存储。
与stancl/tenancy
集成
像往常一样安装包,但发布迁移并将它们移动到migrations/tenant
php artisan vendor:publish --tag="setting-migrations"
mv database/migrations/*_create_settings_table.php database/migrations/tenant
然后将其添加到您的AppServiceProvider::boot()
方法中
Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) { \MichaelNabil230\Setting\Stores\DatabaseSettingStore::$cacheKey = 'setting.cache.tenant.' . $event->tenancy->tenant->id; });
测试
composer test
支持
变更日志
有关最近更改的更多信息,请参阅变更日志
贡献
有关详细信息,请参阅贡献指南
安全漏洞
有关报告安全漏洞的详细信息,请参阅我们的安全策略
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件