[增添]添加了datasource的setting数据库以及默认值

This commit is contained in:
makotocc0107
2024-08-27 09:57:44 +08:00
parent d111dfaea4
commit 72eb990970
10955 changed files with 978898 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
# Filament Spatie Settings Plugin
## Installation
Install the plugin with Composer:
```bash
composer require filament/spatie-laravel-settings-plugin:"^3.2" -W
```
## Preparing your page class
Settings pages are Filament pages that extend the `Filament\Pages\SettingsPage` class.
This package uses the [`spatie/laravel-settings` package](https://github.com/spatie/laravel-settings) to store and retrieve settings via the database.
Before you start, create a settings class in your `app/Settings` directory, and a database migration for it. You can find out more about how to do this in the [Spatie documentation](https://github.com/spatie/laravel-settings#usage).
Once you've created your settings class, you can create a settings page in Filament for it using the following command:
```bash
php artisan make:filament-settings-page ManageFooter FooterSettings
```
In this example, you have a `FooterSettings` class in your `app/Settings` directory.
In your new settings page class, generated in the `app/Filament/Pages` directory, you will see the static `$settings` property assigned to the settings class:
```php
protected static string $settings = FooterSettings::class;
```
## Building a form
You must define a form schema to interact with your settings class inside the `form()` method.
Since the [Form Builder](https://filamentphp.com/docs/forms) is installed in the Panel Builder by default, you may use any form [fields](https://filamentphp.com/docs/forms/fields) or [layout components](https://filamentphp.com/docs/forms/layout) you like:
```php
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('copyright')
->label('Copyright notice')
->required(),
Repeater::make('links')
->schema([
TextInput::make('label')->required(),
TextInput::make('url')
->url()
->required(),
]),
]);
}
```
The name of each form field must correspond with the name of the property on your settings class.
The form will automatically be filled with settings from the database, and saved without any extra work.
## Publishing translations
If you wish to translate the package, you may publish the language files using:
```bash
php artisan vendor:publish --tag=filament-spatie-laravel-settings-plugin-translations
```

View File

@@ -0,0 +1,35 @@
{
"name": "filament/spatie-laravel-settings-plugin",
"description": "Filament support for `spatie/laravel-settings`.",
"license": "MIT",
"homepage": "https://github.com/filamentphp/filament",
"support": {
"issues": "https://github.com/filamentphp/filament/issues",
"source": "https://github.com/filamentphp/filament"
},
"require": {
"php": "^8.1",
"filament/filament": "self.version",
"illuminate/console": "^10.45|^11.0",
"illuminate/filesystem": "^10.45|^11.0",
"illuminate/support": "^10.45|^11.0",
"spatie/laravel-settings": "^2.2|^3.0"
},
"autoload": {
"psr-4": {
"Filament\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Filament\\SpatieLaravelSettingsPluginServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'حفظ التغييرات',
],
],
],
'notifications' => [
'saved' => [
'title' => 'تم الحفظ',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Yadda saxla',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Yadda saxlanıldı',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'সংরক্ষণ করুন',
],
],
],
'notifications' => [
'saved' => [
'title' => 'সংরক্ষিত হয়েছে',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Desar canvis',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Canvis desats',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'نوێکردنەوە',
],
],
],
'notifications' => [
'saved' => [
'title' => 'نوێکرایەوە',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Uložit změny',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Uloženo',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Gem ændringer',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Gemt',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Änderungen speichern',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Gespeichert',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Save changes',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Saved',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Guardar',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Guardado',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Gorde',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Gordeta',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'ذخیره',
],
],
],
'notifications' => [
'saved' => [
'title' => 'ذخیره شد',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Tallenna',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Tallennettu',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Sauvegarder',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Sauvegardé',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'שמור שינויים',
],
],
],
'notifications' => [
'saved' => [
'title' => 'נשמר בהצלחה',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Spremi promjene',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Spremljeno',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Változtatások mentése',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Mentve',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Պահպանել',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Պահպանվել է',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Simpan',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Pengaturan berhasil disimpan',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Salva',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Salvato',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => '変更を保存',
],
],
],
'notifications' => [
'saved' => [
'title' => '保存しました',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'រក្សាទុកការផ្លាស់ប្តូរ',
],
],
],
'notifications' => [
'saved' => [
'title' => 'បានរក្សាទុក',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'نوێکردنەوە',
],
],
],
'notifications' => [
'saved' => [
'title' => 'نوێکرایەوە',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Išsaugoti pakeitimus',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Išsaugota',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Simpan',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Disimpan',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Wijzigingen opslaan',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Opgeslagen',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Lagre endringer',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Lagret',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'परिवर्तनहरू सुरक्षित गर्नुहोस',
],
],
],
'notifications' => [
'saved' => [
'title' => 'सुरक्षित गरियो',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Salvar',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Salvo',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Guardar alterações',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Guardado',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Salvare',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Salvat cu succes',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Сохранить',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Сохранено',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Uložiť zmeny',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Uložené',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Spara ändringar',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Sparades',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Kaydet',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Kaydedildi',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Зберегти',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Збережено',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'O\'zgarishlarni saqlash',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Saqlandi',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => 'Lưu thay đổi',
],
],
],
'notifications' => [
'saved' => [
'title' => 'Đã lưu',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => '保存',
],
],
],
'notifications' => [
'saved' => [
'title' => '已保存',
],
],
];

