From 9d8f3dd207793e8c094b5a1d1378b23e6b8bf1e8 Mon Sep 17 00:00:00 2001 From: francoism90 Date: Wed, 27 Nov 2024 16:25:37 +0100 Subject: [PATCH 1/4] Make SEO optional --- composer.json | 3 ++- config/wireuse.php | 4 ++-- src/Auth/Concerns/WithAuthorization.php | 4 +++- src/Forms/Concerns/WithAttributes.php | 2 +- src/Forms/Concerns/WithSession.php | 12 ++++++++++++ src/Views/Concerns/WithSeo.php | 14 ++++++++++++++ src/WireUseServiceProvider.php | 4 ++-- 7 files changed, 36 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 5af839ce..4f3020c6 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "livewire", "utilities", "traits", + "concerns", "helpers" ], "homepage": "https://github.com/foxws/wireuse", @@ -27,7 +28,6 @@ "illuminate/view": "^10.0|^11.0", "laravel/scout": "^10.0|^11.0", "livewire/livewire": "^3.4", - "artesaos/seotools": "^1.3", "spatie/laravel-package-tools": "^1.16.5" }, "require-dev": { @@ -45,6 +45,7 @@ "spatie/php-structure-discoverer": "^2.1", "spatie/laravel-html": "^3.11", "spatie/laravel-model-states": "^2.7", + "artesaos/seotools": "^1.3", "blade-ui-kit/blade-icons": "^1.6.0" }, "autoload": { diff --git a/config/wireuse.php b/config/wireuse.php index b1bf9585..bd670822 100644 --- a/config/wireuse.php +++ b/config/wireuse.php @@ -25,7 +25,7 @@ | | This extends Laravel HTML. | - | @doc https://foxws.nl/posts/wireuse/laravel-html + | @doc https://foxws.nl/posts/wireuse-laravel-html-spatie | @doc https://spatie.be/docs/laravel-html/v3 | */ @@ -41,7 +41,7 @@ | | This controls structure discovery. | - | @doc https://foxws.nl/posts/wireuse/structure-scout + | @doc https://foxws.nl/posts/wireuse-structure-scout | @doc https://github.com/spatie/php-structure-discoverer | */ diff --git a/src/Auth/Concerns/WithAuthorization.php b/src/Auth/Concerns/WithAuthorization.php index a570cfba..783d3717 100644 --- a/src/Auth/Concerns/WithAuthorization.php +++ b/src/Auth/Concerns/WithAuthorization.php @@ -16,7 +16,9 @@ public function bootWithAuthorization(): void protected function authorizeAccess(): void { - // $this->canViewAny(Todo::class); + // $this->canViewAny(Post::class); + + // $this->canView($this->post); } protected function canViewAny(mixed $arguments): void diff --git a/src/Forms/Concerns/WithAttributes.php b/src/Forms/Concerns/WithAttributes.php index 07943ce1..d481b265 100644 --- a/src/Forms/Concerns/WithAttributes.php +++ b/src/Forms/Concerns/WithAttributes.php @@ -65,7 +65,7 @@ public function clear(bool $submit = true): void protected function keys(): array { - return $this->request()->keys(); + return array_keys($this->all()); } protected function request(): FormRequest diff --git a/src/Forms/Concerns/WithSession.php b/src/Forms/Concerns/WithSession.php index c5f01a5b..fc2b8463 100644 --- a/src/Forms/Concerns/WithSession.php +++ b/src/Forms/Concerns/WithSession.php @@ -6,6 +6,10 @@ trait WithSession { protected static bool $store = false; + /** + * Restore the form data from the session. + * This should be called in your mount() method. + */ public function restore(): void { if (! $this->useStore() || ! $this->hasStore()) { @@ -18,6 +22,10 @@ public function restore(): void ); } + /** + * Stores the form data in the session. + * This should be called in your updated() method. + */ public function store(): void { if (! $this->useStore() || ! $this->storeWhen()) { @@ -30,6 +38,10 @@ public function store(): void session()->put($this->storeId(), serialize($this->storeWith())); } + /** + * Forget the form data from the session. + * This may be called in your clear() method. + */ public function forget(): void { session()->forget($this->storeId()); diff --git a/src/Views/Concerns/WithSeo.php b/src/Views/Concerns/WithSeo.php index 72817b1a..1154d858 100644 --- a/src/Views/Concerns/WithSeo.php +++ b/src/Views/Concerns/WithSeo.php @@ -7,6 +7,15 @@ trait WithSeo { public function bootWithSeo(): void + { + if (! $this->shouldSetSeoMeta()) { + return; + } + + $this->setSeoMeta(); + } + + protected function setSeoMeta(): void { if (method_exists(static::class, 'getTitle')) { SEOMeta::setTitle($this->getTitle()); @@ -20,4 +29,9 @@ public function bootWithSeo(): void SEOMeta::setRobots($this->getRobots()); } } + + protected function shouldSetSeoMeta(): bool + { + return class_exists(SEOMeta::class); + } } diff --git a/src/WireUseServiceProvider.php b/src/WireUseServiceProvider.php index d264fc48..29cc347c 100644 --- a/src/WireUseServiceProvider.php +++ b/src/WireUseServiceProvider.php @@ -52,7 +52,7 @@ protected function registerFeatures(): static protected function registerStructureDiscovery(): static { if (! InstalledVersions::isInstalled('spatie/php-structure-discoverer')) { - abort(500, 'The spatie/php-structure-discoverer package is required to use the Structure Discovery.'); + abort(500, 'The spatie/php-structure-discoverer package is required to use Structure Discovery.'); } $this->app->singleton(ComponentScout::class, fn () => new ComponentScout); @@ -64,7 +64,7 @@ protected function registerStructureDiscovery(): static protected function registerHtmlMixins(): static { if (! InstalledVersions::isInstalled('spatie/laravel-html')) { - abort(500, 'The spatie/laravel-html package is required to use the HTML mixins.'); + abort(500, 'The spatie/laravel-html package is required to use HTML mixins.'); } $mixins = [ From e06ae26ed8070cdeded73d23da316d3571b8dfbe Mon Sep 17 00:00:00 2001 From: francoism90 Date: Wed, 27 Nov 2024 15:25:59 +0000 Subject: [PATCH 2/4] Fix styling --- src/Views/Concerns/WithSeo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Views/Concerns/WithSeo.php b/src/Views/Concerns/WithSeo.php index 1154d858..28325bf2 100644 --- a/src/Views/Concerns/WithSeo.php +++ b/src/Views/Concerns/WithSeo.php @@ -9,7 +9,7 @@ trait WithSeo public function bootWithSeo(): void { if (! $this->shouldSetSeoMeta()) { - return; + return; } $this->setSeoMeta(); From 693058bd647f74f9f8c4dc9289a069f0948609c2 Mon Sep 17 00:00:00 2001 From: francoism90 Date: Wed, 27 Nov 2024 16:34:56 +0100 Subject: [PATCH 3/4] Refactor LinkElementMixin to use attributes method for wire directives --- src/Support/Html/Mixins/LinkElementMixin.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Support/Html/Mixins/LinkElementMixin.php b/src/Support/Html/Mixins/LinkElementMixin.php index 1b7995f4..4843805f 100644 --- a/src/Support/Html/Mixins/LinkElementMixin.php +++ b/src/Support/Html/Mixins/LinkElementMixin.php @@ -14,12 +14,12 @@ public function link(): mixed $href = route($route, ...$parameters); return $this - ->attribute('wire:navigate') - ->href($href) - ->class([ - 'link', - 'link-active' => request()->routeIs($route, "{$route}.*") || request()->fullUrlIs($href), - ]); + ->attributes([ + 'wire:navigate', + 'wire:current' => 'link-active', + ]) + ->class('link') + ->href($href); }; } From 0fd961e211f5d2484ea2a7f672bbb32d6e1ea489 Mon Sep 17 00:00:00 2001 From: francoism90 Date: Wed, 27 Nov 2024 16:39:53 +0100 Subject: [PATCH 4/4] Add error handling for missing blade-ui-kit/blade-icons dependency in Icon class --- src/Support/Html/Elements/Icon.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Support/Html/Elements/Icon.php b/src/Support/Html/Elements/Icon.php index f762299c..8b0a3c57 100644 --- a/src/Support/Html/Elements/Icon.php +++ b/src/Support/Html/Elements/Icon.php @@ -10,6 +10,8 @@ class Icon extends BaseElement public function svg(?string $name = null, ?string $class = null, ?array $attributes = []): static { + throw_if(! function_exists('svg'), 'Make sure blade-ui-kit/blade-icons is installed'); + return $this->html(svg($name, $class, $attributes)->toHtml()); } }