Skip to content

Commit

Permalink
feat: generate sitemap on setup (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Feb 18, 2024
1 parent ce24da4 commit e35df08
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
24 changes: 15 additions & 9 deletions app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,34 @@ 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.
*/
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'));
}
Expand Down
22 changes: 17 additions & 5 deletions app/Console/Commands/SetupApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function handle(): void
$this->migrate();
$this->cacheConfig();
$this->scout();
$this->sitemap();
$this->cloudflare();
}
}
Expand All @@ -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');
}
}
Expand All @@ -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');
Expand All @@ -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');
}
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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');
}
}
}
36 changes: 36 additions & 0 deletions app/Console/Commands/SubmitSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class SubmitSitemap extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sitemap:submit';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Submits the sitemap to search engines.';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$sitemapUrl = Str::finish(config('app.url'), '/') . 'sitemap.xml';
$url = "https://www.google.com/webmasters/sitemaps/ping?sitemap=$sitemapUrl";
Http::get($url);
}
}
1 change: 1 addition & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
User-agent: *
Disallow:
Sitemap: /sitemap.xml

0 comments on commit e35df08

Please sign in to comment.