From 5e765634949bdf1c48a0ba9d964387edc2736223 Mon Sep 17 00:00:00 2001 From: Quentin Gabriele Date: Thu, 22 Aug 2024 22:33:29 +0200 Subject: [PATCH] better config --- config/seo.php | 4 +++- src/SeoData.php | 17 ++++++++++++++++- src/Unified/SeoUnifiedData.php | 2 +- tests/Features/SeoDataTest.php | 9 +++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/config/seo.php b/config/seo.php index 328baff..8f36531 100644 --- a/config/seo.php +++ b/config/seo.php @@ -5,9 +5,11 @@ 'title' => env('APP_NAME', 'Laravel'), + 'description' => null, + 'robots' => 'max-snippet:-1,max-image-preview:large,max-video-preview:-1', - 'sitemap' => '/sitemap.xml', + 'sitemap' => null, 'image' => null, diff --git a/src/SeoData.php b/src/SeoData.php index f0ffd37..5e12653 100644 --- a/src/SeoData.php +++ b/src/SeoData.php @@ -6,6 +6,7 @@ use Elegantly\Seo\Standard\Alternate; use Elegantly\Seo\Unified\Image; use Elegantly\Seo\Unified\SeoUnifiedData; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\URL; class SeoData extends SeoUnifiedData @@ -22,15 +23,29 @@ public function __construct( ?string $title = null, ?string $canonical = null, public ?string $description = null, + public ?Image $image = null, public ?string $robots = null, public ?string $sitemap = null, public ?array $alternates = null, - public ?Image $image = null, public ?string $locale = null, public ?array $schemas = null, public ?SeoTags $customTags = null, ) { $this->title = $title ?? config('seo.title') ?? ''; $this->canonical = $canonical ?? URL::current(); + $this->description = $description ?? config('seo.description'); + $this->robots = $robots ?? config('seo.robots'); + $this->sitemap = $sitemap ?? config('seo.sitemap'); + $this->image = $image ?? $this->getImageFromConfig(); + $this->locale = $locale ?? App::getLocale(); + } + + public function getImageFromConfig(): ?Image + { + if ($image = config('seo.image')) { + return new Image($image); + } + + return null; } } diff --git a/src/Unified/SeoUnifiedData.php b/src/Unified/SeoUnifiedData.php index 7037af3..f564043 100644 --- a/src/Unified/SeoUnifiedData.php +++ b/src/Unified/SeoUnifiedData.php @@ -26,10 +26,10 @@ public function __construct( public string $title, public string $canonical, public ?string $description = null, + public ?Image $image = null, public ?string $robots = null, public ?string $sitemap = null, public ?array $alternates = null, - public ?Image $image = null, public ?string $locale = null, public ?array $schemas = null, public ?SeoTags $customTags = null, diff --git a/tests/Features/SeoDataTest.php b/tests/Features/SeoDataTest.php index c7d2da0..4209058 100644 --- a/tests/Features/SeoDataTest.php +++ b/tests/Features/SeoDataTest.php @@ -2,6 +2,7 @@ use Elegantly\Seo\Facades\SeoManager; use Elegantly\Seo\SeoData; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\URL; it('renders default seo from config', function () { @@ -9,15 +10,19 @@ $data = new SeoData; $url = URL::current(); + $locale = App::getLocale(); + $robots = config('seo.robots'); expect( $data->toTags()->toHtml() )->toBe(implode("\n", [ 'Laravel', + '', '', '', '', '', + '', '', '', ])); @@ -28,15 +33,19 @@ $data = SeoManager::from(); $url = URL::current(); + $locale = App::getLocale(); + $robots = config('seo.robots'); expect( $data->toTags()->toHtml() )->toBe(implode("\n", [ 'Laravel', + '', '', '', '', '', + '', '', '', ]));