64 lines
1.6 KiB
PHP
64 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Sitemaps;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Writes every document from a published release to the public disk so nginx
|
|
* can serve sitemap.xml and sitemaps/{name}.xml as plain static files,
|
|
* bypassing PHP entirely on subsequent requests.
|
|
*/
|
|
final class SitemapStaticPublisher
|
|
{
|
|
public function __construct(private readonly SitemapReleaseManager $releases)
|
|
{
|
|
}
|
|
|
|
public function enabled(): bool
|
|
{
|
|
return (bool) config('sitemaps.static_publish.enabled', true);
|
|
}
|
|
|
|
/**
|
|
* Copy all documents from the given release to the public disk.
|
|
*
|
|
* @return array{written: int, skipped: int}
|
|
*/
|
|
public function publish(string $releaseId): array
|
|
{
|
|
$manifest = $this->releases->readManifest($releaseId);
|
|
|
|
if ($manifest === null) {
|
|
return ['written' => 0, 'skipped' => 0];
|
|
}
|
|
|
|
$disk = Storage::disk($this->publicDisk());
|
|
$documents = (array) ($manifest['documents'] ?? []);
|
|
|
|
$written = 0;
|
|
$skipped = 0;
|
|
|
|
foreach ($documents as $documentName => $relativePath) {
|
|
$content = $this->releases->getDocument($releaseId, (string) $documentName);
|
|
|
|
if (! is_string($content) || $content === '') {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
|
|
$disk->put((string) $relativePath, $content);
|
|
$written++;
|
|
}
|
|
|
|
return ['written' => $written, 'skipped' => $skipped];
|
|
}
|
|
|
|
private function publicDisk(): string
|
|
{
|
|
return (string) config('sitemaps.static_publish.disk', 'sitemaps_public');
|
|
}
|
|
}
|