From e35df08983b15a7681e05a3483796dd405dde23a Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sun, 18 Feb 2024 19:42:29 +0100 Subject: [PATCH] feat: generate sitemap on setup (#45) --- app/Console/Commands/GenerateSitemap.php | 24 +++++++++------ app/Console/Commands/SetupApplication.php | 22 ++++++++++---- app/Console/Commands/SubmitSitemap.php | 36 +++++++++++++++++++++++ public/robots.txt | 1 + 4 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 app/Console/Commands/SubmitSitemap.php diff --git a/app/Console/Commands/GenerateSitemap.php b/app/Console/Commands/GenerateSitemap.php index 5580680..f17e4d9 100644 --- a/app/Console/Commands/GenerateSitemap.php +++ b/app/Console/Commands/GenerateSitemap.php @@ -14,14 +14,14 @@ class GenerateSitemap extends Command * * @var string */ - protected $signature = 'touslesprenoms:sitemap'; + protected $signature = 'sitemap:generate'; /** * The console command description. * * @var string */ - protected $description = 'Command description'; + protected $description = 'Generate the sitemap.'; /** * Execute the console command. @@ -29,13 +29,19 @@ class GenerateSitemap extends Command public function handle(): void { $sitemap = Sitemap::create(); - Name::get()->each(function (Name $name) use ($sitemap) { - $sitemap->add( - Url::create(route('name.show', $name)) - ->setPriority(0.9) - ->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY) - ); - }); + + Name::where('name', '!=', '_PRENOMS_RARES') + ->get() + ->each(function (Name $name) use ($sitemap) { + $sitemap->add( + Url::create(route('name.show', [ + 'id' => $name->id, + 'name' => $name->name, + ])) + ->setPriority(0.9) + ->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY) + ); + }); $sitemap->writeToFile(public_path('sitemap.xml')); } diff --git a/app/Console/Commands/SetupApplication.php b/app/Console/Commands/SetupApplication.php index d85be3e..6d52112 100644 --- a/app/Console/Commands/SetupApplication.php +++ b/app/Console/Commands/SetupApplication.php @@ -40,6 +40,7 @@ public function handle(): void $this->migrate(); $this->cacheConfig(); $this->scout(); + $this->sitemap(); $this->cloudflare(); } } @@ -49,7 +50,7 @@ public function handle(): void */ protected function resetCache(): void { - if (config('cache.default') != 'database' || Schema::hasTable(config('cache.stores.database.table'))) { + if (config('cache.default') !== 'database' || Schema::hasTable(config('cache.stores.database.table'))) { $this->artisan('✓ Resetting application cache', 'cache:clear'); } } @@ -59,7 +60,7 @@ protected function resetCache(): void */ protected function clearConfig(): void { - if ($this->getLaravel()->environment() == 'production') { + if ($this->getLaravel()->environment() === 'production') { $this->artisan('✓ Clear config cache', 'config:clear'); $this->artisan('✓ Resetting route cache', 'route:cache'); $this->artisan('✓ Resetting view cache', 'view:clear'); @@ -76,8 +77,8 @@ protected function clearConfig(): void protected function cacheConfig(): void { // Cache config - if ($this->getLaravel()->environment() == 'production' - && (config('cache.default') != 'database' || Schema::hasTable(config('cache.stores.database.table')))) { + if ($this->getLaravel()->environment() === 'production' + && (config('cache.default') !== 'database' || Schema::hasTable(config('cache.stores.database.table')))) { $this->artisan('✓ Cache configuraton', 'config:cache'); } } @@ -88,7 +89,7 @@ protected function cacheConfig(): void protected function symlink(): void { if ($this->option('skip-storage-link') !== true - && $this->getLaravel()->environment() != 'testing' + && $this->getLaravel()->environment() !== 'testing' && ! file_exists(public_path('storage'))) { $this->artisan('✓ Symlink the storage folder', 'storage:link'); } @@ -119,4 +120,15 @@ protected function cloudflare(): void $this->artisan('✓ Reload cloudflare ips', 'cloudflare:reload'); } } + + /** + * Setup sitemap. + */ + protected function sitemap(): void + { + if ($this->getLaravel()->environment() === 'production') { + $this->artisan('✓ Generate sitemap', 'sitemap:generate'); + $this->artisan('✓ Submit sitemap', 'sitemap:submit'); + } + } } diff --git a/app/Console/Commands/SubmitSitemap.php b/app/Console/Commands/SubmitSitemap.php new file mode 100644 index 0000000..79c6f4f --- /dev/null +++ b/app/Console/Commands/SubmitSitemap.php @@ -0,0 +1,36 @@ +