Wire admin studio SSR and search infrastructure
This commit is contained in:
@@ -4,58 +4,61 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\Sitemaps\SitemapBuildService;
|
||||
use App\Services\Sitemaps\PublishedSitemapResolver;
|
||||
use App\Services\Sitemaps\SitemapXmlRenderer;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
final class SitemapController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SitemapBuildService $build,
|
||||
private readonly PublishedSitemapResolver $published,
|
||||
private readonly SitemapXmlRenderer $renderer,
|
||||
) {
|
||||
}
|
||||
|
||||
public function index(): Response
|
||||
public function index(): Response|BinaryFileResponse
|
||||
{
|
||||
if ((bool) config('sitemaps.delivery.prefer_published_release', true)) {
|
||||
$published = $this->published->resolveIndex();
|
||||
if ($published !== null) {
|
||||
return $this->renderer->xmlResponse($published['content']);
|
||||
}
|
||||
// 1. Static file written by the build/generate commands.
|
||||
// On production nginx serves this directly via try_files without reaching PHP.
|
||||
// On dev / misconfigured servers we stream it with sendfile — no RAM load.
|
||||
$path = public_path('sitemap.xml');
|
||||
if (file_exists($path)) {
|
||||
return $this->xmlFileResponse($path);
|
||||
}
|
||||
|
||||
abort_unless((bool) config('sitemaps.delivery.fallback_to_live_build', true), 404);
|
||||
// 2. Published release (release management pipeline fallback).
|
||||
$published = $this->published->resolveIndex();
|
||||
if ($published !== null) {
|
||||
return $this->renderer->xmlResponse($published['content']);
|
||||
}
|
||||
|
||||
$built = $this->build->buildIndex(
|
||||
force: false,
|
||||
persist: (bool) config('sitemaps.refresh.build_on_request', true),
|
||||
);
|
||||
|
||||
return $this->renderer->xmlResponse($built['content']);
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
public function show(string $name): Response
|
||||
public function show(string $name): Response|BinaryFileResponse
|
||||
{
|
||||
if ((bool) config('sitemaps.delivery.prefer_published_release', true)) {
|
||||
$published = $this->published->resolveNamed($name);
|
||||
if ($published !== null) {
|
||||
return $this->renderer->xmlResponse($published['content']);
|
||||
}
|
||||
// 1. Static file.
|
||||
$path = public_path('sitemaps/' . $name . '.xml');
|
||||
if (file_exists($path)) {
|
||||
return $this->xmlFileResponse($path);
|
||||
}
|
||||
|
||||
abort_unless((bool) config('sitemaps.delivery.fallback_to_live_build', true), 404);
|
||||
// 2. Published release.
|
||||
$published = $this->published->resolveNamed($name);
|
||||
if ($published !== null) {
|
||||
return $this->renderer->xmlResponse($published['content']);
|
||||
}
|
||||
|
||||
$built = $this->build->buildNamed(
|
||||
$name,
|
||||
force: false,
|
||||
persist: (bool) config('sitemaps.refresh.build_on_request', true),
|
||||
);
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
abort_if($built === null, 404);
|
||||
|
||||
return $this->renderer->xmlResponse($built['content']);
|
||||
private function xmlFileResponse(string $absolutePath): BinaryFileResponse
|
||||
{
|
||||
return response()->file($absolutePath, [
|
||||
'Content-Type' => 'application/xml; charset=UTF-8',
|
||||
'Cache-Control' => 'public, max-age=' . max(60, (int) config('sitemaps.cache_ttl_seconds', 900)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user