spatie / laravel-settings
存储您的应用程序设置
Requires
- php: ^7.4|^8.0
- ext-json: *
- illuminate/database: ^8.73|^9.0|^10.0|^11.0
- phpdocumentor/type-resolver: ^1.5
- spatie/temporary-directory: ^1.3|^2.0
Requires (Dev)
- ext-redis: *
- larastan/larastan: ^2.0
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.23|^7.0|^8.0|^9.0
- pestphp/pest: ^1.21|^2.0
- pestphp/pest-plugin-laravel: ^1.2|^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5|^10.0
- spatie/laravel-data: ^1.0.0|^2.0.0|^4.0.0
- spatie/pest-plugin-snapshots: ^1.1|^2.0
- spatie/phpunit-snapshot-assertions: ^4.2|^5.0
- spatie/ray: ^1.36
Suggests
- spatie/data-transfer-object: Allows for DTO casting to settings. (deprecated)
- dev-main
- 3.4.0
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.0
- 2.8.3
- 2.8.2
- 2.8.1
- 2.8.0
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.12
- 2.1.11
- 2.1.10
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.6.1
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.2
- 0.0.1
- dev-revert-241-main
- dev-revert-239-feature/improved-migration-match
- dev-features/structure-discoverer
This package is auto-updated.
Last update: 2024-09-20 13:48:54 UTC
README
存储强类型应用程序设置
此包允许您将设置存储在存储库(数据库、Redis、...)中,并且无需麻烦即可在应用程序中使用它们。您可以创建一个设置类如下所示
class GeneralSettings extends Settings { public string $site_name; public bool $site_active; public static function group(): string { return 'general'; } }
如果您想在应用程序的某个地方使用这些设置,您可以注入它们,因为我们在 Laravel 容器中注册了它们。例如,在一个控制器中
class GeneralSettingsController { public function show(GeneralSettings $settings){ return view('settings.show', [ 'site_name' => $settings->site_name, 'site_active' => $settings->site_active ]); } }
您可以像这样更新设置
class GeneralSettingsController { public function update( GeneralSettingsRequest $request, GeneralSettings $settings ){ $settings->site_name = $request->input('site_name'); $settings->site_active = $request->input('site_active'); $settings->save(); return redirect()->back(); } }
让我们看看如何创建您自己的设置类。
支持我们
我们在创建一流的开源软件包上投入了大量资源。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡寄来明信片,说明您正在使用我们的哪个包。您可以在我们的联系页面上找到我们的地址。我们将发布在我们的虚拟明信片墙上。
安装
您可以通过 composer 安装此包
composer require spatie/laravel-settings
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="config"
这是已发布的配置文件的内容
return [ /* * Each settings class used in your application must be registered, you can * add them (manually) here. */ 'settings' => [ ], /* * The path where the settings classes will be created. */ 'setting_class_path' => app_path('Settings'), /* * In these directories settings migrations will be stored and ran when migrating. A settings * migration created via the make:settings-migration command will be stored in the first path or * a custom defined path when running the command. */ 'migrations_paths' => [ database_path('settings'), ], /* * When no repository is set for a settings class, the following repository * will be used for loading and saving settings. */ 'default_repository' => 'database', /* * Settings will be stored and loaded from these repositories. */ 'repositories' => [ 'database' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => null, ], 'redis' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\RedisSettingsRepository::class, 'connection' => null, 'prefix' => null, ], ], /* * The encoder and decoder will determine how settings are stored and * retrieved in the database. By default, `json_encode` and `json_decode` * are used. */ 'encoder' => null, 'decoder' => null, /* * The contents of settings classes can be cached through your application, * settings will be stored within a provided Laravel store and can have an * additional prefix. */ 'cache' => [ 'enabled' => env('SETTINGS_CACHE_ENABLED', false), 'store' => null, 'prefix' => null, ], /* * These global casts will be automatically used whenever a property within * your settings class isn't the default PHP type. */ 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, // Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, Spatie\LaravelData\Data::class => Spatie\LaravelSettings\SettingsCasts\DataCast::class, ], /* * The package will look for settings in these paths and automatically * register them. */ 'auto_discover_settings' => [ app_path('Settings'), ], /* * Automatically discovered settings classes can be cached, so they don't * need to be searched each time the application boots up. */ 'discovered_settings_cache_path' => base_path('bootstrap/cache'), ];
用法
此包围绕设置类构建,这些类是扩展自 Settings
的具有公共属性的类。它们还有一个静态方法 group
,该方法应返回一个字符串。
您可以为每个设置类创建多个设置组,每个组都有一个设置类。例如,您可以有 GeneralSettings
,具有 general
组,以及 BlogSettings
,具有 blog
组。这些组如何结构化取决于您。
尽管可以为不同的设置类使用相同的组,但我们建议不要为多个设置类使用相同的组。
use Spatie\LaravelSettings\Settings; class GeneralSettings extends Settings { public string $site_name; public bool $site_active; public static function group(): string { return 'general'; } }
您可以使用以下 artisan 命令生成一个新的设置类。在您这样做之前,请确保已正确设置了 setting_class_path
。您还可以指定一个可选的 path
选项。
php artisan make:setting SettingName --group=groupName
现在,您必须将此设置类添加到 settings.php
配置文件中的 settings
数组中,以便它可以通过 Laravel 加载
/* * Each settings class used in your application must be registered, you can * add them (manually) here. */ 'settings' => [ GeneralSettings::class ],
设置类中的每个属性都需要一个默认值,该值应在其迁移中设置。您可以创建一个迁移如下所示
php artisan make:settings-migration CreateGeneralSettings
此命令将在 database/settings
中创建一个新的文件,您可以在此添加属性及其默认值
use Spatie\LaravelSettings\Migrations\SettingsMigration; class CreateGeneralSettings extends SettingsMigration { public function up(): void { $this->migrator->add('general.site_name', 'Spatie'); $this->migrator->add('general.site_active', true); } }
我们在这里将属性 site_name
和 site_active
添加到 general
组,值分别为 Spatie
和 true
。有关迁移的更多信息,请参阅此处。
您应该迁移数据库以添加属性
php artisan migrate
现在,当您想要使用 GeneralSettings
设置类的 site_name
属性时,您可以将其注入到您的应用程序中
class IndexController { public function __invoke(GeneralSettings $settings){ return view('index', [ 'site_name' => $settings->site_name, ]); } }
或将其加载到应用程序的某个位置,如下所示
function getName(): string{ return app(GeneralSettings::class)->site_name; }
更新设置可以这样做
class SettingsController { public function __invoke(GeneralSettings $settings, GeneralSettingsRequest $request){ $settings->site_name = $request->input('site_name'); $settings->site_active = $request->boolean('site_active'); $settings->save(); return redirect()->back(); } }
选择存储库
设置将被存储和从仓库中加载。存在两种类型的仓库:数据库
和 redis
。可以为这些类型创建多个仓库。例如,您可能有两个 数据库
仓库,一个用于数据库中的 settings
表,另一个用于 global_settings
表。
您可以通过实现 repository
方法显式设置设置类的仓库。
class GeneralSettings extends Settings { public string $site_name; public bool $site_active; public static function group(): string { return 'general'; } public static function repository(): ?string { return 'global_settings'; } }
如果一个设置类没有设置仓库,将使用 settings.php
配置文件中的 default_repository
。
创建设置迁移
在您能加载/更新设置之前,您必须迁移它们。虽然一开始这可能听起来有些奇怪,但实际上是非常合理的。您希望在创建新应用程序时有一些默认设置。如果我们更改设置类的属性会发生什么?我们的代码会更改,但我们的数据不会。
因此,每次更改/创建设置类的结构时,该包都需要迁移。这些迁移将运行在常规 Laravel 数据库迁移旁边,并且我们添加了一些工具以便快速编写它们。
创建设置迁移的方式与创建常规数据库迁移一样。您可以运行以下命令
php artisan make:settings-migration CreateGeneralSettings
这将向 application/database/settings
目录添加迁移
use Spatie\LaravelSettings\Migrations\SettingsMigration; class CreateGeneralSettings extends SettingsMigration { public function up(): void { } }
我们没有添加 down
方法,但如果需要,可以添加。在 up
方法中,您可以在迁移时更改仓库中的设置数据。支持一些默认操作
添加属性
您可以这样向设置组添加属性
public function up(): void { $this->migrator->add('general.timezone', 'Europe/Brussels'); }
我们已向 general
组添加了一个 timezone
属性,它被 GeneralSettings
使用。您应该始终为新建的设置提供一个默认值。在这种情况下,这是 Europe/Brussels
时区。
如果设置类中的属性是可空的,则可以提供 null
作为默认值。
重命名属性
可以重命名属性
public function up(): void { $this->migrator->rename('general.timezone', 'general.local_timezone'); }
您也可以将属性移动到另一个组
public function up(): void { $this->migrator->rename('general.timezone', 'country.timezone'); }
更新属性
可以更新属性的内容
public function up(): void { $this->migrator->update( 'general.timezone', fn(string $timezone) => return 'America/New_York' ); }
如您所见,此方法接受一个闭包作为参数,这使得根据其旧值更新值成为可能。
删除属性
public function up(): void { $this->migrator->delete('general.timezone'); }
检查属性是否存在
有时您可能想要检查数据库中是否存在属性。可以这样操作
public function up(): void { if ($this->migrator->exists('general.timezone')) { // do something } }
组内的操作
当您处理具有许多属性的庞大设置类时,总是需要预先添加设置组可能有些繁琐。这就是为什么您也可以在设置组内执行操作
public function up(): void { $this->migrator->inGroup('general', function (SettingsBlueprint $blueprint): void { $blueprint->add('timezone', 'Europe/Brussels'); $blueprint->rename('timezone', 'local_timezone'); $blueprint->update('timezone', fn(string $timezone) => return 'America/New_York'); $blueprint->delete('timezone'); }); }
属性类型
您可以使用常规 PHP 类型创建设置类
class RegularTypeSettings extends Settings { public string $a_string; public bool $a_bool; public int $an_int; public float $a_float; public array $an_array; public static function group(): string { return 'regular_type'; } }
内部,该包将将这些类型转换为 JSON 并以这种方式存储在仓库中。但是关于 DateTime
和 Carbon
或您自己创建的类型呢?尽管这些类型可以被转换为 JSON,但从 JSON 中重新构建它们是不支持的。
因此,您可以在该包中指定类型转换。有两种定义这些转换的方法:本地或全局。
本地转换
本地转换适用于一个特定的设置类,并且应该为每个属性定义
class DateSettings extends Settings { public DateTime $birth_date; public static function group(): string { return 'date'; } public static function casts(): array { return [ 'birth_date' => DateTimeInterfaceCast::class ]; } }
DateTimeInterfaceCast
可以用于具有类似 DateTime
、DateTimeImmutable
、Carbon
和 CarbonImmutable
的类型的属性。您还可以使用已经构造的转换。当您需要向转换传递一些额外参数时,这很有用。
class DateSettings extends Settings { public $birth_date; public static function group(): string { return 'date'; } public static function casts(): array { return [ 'birth_date' => new DateTimeInterfaceWithTimeZoneCast(DateTime::class, 'Europe/Brussels') ]; } }
如您所见,我们向转换提供了 DateTime::class
,因此它知道应该使用哪种类型的 DateTime
,因为 birth_date
属性没有类型化,转换无法推断要使用的类型。
您还可以为转换提供参数,而无需构建它
class DateSettings extends Settings { public $birth_date; public static function group(): string { return 'date'; } public static function casts(): array { return [ 'birth_date' => DateTimeInterfaceCast::class.':'.DateTime::class ]; } }
全局转换
局部转换非常适合为设置类定义特定属性的类型。但为每个经常使用的类型(如DateTime
)定义局部转换是一项繁重的工作。全局转换试图简化此过程。
您可以在包配置的global_casts
数组中定义全局转换。我们已经添加了一些默认转换到配置中,这可能很有用
'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class, // Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class, Spatie\LaravelData\Data::class => Spatie\LaravelSettings\SettingsCasts\DataCast::class, ],
全局转换可以作用于
- 特定类型(
DateTimeZone::class
) - 实现接口的类型(
DateTimeInterface::class
) - 从另一个类扩展的类型(
Data::class
)
在您的设置类中,当您使用DateTime
属性(该属性实现了DateTimeInterface
)时,您不再需要定义局部转换
class DateSettings extends Settings { public DateTime $birth_date; public static function group(): string { return 'date'; } }
包将自动找到转换,并将其用于在设置类和存储库之间转换类型。
属性类型
类型属性有很多选项。您可以在PHP中指定它们
class DateSettings extends Settings { public DateTime $birth_date; public ?int $a_nullable_int; public static function group(): string { return 'date'; } }
或者您可以使用文档块
class DateSettings extends Settings { /** @var \DateTime */ public $birth_date; /** @var ?int */ public $a_nullable_int; /** @var int|null */ public $another_nullable_int; /** @var int[]|null */ public $an_array_of_ints_or_null; public static function group(): string { return 'date'; } }
文档块对于指定对象数组非常有用
class DateSettings extends Settings { /** @var array<\DateTime> */ public array $birth_dates; // OR /** @var \DateTime[] */ public array $birth_dates_alternative; public static function group(): string { return 'date'; } }
锁定属性
当您想要禁用更新设置值的权限时,您可以对其进行锁定
$dateSettings->lock('birth_date');
现在无法更新birth_date
的值。当尝试覆盖birth_date
并保存设置时,包将从存储库中加载birth_date
的旧值,看起来好像没有发生任何事情。
您还可以一次性锁定多个设置
$dateSettings->lock('birth_date', 'name', 'email');
您可以看到所有已锁定的设置
$dateSettings->getLockedProperties(); // ['birth_date']
解锁设置可以这样做
$dateSettings->unlock('birth_date', 'name', 'email');
检查设置是否当前已锁定可以这样做
$dateSettings->isLocked('birth_date');
检查设置是否当前已解锁可以这样做
$dateSettings->isUnlocked('birth_date');
加密属性
您的设置类中的一些属性可能是机密的,例如API密钥。您可以将一些属性加密,这样在存储库数据被破坏时,将无法读取它们。
将加密添加到设置类的属性可以这样完成。通过向设置类添加encrypted
静态方法并列出所有应加密的属性
class GeneralSettings extends Settings { public string $site_name; public bool $site_active; public static function group(): string { return 'general'; } public static function encrypted(): array { return [ 'site_name' ]; } }
在迁移中使用加密
在迁移中创建和更新加密属性的工作方式与非加密属性略有不同。
在创建新属性时,您应使用addEncrypted
方法而不是调用add
方法
public function up(): void { $this->migrator->addEncrypted('general.site_name', 'Spatie'); }
对于update
方法,也应将其替换为updateEncrypted
public function up(): void { $this->migrator->updateEncrypted( 'general.site_name', fn(string $siteName) => return 'Space' ); }
您可以在迁移中将非加密属性转换为加密属性
public function up(): void { $this->migrator->add('general.site_name', 'Spatie'); $this->migrator->encrypt('general.site_name'); }
或者将加密属性转换为非加密属性
public function up(): void { $this->migrator->addEncrypted('general.site_name', 'Spatie'); $this->migrator->decrypt('general.site_name'); }
当然,您可以在使用inGroup
迁移操作时使用这些方法。
自定义编码和解码器
您可以通过更改包配置来定义自定义编码和解码器,而不是使用内置的json_encode
和json_decode
... 'encoder' => fn($value): string => str_rot13(json_encode($value)), 'decoder' => fn(string $payload, bool $associative) => json_decode(str_rot13($payload), $associative), ...
伪造设置类
在测试中,有时希望某些设置类可以使用与您在迁移中编写的默认值不同的值快速使用。这就是为什么您可以伪造设置。伪造的设置类将注册在容器中。您可以在设置类中覆盖一些或所有属性
DateSettings::fake([ 'birth_date' => new DateTime('16-05-1994') ]);
现在,当DateSettings
设置类在您的应用程序中某处被注入时,birth_date
属性将为DateTime('16-05-1994')
。
如果所有属性都被覆盖,则不会调用存储库。如果只覆盖了一些属性,则包将首先添加覆盖的属性,然后从存储库中加载缺失的设置。在像这样模拟方法调用时,可以显式抛出 MissingSettings 异常,当属性未被覆盖时。
DateSettings::fake([ 'birth_date' => new DateTime('16-05-1994') ], false);
缓存设置
从存储库加载设置类需要一点时间。当你有很多设置类时,这些额外的小量时间会迅速累积。该包内置了对使用 Laravel 缓存存储设置的缓存支持。
您应该首先在 settings.php
配置文件中启用缓存
'cache' => [ 'enabled' => env('SETTINGS_CACHE_ENABLED', false), 'store' => null, 'prefix' => null, ],
我们建议您通过将 SETTINGS_CACHE_ENABLED=true
添加到您的 .env
文件中来在生产环境中启用缓存。您还可以定义一个缓存存储,这应该是您在 cache.php
配置文件中定义的存储之一。如果没有定义存储,则默认缓存存储将被使用。为了避免缓存中的冲突,您还可以定义一个前缀,该前缀将被添加到每个缓存条目中。
这就完了。该包现在足够智能,可以缓存首次加载的设置。每当设置被编辑时,该包将刷新设置。
您可以使用以下命令清除缓存的设置:
php artisan settings:clear-cache
自动发现设置类
您创建的每个设置类都应该添加到 settings.php
配置文件中的 settings
数组中。当您有很多设置时,这可能会很快被遗忘。
这就是为什么还可以自动发现设置类。该包将遍历您的应用程序并尝试发现设置类。您可以在配置中的 auto_discover_settings
数组中指定要搜索的路径。默认情况下,这是应用程序的 app 路径。
自动发现设置需要在您的应用程序启动之前额外花费一些时间。这就是为什么可以使用以下命令来缓存它们:
php artisan settings:discover
您可以通过运行以下命令来清除此缓存:
php artisan settings:clear-discovered
编写自己的铸件
铸件是实现了 SettingsCast
接口的类
interface SettingsCast { /** * Will be used to when retrieving a value from the repository, and * inserting it into the settings class. */ public function get($payload); /** * Will be used to when retrieving a value from the settings class, and * inserting it into the repository. */ public function set($payload); }
创建的铸件可用于本地和全局铸件,但它们之间有一些细微差别。该包将始终尝试注入它所转换的类型。这是一个类字符串,并在构造铸件时作为第一个参数提供。当它无法推断类型时,将使用 null
作为第一个参数。
以下是一个具有类型注入的此类铸件的示例:简化的 DtoCast
class DtoCast implements SettingsCast { private string $type; public function __construct(?string $type) { $this->type = $type; } public function get($payload): Data { return $this->type::from($payload); } public function set($payload): array { return $payload->toArray(); } }
上述是 spatie/laravel-data 包的铸件,在其构造函数中,类型将是一个特定的 Data 类,例如 SongData::class
。在 get
方法中,铸件将使用存储库属性构造一个 Data::class
。在 set
方法中,铸件将接收一个 Data::class
作为有效载荷并转换为数组以安全地存储在存储库中。
本地转换
在本地铸件中,有几个不同的可能性来推断类型
// By the type of property class CastSettings extends Settings { public DateTime $birth_date; public static function casts(): array { return [ 'birth_date' => DateTimeInterfaceCast::class ]; } ... }
// By the docblock of a property class CastSettings extends Settings { /** @var \DateTime */ public $birth_date; public static function casts(): array { return [ 'birth_date' => DateTimeInterfaceCast::class ]; } ... }
// By explicit definition class CastSettings extends Settings { public $birth_date; public static function casts(): array { return [ 'birth_date' => DateTimeInterfaceCast::class.':'.DateTime::class ]; } ... }
在最后一种情况下:通过显式定义,可以提供传递给构造函数的额外参数
class CastSettings extends Settings { public $birth_date; public static function casts(): array { return [ 'birth_date' => DateTimeWthTimeZoneInterfaceCast::class.':'.DateTime::class.',Europe/Brussels' ]; } ... }
尽管在这种情况下,在设置类中构造铸件可能更容易阅读
class CastSettings extends Settings { public $birth_date; public static function casts(): array { return [ 'birth_date' => new DateTimeWthTimeZoneInterfaceCast(DateTime::class, 'Europe/Brussels') ]; } ... }
全局转换
在全局铸件中,该包将再次尝试推断所转换属性的类型。在这种情况下,它只能使用属性类型或推断属性 docblock 的类型。
全局铸件应在 settings.php
配置文件中配置,并且始终有一个特定的(一组)类型与之相关联。这些类型可以是特定类、实现接口的类组或从另一个类扩展的类组。
这里的一个好例子是我们默认添加到配置中的DateTimeInterfaceCast
。它在配置中定义如下:
... 'global_casts' => [ DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class, ], ...
每当包检测到Carbon
、CarbonImmutable
、DateTime
或DateTimeImmutable
类型作为设置类属性的类型时。它将使用DateTimeInterfaceCast
作为转换器。这是因为Carbon
、CarbonImmutable
、DateTime
和DateTimeImmutable
都实现了DateTimeInterface
。在settings.php
中使用的键来表示转换。
注入到转换器中的类型将是属性的类型。假设在你的设置类中有一个类型为DateTime
的属性。当转换这个属性时,DateTimeInterfaceCast
将接收DateTime:class
作为类型。
仓库
该包包含了两种类型的仓库,即redis
和database
仓库。你可以在setting.php
配置文件中为一种类型创建多个仓库。每个仓库都可以进行配置。
数据库仓库
数据库仓库有两个可选配置选项
model
用来加载/保存属性到数据库的Eloquent模型table
数据库中使用的表connection
与数据库交互时使用的连接
Redis仓库
Redis仓库也有两个可选配置选项
prefix
可选的前缀,它将被添加到键的前面connection
与Redis交互时使用的连接
缓存
您可以通过在settings.php
配置文件中添加默认的缓存配置,为每个仓库添加自定义的缓存配置,来为每个仓库添加自定义的缓存配置
'repositories' => [ 'landlord' => [ 'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class, 'model' => null, 'table' => null, 'connection' => 'landlord', 'cache' => [ 'enabled' => env('SETTINGS_CACHE_ENABLED', false), 'store' => null, 'prefix' => 'landlord', 'ttl' => null, ], ], ... ],
创建自己的仓库类型
您可以创建自己的仓库类型。仓库是一个实现了SettingsRepository
的类
interface SettingsRepository { /** * Get all the properties in the repository for a single group */ public function getPropertiesInGroup(string $group): array; /** * Check if a property exists in a group */ public function checkIfPropertyExists(string $group, string $name): bool; /** * Get the payload of a property */ public function getPropertyPayload(string $group, string $name); /** * Create a property within a group with a payload */ public function createProperty(string $group, string $name, $payload): void; /** * Update the payloads of properties within a group. */ public function updatePropertiesPayload(string $group, array $properties): void; /** * Delete a property from a group */ public function deleteProperty(string $group, string $name): void; /** * Lock a set of properties for a specific group */ public function lockProperties(string $group, array $properties): void; /** * Unlock a set of properties for a group */ public function unlockProperties(string $group, array $properties): void; /** * Get all the locked properties within a group */ public function getLockedProperties(string $group): array; }
所有这些函数都应实现以与您使用的存储类型交互。payload
参数是原始值(int
、bool
、float
、string
、array
)。在database
和redis
仓库类型中,这些原始值被转换为JSON。但这不是必需的。
在getPropertiesInGroup
和getPropertyPayload
方法中,必须再次返回原始值。
每个仓库的构造函数将接收一个$config
数组,该数组由用户在应用程序的settings.php
配置文件中为仓库定义。您还可以向构造函数添加其他依赖项。它们将在创建仓库时被注入。
刷新设置
您可以刷新设置类中的值和锁定属性。如果您更改了仓库中的某些内容并希望它在设置中反映出来,这可能很有用
$settings->refresh();
仅在仓库值更改时刷新设置,而设置类已经加载。
事件
当加载/保存设置类时,该包将发出一系列事件
LoadingSettings
当设置从仓库加载但尚未插入设置类时SettingsLoaded
设置加载到设置类之后SavingSettings
当设置保存到仓库但尚未转换或加密时SettingsSaved
设置存储在仓库中之后
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志
贡献
有关详细信息,请参阅贡献指南
安全性
如果您发现了关于安全的错误,请通过邮件security@spatie.be报告,而不是使用问题跟踪器。
鸣谢
许可
麻省理工学院许可证(MIT)。有关更多信息,请参阅许可文件。