View File

@@ -0,0 +1,25 @@
<?php
return [
'form' => [
'actions' => [
'save' => [
'label' => '保存',
],
],
],
'notifications' => [
'saved' => [
'title' => '已保存',
],
],
];

View File

@@ -0,0 +1,12 @@
<x-filament-panels::page>
<x-filament-panels::form id="form" wire:submit="save">
{{ $this->form }}
<x-filament-panels::form.actions
:actions="$this->getCachedFormActions()"
:full-width="$this->hasFullWidthFormActions()"
/>
</x-filament-panels::form>
<x-filament-panels::page.unsaved-data-changes-alert />
</x-filament-panels::page>

View File

@@ -0,0 +1,12 @@
<?php
namespace Filament\Commands\Aliases;
use Filament\Commands;
class MakeSettingsPageCommand extends Commands\MakeSettingsPageCommand
{
protected $hidden = true;
protected $signature = 'filament:settings-page {name?} {settingsClass?} {--panel=} {--F|force}';
}

View File

@@ -0,0 +1,118 @@
<?php
namespace Filament\Commands;
use Filament\Clusters\Cluster;
use Filament\Facades\Filament;
use Filament\Panel;
use Filament\Support\Commands\Concerns\CanIndentStrings;
use Filament\Support\Commands\Concerns\CanManipulateFiles;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
class MakeSettingsPageCommand extends Command
{
use CanIndentStrings;
use CanManipulateFiles;
protected $description = 'Create a new Filament settings page class';
protected $signature = 'make:filament-settings-page {name?} {settingsClass?} {--panel=} {--F|force}';
public function handle(): int
{
$page = (string) str($this->argument('name') ?? text(
label: 'What is the page name?',
placeholder: 'ManageFooter',
required: true,
))
->trim('/')
->trim('\\')
->trim(' ')
->replace('/', '\\');
$pageClass = (string) str($page)->afterLast('\\');
$pageNamespace = str($page)->contains('\\') ?
(string) str($page)->beforeLast('\\') :
'';
$settingsClass = (string) str($this->argument('settingsClass') ?? text(
label: 'What is the settings name?',
placeholder: 'FooterSettings',
required: true,
))
->trim('/')
->trim('\\')
->trim(' ');
$panel = $this->option('panel');
if ($panel) {
$panel = Filament::getPanel($panel, isStrict: false);
}
if (! $panel) {
$panels = Filament::getPanels();
/** @var Panel $panel */
$panel = (count($panels) > 1) ? $panels[select(
label: 'Which panel would you like to create this in?',
options: array_map(
fn (Panel $panel): string => $panel->getId(),
$panels,
),
default: Filament::getDefaultPanel()->getId()
)] : Arr::first($panels);
}
$pageDirectories = $panel->getPageDirectories();
$pageNamespaces = $panel->getPageNamespaces();
$namespace = (count($pageNamespaces) > 1) ?
select(
label: 'Which namespace would you like to create this in?',
options: $pageNamespaces
) :
(Arr::first($pageNamespaces) ?? 'App\\Filament\\Pages');
$path = (count($pageDirectories) > 1) ?
$pageDirectories[array_search($namespace, $pageNamespaces)] :
(Arr::first($pageDirectories) ?? app_path('Filament/Pages/'));
$path = (string) str($page)
->prepend('/')
->prepend($path)
->replace('\\', '/')
->replace('//', '/')
->append('.php');
if ($this->checkForCollision([$path])) {
return static::INVALID;
}
$potentialCluster = (string) str($namespace)->beforeLast('\Pages');
$clusterAssignment = null;
$clusterImport = null;
if (
class_exists($potentialCluster) &&
is_subclass_of($potentialCluster, Cluster::class)
) {
$clusterAssignment = $this->indentString(PHP_EOL . PHP_EOL . 'protected static ?string $cluster = ' . class_basename($potentialCluster) . '::class;');
$clusterImport = "use {$potentialCluster};" . PHP_EOL;
}
$this->copyStubToApp('SettingsPage', $path, [
'class' => $pageClass,
'clusterAssignment' => $clusterAssignment,
'clusterImport' => $clusterImport,
'namespace' => $namespace . ($pageNamespace !== '' ? "\\{$pageNamespace}" : ''),
'settingsClass' => $settingsClass,
]);
$this->components->info("Filament settings page [{$path}] created successfully.");
return static::SUCCESS;
}
}

View File

