Skip to content

Commit 434e7e3

Browse files
authored
Merge pull request #28 from lara-zeus/configuration-model
refactor configuration model
2 parents c094bc9 + 5a28700 commit 434e7e3

File tree

13 files changed

+1973
-579
lines changed

13 files changed

+1973
-579
lines changed

composer.json

+9-8
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@
2828
"lara-zeus/core": "^3.0"
2929
},
3030
"require-dev": {
31-
"pestphp/pest": "^2.0",
32-
"phpunit/phpunit": "^10.2",
3331
"laravel/pint": "^1.0",
3432
"nunomaduro/collision": "^7.0",
3533
"nunomaduro/larastan": "^2.0.1",
36-
"orchestra/testbench": "^7.0 || ^8.0",
37-
"pestphp/pest-plugin-laravel": "2.x-dev",
38-
"pestphp/pest-plugin-livewire": "2.x-dev",
34+
"nunomaduro/phpinsights": "^2.8",
35+
"orchestra/testbench": "^8.0",
36+
"pestphp/pest": "^2.0",
37+
"pestphp/pest-plugin-arch": "^2.0",
38+
"pestphp/pest-plugin-laravel": "^2.0",
39+
"pestphp/pest-plugin-livewire": "^2.1",
3940
"phpstan/extension-installer": "^1.1",
40-
"phpstan/phpstan-deprecation-rules": "^1.0",
41-
"phpstan/phpstan-phpunit": "^1.0"
41+
"phpstan/phpstan-deprecation-rules": "^1.1"
4242
},
4343
"autoload": {
4444
"psr-4": {
@@ -63,7 +63,8 @@
6363
"sort-packages": true,
6464
"allow-plugins": {
6565
"pestphp/pest-plugin": true,
66-
"phpstan/extension-installer": true
66+
"phpstan/extension-installer": true,
67+
"dealerdirect/phpcodesniffer-composer-installer": true
6768
}
6869
},
6970
"extra": {

composer.lock

+1,908-518
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/configuration.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ these all the available configuration, and their defaults values
1313
RainPlugin::make()
1414
->rainPrefix('rain')
1515
->rainMiddleware(['web'])
16-
->layoutModel(\LaraZeus\Rain\Models\Layout::class)
17-
->columnsModel(\LaraZeus\Rain\Models\Columns::class)
16+
->rainModels([
17+
'Layout' => \LaraZeus\Rain\Models\Layout::class,
18+
'Columns' => \LaraZeus\Rain\Models\Columns::class
19+
])
1820
->uploadDisk('public')
1921
->uploadDirectory('layouts')
2022
->navigationGroupLabel('RaRainin')

resources/views/filament/pages/layouts.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{ $this->mainWidgetForm }}
55

66
<div class="grid grid-cols-12 gap-2 w-full">
7-
@foreach (\LaraZeus\Rain\RainPlugin::get()->getColumnsModel()::all() as $layout)
7+
@foreach (\LaraZeus\Rain\RainPlugin::get()->getModel('Columns')::all() as $layout)
88
<x-filament::section class="w-full {{ $layout->class }} p-2">
99
<p>{{ $layout->name }}</p>
1010
{{ $this->{'widgetsFrom'.$layout->key} }}

resources/views/themes/zeus/rain/layouts.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="container mx-auto">
22
@if($layout->widgets !== null)
33
<div class="grid grid-cols-1 md:grid-cols-12 gap-2 w-full px-2">
4-
@foreach (\LaraZeus\Rain\RainPlugin::get()->getColumnsModel()::all() as $column)
4+
@foreach (\LaraZeus\Rain\RainPlugin::get()->getModel('Columns')::all() as $column)
55
<div class="w-full {{ $column->class }}">
66
@if(isset($layout->widgets[$column->key]))
77
@php

src/Configuration.php

+36-37
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,49 @@
22

33
namespace LaraZeus\Rain;
44

5+
use Closure;
6+
57
trait Configuration
68
{
79
/**
810
* set the default path for the layout page.
911
*/
10-
protected string $rainPrefix = 'rain';
12+
protected Closure | string $rainPrefix = 'rain';
1113

1214
/**
1315
* the middleware you want to apply on the layout page routes
1416
*/
1517
protected array $rainMiddleware = ['web'];
1618

1719
/**
18-
* customize the models
20+
* you can overwrite any model and use your own
1921
*/
20-
protected string $layoutModel = \LaraZeus\Rain\Models\Layout::class;
21-
22-
protected string $columnsModel = \LaraZeus\Rain\Models\Columns::class;
22+
protected array $rainModels = [
23+
'Layout' => \LaraZeus\Rain\Models\Layout::class,
24+
'Columns' => \LaraZeus\Rain\Models\Columns::class,
25+
];
2326

2427
/**
2528
* set the default upload options.
2629
*/
27-
protected string $uploadDisk = 'public';
30+
protected Closure | string $uploadDisk = 'public';
2831

29-
protected string $uploadDirectory = 'layouts';
32+
protected Closure | string $uploadDirectory = 'layouts';
3033

31-
protected string $navigationGroupLabel = 'Rain';
34+
protected Closure | string $navigationGroupLabel = 'Rain';
3235

33-
protected string $defaultLayout = 'new-page';
36+
protected Closure | string $defaultLayout = 'new-page';
3437

35-
public function rainPrefix(string $prefix): static
38+
public function rainPrefix(Closure | string $prefix): static
3639
{
3740
$this->rainPrefix = $prefix;
3841

3942
return $this;
4043
}
4144

42-
public function getRainPrefix(): string
45+
public function getRainPrefix(): Closure | string
4346
{
44-
return $this->rainPrefix;
47+
return $this->evaluate($this->rainPrefix);
4548
}
4649

4750
public function rainMiddleware(array $middleware): static
@@ -56,75 +59,71 @@ public function getMiddleware(): array
5659
return $this->rainMiddleware;
5760
}
5861

59-
public function layoutModel(string $model): static
62+
public function rainModels(array $models): static
6063
{
61-
$this->layoutModel = $model;
64+
$this->rainModels = $models;
6265

6366
return $this;
6467
}
6568

66-
public function getLayoutModel(): string
69+
public function getRainModels(): array
6770
{
68-
return $this->layoutModel;
69-
}
70-
71-
public function columnsModel(string $model): static
72-
{
73-
$this->columnsModel = $model;
74-
75-
return $this;
71+
return $this->rainModels;
7672
}
7773

78-
public function getColumnsModel(): string
74+
public static function getModel(string $model): string
7975
{
80-
return $this->columnsModel;
76+
return array_merge(
77+
(new static())->rainModels,
78+
(new static())::get()->getRainModels()
79+
)[$model];
8180
}
8281

83-
public function uploadDisk(string $disk): static
82+
public function uploadDisk(Closure | string $disk): static
8483
{
8584
$this->uploadDisk = $disk;
8685

8786
return $this;
8887
}
8988

90-
public function getUploadDisk(): string
89+
public function getUploadDisk(): Closure | string
9190
{
92-
return $this->uploadDisk;
91+
return $this->evaluate($this->uploadDisk);
9392
}
9493

95-
public function uploadDirectory(string $dir): static
94+
public function uploadDirectory(Closure | string $dir): static
9695
{
9796
$this->uploadDirectory = $dir;
9897

9998
return $this;
10099
}
101100

102-
public function getUploadDirectory(): string
101+
public function getUploadDirectory(): Closure | string
103102
{
104-
return $this->uploadDirectory;
103+
return $this->evaluate($this->uploadDirectory);
105104
}
106105

107-
public function navigationGroupLabel(string $lable): static
106+
public function navigationGroupLabel(Closure | string $lable): static
108107
{
109108
$this->navigationGroupLabel = $lable;
110109

111110
return $this;
112111
}
113112

114-
public function getNavigationGroupLabel(): string
113+
public function getNavigationGroupLabel(): Closure | string
115114
{
116-
return $this->navigationGroupLabel;
115+
return $this->evaluate($this->navigationGroupLabel);
117116
}
118117

119-
public function defaultLayout(string $layout): static
118+
public function defaultLayout(Closure | string $layout): static
120119
{
121120
$this->defaultLayout = $layout;
122121

123122
return $this;
124123
}
125124

126-
public function getDefaultLayout(): string
125+
public function getDefaultLayout(): Closure | string
127126
{
128-
return $this->defaultLayout;
127+
return $this->evaluate($this->defaultLayout);
129128
}
130129
}

