Files
memefast/app/Console/Commands/GenerateMemesSitemap.php
2025-07-17 03:42:55 +08:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\MemeMediaService;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
class GenerateMemesSitemap extends Command
{
protected $signature = 'sitemap:generate:memes';
protected $description = 'Generate sitemap for individual meme pages (memes.show)';
public function __construct(
private MemeMediaService $memeMediaService
) {
parent::__construct();
}
public function handle(): int
{
$this->info('Starting memes sitemap generation...');
$sitemap = Sitemap::create();
// Get the base URL from config
$baseUrl = config('app.url');
// Get all enabled memes
$memes = $this->memeMediaService->getAllEnabledMemes();
$this->info("Found {$memes->count()} enabled memes");
// Add a progress bar for large datasets
$progressBar = $this->output->createProgressBar($memes->count());
$progressBar->start();
// Add each meme to the sitemap
foreach ($memes as $meme) {
$url = Url::create($baseUrl.'/meme/'.$meme->slug)
->setPriority(0.8)
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
->setLastModificationDate($meme->updated_at);
$sitemap->add($url);
$progressBar->advance();
}
$progressBar->finish();
$this->newLine();
// Save the sitemap
$sitemapPath = public_path('sitemap_memes.xml');
$sitemap->writeToFile($sitemapPath);
$this->info('Memes sitemap generated successfully!');
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info("Total URLs: {$memes->count()}");
return Command::SUCCESS;
}
}