Skip to content

Commit

Permalink
Merge pull request #71 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 921cd23 + 8b38fdf commit 6745232
Show file tree
Hide file tree
Showing 14 changed files with 1,931 additions and 535 deletions.
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,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 @@ -64,7 +64,8 @@
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"minimum-stability": "dev",
Expand Down
2,333 changes: 1,861 additions & 472 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion database/factories/DepartmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DepartmentFactory extends Factory
{
public function getModel(): string
{
return WindPlugin::get()->getDepartmentModel();
return WindPlugin::get()->getModel('Department');
}

public function definition(): array
Expand Down
2 changes: 1 addition & 1 deletion database/factories/LetterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LetterFactory extends Factory
{
public function getModel(): string
{
return WindPlugin::get()->getDepartmentModel();
return WindPlugin::get()->getModel('Department');
}

public function definition(): array
Expand Down
4 changes: 2 additions & 2 deletions database/seeders/WindSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class WindSeeder extends Seeder
{
public function run(): void
{
WindPlugin::get()->getDepartmentModel()::factory()
WindPlugin::get()->getModel('Department')::factory()
->has(
WindPlugin::get()->getLetterModel()::factory()
WindPlugin::get()->getModel('Letter')::factory()
->count(5)
->state(function (array $attributes, Department $department) {
return [
Expand Down
6 changes: 4 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ WindPlugin::make()
->defaultDepartmentId(1)
->defaultStatus('NEW')
->departmentResource()
->departmentModel(\LaraZeus\Wind\Models\Department::class)
->letterModel(\LaraZeus\Wind\Models\Letter::class)
->windModels([
'Department' => \LaraZeus\Wind\Models\Department::class,
'Letter' => \LaraZeus\Wind\Models\Letter::class,
])
->uploadDisk('public')
->uploadDirectory('logos')
->navigationGroupLabel('Wind'),
Expand Down
2 changes: 1 addition & 1 deletion resources/views/themes/zeus/wind/departments.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
>
<div x-data="{ state: $wire.entangle('{{ $getStatePath() }}').defer }">
@if(\LaraZeus\Wind\WindPlugin::get()->hasDepartmentResource())
@php $departments = \LaraZeus\Wind\WindPlugin::get()->getDepartmentModel()::departments()->get(); @endphp
@php $departments = \LaraZeus\Wind\WindPlugin::get()->getModel('Department')::departments()->get(); @endphp
@if($departments->isEmpty())
<x-filament::section>
<div class="text-red-400">
Expand Down
80 changes: 41 additions & 39 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace LaraZeus\Wind;

use Closure;

trait Configuration
{
/**
* set the default path for the contact form homepage.
*/
protected string $windPrefix = 'wind';
protected Closure | string $windPrefix = 'wind';

/**
* the middleware you want to apply on the contact routes
Expand All @@ -17,7 +19,7 @@ trait Configuration
/**
* you can set a default department to receive all messages, if the user didn't chose one.
*/
protected int $defaultDepartmentId = 1;
protected Closure | int $defaultDepartmentId = 1;

/**
* set the default status that all messages will have when received.
Expand All @@ -26,26 +28,30 @@ trait Configuration

protected bool $hasDepartmentResource = true;

protected string $departmentModel = \LaraZeus\Wind\Models\Department::class;

protected string $letterModel = \LaraZeus\Wind\Models\Letter::class;
/**
* you can overwrite any model and use your own
*/
protected array $windModels = [
'Department' => \LaraZeus\Wind\Models\Department::class,
'Letter' => \LaraZeus\Wind\Models\Letter::class,
];

protected string $uploadDisk = 'public';
protected Closure | string $uploadDisk = 'public';

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

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

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

return $this;
}

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

public function windMiddleware(array $middleware): static
Expand All @@ -72,16 +78,16 @@ public function getDefaultStatus(): string
return $this->defaultStatus;
}

public function defaultDepartmentId(int $defaultDepartment): static
public function defaultDepartmentId(Closure | int $defaultDepartment): static
{
$this->defaultDepartmentId = $defaultDepartment;

return $this;
}

public function getDefaultDepartmentId(): int
public function getDefaultDepartmentId(): Closure | int
{
return $this->defaultDepartmentId;
return $this->evaluate($this->defaultDepartmentId);
}

public function departmentResource(bool $condition = true): static
Expand All @@ -96,63 +102,59 @@ public function hasDepartmentResource(): bool
return $this->hasDepartmentResource;
}

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

return $this;
}

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

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

return $this;
}

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

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

return $this;
}

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

public function uploadDirectory(string $dir): static
public function windModels(array $models): static
{
$this->uploadDirectory = $dir;
$this->windModels = $models;

return $this;
}

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

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

return $this;
return $this->windModels;
}

public function getNavigationGroupLabel(): string
public static function getModel(string $model): string
{
return $this->navigationGroupLabel;
return array_merge(
(new static())->windModels,
(new static())::get()->getWindModels()
)[$model];
}
}
2 changes: 1 addition & 1 deletion src/Filament/Resources/DepartmentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DepartmentResource extends Resource

public static function getModel(): string
{
return WindPlugin::get()->getDepartmentModel();
return WindPlugin::get()->getModel('Department');
}

public static function getNavigationBadge(): ?string
Expand Down
6 changes: 3 additions & 3 deletions src/Filament/Resources/LetterResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LetterResource extends Resource
{
public static function getModel(): string
{
return WindPlugin::get()->getLetterModel();
return WindPlugin::get()->getModel('Letter');
}

protected static ?string $navigationIcon = 'heroicon-o-inbox';
Expand Down Expand Up @@ -102,7 +102,7 @@ public static function form(Form $form): Form
->schema([
Select::make('department_id')
->label(__('department'))
->options(WindPlugin::get()->getDepartmentModel()::pluck('name', 'id'))
->options(WindPlugin::get()->getModel('Department')::pluck('name', 'id'))
->required()
->visible(fn (): bool => WindPlugin::get()->hasDepartmentResource()),
TextInput::make('status')
Expand Down Expand Up @@ -207,7 +207,7 @@ public static function table(Table $table): Table
->label(__('status')),
SelectFilter::make('department_id')
->visible(fn (): bool => WindPlugin::get()->hasDepartmentResource())
->options(WindPlugin::get()->getDepartmentModel()::pluck('name', 'id'))
->options(WindPlugin::get()->getModel('Department')::pluck('name', 'id'))
->label(__('department')),
])
->actions([
Expand Down
6 changes: 3 additions & 3 deletions src/Livewire/ContactsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function mount(string $departmentSlug = null): void
{
if (WindPlugin::get()->hasDepartmentResource()) {
if ($departmentSlug !== null) {
$this->department = WindPlugin::get()->getDepartmentModel()::where('slug', $departmentSlug)->first();
$this->department = WindPlugin::get()->getModel('Department')::where('slug', $departmentSlug)->first();
} elseif (WindPlugin::get()->getDefaultDepartmentId() !== null) {
$this->department = WindPlugin::get()->getDepartmentModel()::find(WindPlugin::get()->getDefaultDepartmentId());
$this->department = WindPlugin::get()->getModel('Department')::find(WindPlugin::get()->getDefaultDepartmentId());
}
}

Expand All @@ -61,7 +61,7 @@ public function mount(string $departmentSlug = null): void

public function store(): void
{
$letter = WindPlugin::get()->getLetterModel()::create($this->form->getState());
$letter = WindPlugin::get()->getModel('Letter')::create($this->form->getState());
$this->sent = true;
LetterSent::dispatch($letter);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Department.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected static function newFactory(): DepartmentFactory
/** @phpstan-return hasMany<Letter> */
public function letters(): HasMany
{
return $this->hasMany(WindPlugin::get()->getDepartmentModel());
return $this->hasMany(WindPlugin::get()->getModel('Department'));
}

public function image(): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Letter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected static function newFactory(): LetterFactory
/** @return BelongsTo<Letter, Department> */
public function department(): BelongsTo
{
return $this->belongsTo(WindPlugin::get()->getDepartmentModel());
return $this->belongsTo(WindPlugin::get()->getModel('Department'));
}

public function getReplyTitleAttribute(): string
Expand Down
2 changes: 2 additions & 0 deletions src/WindPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;
use LaraZeus\Wind\Filament\Resources\DepartmentResource;
use LaraZeus\Wind\Filament\Resources\LetterResource;

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

public function getId(): string
{
Expand Down

0 comments on commit 6745232

Please sign in to comment.