@@ -0,0 +1,198 @@
<?php
namespace Filament\Pages;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Forms\ComponentContainer;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Pages\Concerns\CanUseDatabaseTransactions;
use Filament\Pages\Concerns\HasUnsavedDataChangesAlert;
use Filament\Support\Exceptions\Halt;
use Filament\Support\Facades\FilamentView;
use Throwable;
use function Filament\Support\is_app_url;
/**
* @property ComponentContainer $form
*/
class SettingsPage extends Page
{
use CanUseDatabaseTransactions;
use Concerns\InteractsWithFormActions;
use HasUnsavedDataChangesAlert;
protected static string $settings;
protected static string $view = 'filament-spatie-laravel-settings-plugin::pages.settings-page';
/**
* @var array<string, mixed> | null
*/
public ?array $data = [];
public function mount(): void
{
$this->fillForm();
}
protected function fillForm(): void
{
$this->callHook('beforeFill');
$settings = app(static::getSettings());
$data = $this->mutateFormDataBeforeFill($settings->toArray());
$this->form->fill($data);
$this->callHook('afterFill');
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeFill(array $data): array
{
return $data;
}
public function save(): void
{
try {
$this->beginDatabaseTransaction();
$this->callHook('beforeValidate');
$data = $this->form->getState();
$this->callHook('afterValidate');
$data = $this->mutateFormDataBeforeSave($data);
$this->callHook('beforeSave');
$settings = app(static::getSettings());
$settings->fill($data);
$settings->save();
$this->callHook('afterSave');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
$this->commitDatabaseTransaction();
return;
} catch (Throwable $exception) {
$this->rollBackDatabaseTransaction();
throw $exception;
}
$this->rememberData();
$this->getSavedNotification()?->send();
if ($redirectUrl = $this->getRedirectUrl()) {
$this->redirect($redirectUrl, navigate: FilamentView::hasSpaMode() && is_app_url($redirectUrl));
}
}
public function getSavedNotification(): ?Notification
{
$title = $this->getSavedNotificationTitle();
if (blank($title)) {
return null;
}
return Notification::make()
->success()
->title($title);
}
public function getSavedNotificationTitle(): ?string
{
return $this->getSavedNotificationMessage() ?? __('filament-spatie-laravel-settings-plugin::pages/settings-page.notifications.saved.title');
}
/**
* @deprecated Use `getSavedNotificationTitle()` instead.
*/
protected function getSavedNotificationMessage(): ?string
{
return null;
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeSave(array $data): array
{
return $data;
}
public static function getSettings(): string
{
return static::$settings ?? (string) str(class_basename(static::class))
->beforeLast('Settings')
->prepend('App\\Settings\\')
->append('Settings');
}
/**
* @return array<Action | ActionGroup>
*/
public function getFormActions(): array
{
return [
$this->getSaveFormAction(),
];
}
public function getSaveFormAction(): Action
{
return Action::make('save')
->label(__('filament-spatie-laravel-settings-plugin::pages/settings-page.form.actions.save.label'))
->submit('save')
->keyBindings(['mod+s']);
}
public function getSubmitFormAction(): Action
{
return $this->getSaveFormAction();
}
public function form(Form $form): Form
{
return $form;
}
/**
* @return array<int | string, string | Form>
*/
protected function getForms(): array
{
return [
'form' => $this->form(
$this->makeForm()
->schema($this->getFormSchema())
->statePath('data')
->columns(2)
->inlineLabel($this->hasInlineLabels()),
),
];
}
public function getRedirectUrl(): ?string
{
return null;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Filament;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\ServiceProvider;
class SpatieLaravelSettingsPluginServiceProvider extends ServiceProvider
{
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->commands($this->getCommands());
$this->publishes([
__DIR__ . '/../resources/views' => resource_path('views/vendor/filament-spatie-laravel-settings-plugin'),
], 'filament-spatie-laravel-settings-plugin-views');
foreach (app(Filesystem::class)->files(__DIR__ . '/../stubs/') as $file) {
$this->publishes([
$file->getRealPath() => base_path("stubs/filament/{$file->getFilename()}"),
], 'filament-stubs');
}
}
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'filament-spatie-laravel-settings-plugin');
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'filament-spatie-laravel-settings-plugin');
}
/**
* @return array<class-string>
*/
protected function getCommands(): array
{
$commands = [
Commands\MakeSettingsPageCommand::class,
];
$aliases = [];
foreach ($commands as $command) {
$class = 'Filament\\Commands\\Aliases\\' . class_basename($command);
if (! class_exists($class)) {
continue;
}
$aliases[] = $class;
}
return [
...$commands,
...$aliases,
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace {{ namespace }};
{{ clusterImport }}use App\Settings\{{ settingsClass }};
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Pages\SettingsPage;
class {{ class }} extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';
protected static string $settings = {{ settingsClass }}::class;{{ clusterAssignment }}
public function form(Form $form): Form
{
return $form
->schema([
// ...
]);
}
}