Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor configuration model #151

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,23 @@
"filament/spatie-laravel-media-library-plugin": "^3.0@beta",
"filament/spatie-laravel-tags-plugin": "^3.0@beta",
"spatie/laravel-medialibrary": "^10.0.0",
"awcodes/filament-tiptap-editor": "^3.0",
"spatie/laravel-sluggable": "^3.3",
"spatie/laravel-tags": "^4.4",
"andrewdwallo/filament-selectify": "^2.0",
"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 @@ -79,7 +78,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,570 changes: 1,836 additions & 734 deletions composer.lock

Large diffs are not rendered by default.

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

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

/**
Expand Down
22 changes: 11 additions & 11 deletions database/seeders/SkySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ class SkySeeder extends Seeder
{
public function run(): void
{
SkyPlugin::get()->getTagModel()::create(['name' => ['en' => 'laravel', 'ar' => 'لارافل'], 'type' => 'category']);
SkyPlugin::get()->getTagModel()::create(['name' => ['en' => 'talks', 'ar' => 'اخبار'], 'type' => 'category']);
SkyPlugin::get()->getTagModel()::create(['name' => ['en' => 'dev', 'ar' => 'تطوير'], 'type' => 'category']);
SkyPlugin::get()->getModel('Tag')::create(['name' => ['en' => 'laravel', 'ar' => 'لارافل'], 'type' => 'category']);
SkyPlugin::get()->getModel('Tag')::create(['name' => ['en' => 'talks', 'ar' => 'اخبار'], 'type' => 'category']);
SkyPlugin::get()->getModel('Tag')::create(['name' => ['en' => 'dev', 'ar' => 'تطوير'], 'type' => 'category']);

SkyPlugin::get()->getPostModel()::factory()
SkyPlugin::get()->getModel('Post')::factory()
->count(8)
->create();

foreach (SkyPlugin::get()->getPostModel()::all() as $post) {
$random_tags = SkyPlugin::get()->getTagModel()::all()->random(1)->first()->name;
foreach (SkyPlugin::get()->getModel('Post')::all() as $post) {
$random_tags = SkyPlugin::get()->getModel('Tag')::all()->random(1)->first()->name;
$post->syncTagsWithType([$random_tags], 'category');
}

SkyPlugin::get()->getTagModel()::create(['name' => ['en' => 'support docs', 'ar' => 'الدعم الفني'], 'type' => 'library']);
SkyPlugin::get()->getTagModel()::create(['name' => ['en' => 'how to', 'ar' => 'كيف'], 'type' => 'library']);
SkyPlugin::get()->getModel('Tag')::create(['name' => ['en' => 'support docs', 'ar' => 'الدعم الفني'], 'type' => 'library']);
SkyPlugin::get()->getModel('Tag')::create(['name' => ['en' => 'how to', 'ar' => 'كيف'], 'type' => 'library']);

SkyPlugin::get()->getLibraryModel()::factory()
SkyPlugin::get()->getModel('Library')::factory()
->count(8)
->create();

foreach (SkyPlugin::get()->getLibraryModel()::all() as $library) {
$random_tags = SkyPlugin::get()->getTagModel()::getWithType('library')->random(1)->first()->name;
foreach (SkyPlugin::get()->getModel('Library')::all() as $library) {
$random_tags = SkyPlugin::get()->getModel('Tag')::getWithType('library')->random(1)->first()->name;
$library->syncTagsWithType([$random_tags], 'library');
}
}
Expand Down
12 changes: 7 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ SkyPlugin::make()
->navigationGroupLabel('Sky')

// the default models
->faqModel(\LaraZeus\Sky\Models\Faq::class)
->postModel(\LaraZeus\Sky\Models\Post::class)
->postStatusModel(\LaraZeus\Sky\Models\PostStatus::class)
->tagModel(\LaraZeus\Sky\Models\Tag::class)
->libraryModel(\LaraZeus\Sky\Models\Library::class)
->skyModels([
'Faq' => \LaraZeus\Sky\Models\Faq::class,
'Post' => \LaraZeus\Sky\Models\Post::class,
'PostStatus' => \LaraZeus\Sky\Models\PostStatus::class,
'Tag' => \LaraZeus\Sky\Models\Tag::class,
'Library' => \LaraZeus\Sky\Models\Library::class,
])

->editor(Editors\TipTapEditor::class)
->parsers([\LaraZeus\Sky\Classes\BoltParser::class])
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

Route::post('passwordConfirmation/{slug}', function ($slug) {

$post = SkyPlugin::get()->getPostModel()::query()
$post = SkyPlugin::get()->getModel('Post')::query()
->where('slug', $slug)
->where('password', request('password'))
->first();
Expand Down
6 changes: 3 additions & 3 deletions src/Classes/RenderNavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static function render(array $item, string $class = ''): string
$color = 'border-b border-b-secondary-500 text-secondary-500';

if ($item['type'] === 'page-link' || $item['type'] === 'page_link') {
$page = SkyPlugin::get()->getPostModel()::page()->find($item['data']['page_id']) ?? '';
$page = SkyPlugin::get()->getModel('Post')::page()->find($item['data']['page_id']) ?? '';
$activeClass = (request()->routeIs('page', $page)) ? $color : 'border-transparent';

return '<a class="' . $class . ' ' . $activeClass . '"
Expand All @@ -21,7 +21,7 @@ public static function render(array $item, string $class = ''): string
$item['label'] .
'</a>';
} elseif ($item['type'] === 'post-link' || $item['type'] === 'post_link') {
$post = SkyPlugin::get()->getPostModel()::find($item['data']['post_id']) ?? '';
$post = SkyPlugin::get()->getModel('Post')::find($item['data']['post_id']) ?? '';
$activeClass = (request()->routeIs('post', $post)) ? $color : 'border-transparent';

return '<a class="' . $class . ' ' . $activeClass . '"
Expand All @@ -31,7 +31,7 @@ public static function render(array $item, string $class = ''): string
$item['label'] .
'</a>';
} elseif ($item['type'] === 'library-link' || $item['type'] === 'library_link') {
$tag = SkyPlugin::get()->getTagModel()::find($item['data']['library_id']) ?? '';
$tag = SkyPlugin::get()->getModel('Tag')::find($item['data']['library_id']) ?? '';
$activeClass = (str(request()->url())->contains($tag->library->first()->slug)) ? $color : 'border-transparent';

return '<a class="' . $class . ' ' . $activeClass . '"
Expand Down
97 changes: 30 additions & 67 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace LaraZeus\Sky;

use Closure;

trait Configuration
{
/**
* set the default path for the blog homepage.
*/
protected string $skyPrefix = 'sky';
protected Closure | string $skyPrefix = 'sky';