src/Filament/Resources/LayoutResource.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LayoutResource extends Resource
2020

2121
public static function getModel(): string
2222
{
23-
return RainPlugin::get()->getLayoutModel();
23+
return RainPlugin::get()->getModel('Layout');
2424
}
2525

2626
public static function table(Table $table): Table

src/Filament/Resources/LayoutResource/Pages/CreateLayout.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class CreateLayout extends Page implements Forms\Contracts\HasForms
3737
public function mount(int $record = null): void
3838
{
3939
if ($record === null) {
40-
$layoutModel = RainPlugin::get()->getLayoutModel();
40+
$layoutModel = RainPlugin::get()->getModel('Layout');
4141
$this->rainLayout = new $layoutModel();
42-
foreach (RainPlugin::get()->getColumnsModel()::all() as $column) {
42+
foreach (RainPlugin::get()->getModel('Columns')::all() as $column) {
4343
$this->{'widgetsFrom' . $column->key}->fill([
4444
'widgetsData.' . $column->key => [],
4545
]);
@@ -50,10 +50,10 @@ public function mount(int $record = null): void
5050
]);
5151
}
5252
} else {
53-
$this->rainLayout = RainPlugin::get()->getLayoutModel()::findOrFail($record);
53+
$this->rainLayout = RainPlugin::get()->getModel('Layout')::findOrFail($record);
5454

5555
$allWidgets = $this->rainLayout->widgets;
56-
foreach (RainPlugin::get()->getColumnsModel()::all() as $column) {
56+
foreach (RainPlugin::get()->getModel('Columns')::all() as $column) {
5757
if (isset($allWidgets[$column->key])) {
5858
$widgetsItems = (new Collection($allWidgets[$column->key]))->sortBy('data.sort')->toArray();
5959
$this->{'widgetsFrom' . $column->key}->fill([
@@ -133,7 +133,7 @@ protected function getForms(): array
133133

134134
$forms['mainWidgetForm'] = $this->makeForm()->schema($this->mainComponents());
135135

136-
foreach (RainPlugin::get()->getColumnsModel()::all() as $layout) {
136+
foreach (RainPlugin::get()->getModel('Columns')::all() as $layout) {
137137
$forms['widgetsFrom' . $layout->key] = $this->makeForm()
138138
->schema($this->getBlocksForms($layout->key));
139139
}
@@ -145,7 +145,7 @@ public function submit(): Application | Redirector | \Illuminate\Contracts\Found
145145
{
146146
$widgetsData = [];
147147

148-
foreach (RainPlugin::get()->getColumnsModel()::all() as $layout) {
148+
foreach (RainPlugin::get()->getModel('Columns')::all() as $layout) {
149149
$widgetsData[$layout->key] = $this->{'widgetsFrom' . $layout->key}->getState()['widgetsData'][$layout->key];
150150
}
151151

src/Http/Livewire/Layouts.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class Layouts extends Component
1414
public function mount(string $slug = null): void
1515
{
1616
if ($slug === null) {
17-
$this->layout = RainPlugin::get()->getLayoutModel()::where('layout_slug', RainPlugin::get()->getDefaultLayout())->firstOrFail();
17+
$this->layout = RainPlugin::get()->getModel('Layout')::where('layout_slug', RainPlugin::get()->getDefaultLayout())->firstOrFail();
1818
} else {
19-
$this->layout = RainPlugin::get()->getLayoutModel()::where('layout_slug', $slug)->firstOrFail();
19+
$this->layout = RainPlugin::get()->getModel('Layout')::where('layout_slug', $slug)->firstOrFail();
2020
}
2121
}
2222

src/RainPlugin.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Filament\Contracts\Plugin;
66
use Filament\Panel;
7+
use Filament\Support\Concerns\EvaluatesClosures;
78
use LaraZeus\Rain\Filament\Resources\LayoutResource;
89

910
final class RainPlugin implements Plugin
1011
{
1112
use Configuration;
13+
use EvaluatesClosures;
1214

1315
public function getId(): string
1416
{

src/Widgets/Classes/FaqWidget.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function viewData(array $data): array
4343
{
4444
return [
4545
// @phpstan-ignore-next-line
46-
'faqs' => ($data['faq_cat'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getFaqModel()::withAnyTags([$data['faq_cat']], 'faq')->get() : null,
46+
'faqs' => ($data['faq_cat'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getModel('Faq')::withAnyTags([$data['faq_cat']], 'faq')->get() : null,
4747
];
4848
}
4949
}

src/Widgets/Classes/LibraryWidget.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function viewData(array $data): array
4343
{
4444
return [
4545
// @phpstan-ignore-next-line
46-
'library' => ($data['library_slug'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getLibraryModel()::withAnyTags([$data['library_slug']], 'library')->get() : null,
46+
'library' => ($data['library_slug'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getModel('Library')::withAnyTags([$data['library_slug']], 'library')->get() : null,
4747
];
4848
}
4949
}

stubs/ZeusWidget.php.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class {{ class }}Widget extends Widget implements \LaraZeus\Rain\Contracts\Widge
1919
Tabs\Tab::make('{{ class }}')
2020
->label(__('{{ class }}'))
2121
->schema([
22-
// todo add any filament components
22+
// add any filament components you want
2323
MarkdownEditor::make('content')
2424
->label(__('content'))
2525
->required(),

0 commit comments

Comments
 (0)