Skip to content

Commit

Permalink
Merge pull request #28 from lara-zeus/configuration-model
Browse files Browse the repository at this point in the history
refactor configuration model
  • Loading branch information
atmonshi authored Sep 12, 2023
2 parents c094bc9 + 5a28700 commit 434e7e3
Show file tree
Hide file tree
Showing 13 changed files with 1,973 additions and 579 deletions.
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
"lara-zeus/core": "^3.0"
},
"require-dev": {
"pestphp/pest": "^2.0",
"phpunit/phpunit": "^10.2",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0 || ^8.0",
"pestphp/pest-plugin-laravel": "2.x-dev",
"pestphp/pest-plugin-livewire": "2.x-dev",
"nunomaduro/phpinsights": "^2.8",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-arch": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"pestphp/pest-plugin-livewire": "^2.1",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0"
"phpstan/phpstan-deprecation-rules": "^1.1"
},
"autoload": {
"psr-4": {
Expand All @@ -63,7 +63,8 @@
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
Expand Down
2,426 changes: 1,908 additions & 518 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ these all the available configuration, and their defaults values
RainPlugin::make()
->rainPrefix('rain')
->rainMiddleware(['web'])
->layoutModel(\LaraZeus\Rain\Models\Layout::class)
->columnsModel(\LaraZeus\Rain\Models\Columns::class)
->rainModels([
'Layout' => \LaraZeus\Rain\Models\Layout::class,
'Columns' => \LaraZeus\Rain\Models\Columns::class
])
->uploadDisk('public')
->uploadDirectory('layouts')
->navigationGroupLabel('RaRainin')
Expand Down
2 changes: 1 addition & 1 deletion resources/views/filament/pages/layouts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{ $this->mainWidgetForm }}

<div class="grid grid-cols-12 gap-2 w-full">
@foreach (\LaraZeus\Rain\RainPlugin::get()->getColumnsModel()::all() as $layout)
@foreach (\LaraZeus\Rain\RainPlugin::get()->getModel('Columns')::all() as $layout)
<x-filament::section class="w-full {{ $layout->class }} p-2">
<p>{{ $layout->name }}</p>
{{ $this->{'widgetsFrom'.$layout->key} }}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/themes/zeus/rain/layouts.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="container mx-auto">
@if($layout->widgets !== null)
<div class="grid grid-cols-1 md:grid-cols-12 gap-2 w-full px-2">
@foreach (\LaraZeus\Rain\RainPlugin::get()->getColumnsModel()::all() as $column)
@foreach (\LaraZeus\Rain\RainPlugin::get()->getModel('Columns')::all() as $column)
<div class="w-full {{ $column->class }}">
@if(isset($layout->widgets[$column->key]))
@php
Expand Down
73 changes: 36 additions & 37 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,49 @@

namespace LaraZeus\Rain;

use Closure;

trait Configuration
{
/**
* set the default path for the layout page.
*/
protected string $rainPrefix = 'rain';
protected Closure | string $rainPrefix = 'rain';

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

/**
* customize the models
* you can overwrite any model and use your own
*/
protected string $layoutModel = \LaraZeus\Rain\Models\Layout::class;

protected string $columnsModel = \LaraZeus\Rain\Models\Columns::class;
protected array $rainModels = [
'Layout' => \LaraZeus\Rain\Models\Layout::class,
'Columns' => \LaraZeus\Rain\Models\Columns::class,
];

/**
* set the default upload options.
*/
protected string $uploadDisk = 'public';
protected Closure | string $uploadDisk = 'public';

protected string $uploadDirectory = 'layouts';
protected Closure | string $uploadDirectory = 'layouts';

protected string $navigationGroupLabel = 'Rain';
protected Closure | string $navigationGroupLabel = 'Rain';

protected string $defaultLayout = 'new-page';
protected Closure | string $defaultLayout = 'new-page';

public function rainPrefix(string $prefix): static
public function rainPrefix(Closure | string $prefix): static
{
$this->rainPrefix = $prefix;

return $this;
}

public function getRainPrefix(): string
public function getRainPrefix(): Closure | string
{
return $this->rainPrefix;
return $this->evaluate($this->rainPrefix);
}

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

public function layoutModel(string $model): static
public function rainModels(array $models): static
{
$this->layoutModel = $model;
$this->rainModels = $models;

return $this;
}

public function getLayoutModel(): string
public function getRainModels(): array
{
return $this->layoutModel;
}

public function columnsModel(string $model): static
{
$this->columnsModel = $model;

return $this;
return $this->rainModels;
}

public function getColumnsModel(): string
public static function getModel(string $model): string
{
return $this->columnsModel;
return array_merge(
(new static())->rainModels,
(new static())::get()->getRainModels()
)[$model];
}

public function uploadDisk(string $disk): static
public function uploadDisk(Closure | string $disk): static
{
$this->uploadDisk = $disk;

return $this;
}

public function getUploadDisk(): string
public function getUploadDisk(): Closure | string
{
return $this->uploadDisk;
return $this->evaluate($this->uploadDisk);
}

public function uploadDirectory(string $dir): static
public function uploadDirectory(Closure | string $dir): static
{
$this->uploadDirectory = $dir;

return $this;
}

public function getUploadDirectory(): string
public function getUploadDirectory(): Closure | string
{
return $this->uploadDirectory;
return $this->evaluate($this->uploadDirectory);
}

public function navigationGroupLabel(string $lable): static
public function navigationGroupLabel(Closure | string $lable): static
{
$this->navigationGroupLabel = $lable;

return $this;
}

public function getNavigationGroupLabel(): string
public function getNavigationGroupLabel(): Closure | string
{
return $this->navigationGroupLabel;
return $this->evaluate($this->navigationGroupLabel);
}

public function defaultLayout(string $layout): static
public function defaultLayout(Closure | string $layout): static
{
$this->defaultLayout = $layout;

return $this;
}

public function getDefaultLayout(): string
public function getDefaultLayout(): Closure | string
{
return $this->defaultLayout;
return $this->evaluate($this->defaultLayout);
}
}
2 changes: 1 addition & 1 deletion src/Filament/Resources/LayoutResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LayoutResource extends Resource

public static function getModel(): string
{
return RainPlugin::get()->getLayoutModel();
return RainPlugin::get()->getModel('Layout');
}

public static function table(Table $table): Table
Expand Down
12 changes: 6 additions & 6 deletions src/Filament/Resources/LayoutResource/Pages/CreateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class CreateLayout extends Page implements Forms\Contracts\HasForms
public function mount(int $record = null): void
{
if ($record === null) {
$layoutModel = RainPlugin::get()->getLayoutModel();
$layoutModel = RainPlugin::get()->getModel('Layout');
$this->rainLayout = new $layoutModel();
foreach (RainPlugin::get()->getColumnsModel()::all() as $column) {
foreach (RainPlugin::get()->getModel('Columns')::all() as $column) {
$this->{'widgetsFrom' . $column->key}->fill([
'widgetsData.' . $column->key => [],
]);
Expand All @@ -50,10 +50,10 @@ public function mount(int $record = null): void
]);
}
} else {
$this->rainLayout = RainPlugin::get()->getLayoutModel()::findOrFail($record);
$this->rainLayout = RainPlugin::get()->getModel('Layout')::findOrFail($record);

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

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Livewire/Layouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Layouts extends Component
public function mount(string $slug = null): void
{
if ($slug === null) {
$this->layout = RainPlugin::get()->getLayoutModel()::where('layout_slug', RainPlugin::get()->getDefaultLayout())->firstOrFail();
$this->layout = RainPlugin::get()->getModel('Layout')::where('layout_slug', RainPlugin::get()->getDefaultLayout())->firstOrFail();
} else {
$this->layout = RainPlugin::get()->getLayoutModel()::where('layout_slug', $slug)->firstOrFail();
$this->layout = RainPlugin::get()->getModel('Layout')::where('layout_slug', $slug)->firstOrFail();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/RainPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;
use LaraZeus\Rain\Filament\Resources\LayoutResource;

final class RainPlugin implements Plugin
{
use Configuration;
use EvaluatesClosures;

public function getId(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Classes/FaqWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function viewData(array $data): array
{
return [
// @phpstan-ignore-next-line
'faqs' => ($data['faq_cat'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getFaqModel()::withAnyTags([$data['faq_cat']], 'faq')->get() : null,
'faqs' => ($data['faq_cat'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getModel('Faq')::withAnyTags([$data['faq_cat']], 'faq')->get() : null,
];
}
}
2 changes: 1 addition & 1 deletion src/Widgets/Classes/LibraryWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function viewData(array $data): array
{
return [
// @phpstan-ignore-next-line
'library' => ($data['library_slug'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getLibraryModel()::withAnyTags([$data['library_slug']], 'library')->get() : null,
'library' => ($data['library_slug'] !== null) ? \LaraZeus\Sky\SkyPlugin::get()->getModel('Library')::withAnyTags([$data['library_slug']], 'library')->get() : null,
];
}
}
2 changes: 1 addition & 1 deletion stubs/ZeusWidget.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class {{ class }}Widget extends Widget implements \LaraZeus\Rain\Contracts\Widge
Tabs\Tab::make('{{ class }}')
->label(__('{{ class }}'))
->schema([
// todo add any filament components
// add any filament components you want
MarkdownEditor::make('content')
->label(__('content'))
->required(),
Expand Down

0 comments on commit 434e7e3

Please sign in to comment.