/**
* the middleware you want to apply on all the blog routes
Expand All @@ -33,25 +35,26 @@ trait Configuration

protected bool $hasLibraryResource = true;

protected string $navigationGroupLabel = 'Sky';

protected string $faqModel = \LaraZeus\Sky\Models\Faq::class;

protected string $postModel = \LaraZeus\Sky\Models\Post::class;

protected string $postStatusModel = \LaraZeus\Sky\Models\PostStatus::class;

protected string $tagModel = \LaraZeus\Sky\Models\Tag::class;
protected Closure | string $navigationGroupLabel = 'Sky';

protected string $libraryModel = \LaraZeus\Sky\Models\Library::class;
/**
* you can overwrite any model and use your own
*/
protected array $skyModels = [
'Faq' => \LaraZeus\Sky\Models\Faq::class,
'Post' => \LaraZeus\Sky\Models\Post::class,
'PostStatus' => \LaraZeus\Sky\Models\PostStatus::class,
'Tag' => \LaraZeus\Sky\Models\Tag::class,
'Library' => \LaraZeus\Sky\Models\Library::class,
];

/**
* the default editor for pages and posts, Available:
* \LaraZeus\Sky\Editors\TipTapEditor::class,
* \LaraZeus\Sky\Editors\TinyEditor::class,
* \LaraZeus\Sky\Editors\MarkdownEditor::class,
*/
protected string $editor = Editors\TipTapEditor::class;
protected string $editor = Editors\MarkdownEditor::class;

/**
* parse the content
Expand Down Expand Up @@ -98,16 +101,16 @@ trait Configuration
'faq' => 'Faq',
];

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

return $this;
}

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

public function skyMiddleware(array $middleware): static
Expand Down Expand Up @@ -170,76 +173,36 @@ public function hasLibraryResource(): bool
return $this->hasLibraryResource;
}

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 faqModel(string $model): static
public function skyModels(array $models): static
{
$this->faqModel = $model;
$this->skyModels = $models;

return $this;
}

public function getFaqModel(): string
{
return $this->faqModel;
}

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

return $this;
}

public function getPostModel(): string
{
return $this->postModel;
}

public function postStatusModel(string $model): static
public function getSkyModels(): array
{
$this->postStatusModel = $model;

return $this;
}

public function getPostStatusModel(): string
{
return $this->postStatusModel;
}

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

return $this;
}

public function getTagModel(): string
{
return $this->tagModel;
}

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

return $this;
return $this->skyModels;
}

public function getLibraryModel(): string
public static function getModel(string $model): string
{
return $this->libraryModel;
return array_merge(
(new static())->skyModels,
(new static())::get()->getSkyModels()
)[$model];
}

public function editor(string $editor): static
Expand Down
2 changes: 1 addition & 1 deletion src/Console/migrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class migrateCommand extends Command
*/
public function handle(): void
{
$posts = SkyPlugin::get()->getPostModel()::get();
$posts = SkyPlugin::get()->getModel('Post')::get();
foreach ($posts as $post) {
$post->translatable = [];

Expand Down
5 changes: 3 additions & 2 deletions src/Editors/TipTapEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Filament\Forms\Components\Component;
use Filament\Forms\Components\Textarea;
use FilamentTiptapEditor\Enums\TiptapOutput;
use LaraZeus\Sky\Classes\ContentEditor;

class TipTapEditor implements ContentEditor
Expand All @@ -14,7 +13,8 @@ public static function component(): Component
if (class_exists(\FilamentTiptapEditor\TiptapEditor::class)) {
return \FilamentTiptapEditor\TiptapEditor::make('content')
->profile('default')
->output(TiptapOutput::Html)
// @phpstan-ignore-next-line
->output(\FilamentTiptapEditor\Enums\TiptapOutput::Html)
->extraInputAttributes(['style' => 'min-height: 24rem;'])
->required();
}
Expand All @@ -25,6 +25,7 @@ public static function component(): Component
public static function render(string $content): string
{
if (class_exists(\FilamentTiptapEditor\TiptapEditor::class)) {
// @phpstan-ignore-next-line
return tiptap_converter()->asHTML($content);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filament/Resources/FaqResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FaqResource extends SkyResource

public static function getModel(): string
{
return SkyPlugin::get()->getFaqModel();
return SkyPlugin::get()->getModel('Faq');
}

public static function getLabel(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Filament/Resources/LibraryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LibraryResource extends SkyResource

public static function getModel(): string
{
return SkyPlugin::get()->getLibraryModel();
return SkyPlugin::get()->getModel('Library');
}

public static function form(Form $form): Form
Expand Down
Loading