1: <?php
2:
3: namespace LaravelUi5\Core\Controllers;
4:
5: use Illuminate\Http\Response;
6: use Illuminate\Support\Facades\File;
7: use LaravelUi5\Core\Contracts\Ui5ContextInterface;
8: use LaravelUi5\Core\Ui5\Contracts\Ui5ReportInterface;
9: use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10:
11: class ReportResourceController
12: {
13: public function __invoke(Ui5ContextInterface $context, string $slug, string $extension): Response
14: {
15: /** @var Ui5ReportInterface $report */
16: $report = $context->artifact();
17:
18: $path = match ($extension) {
19: 'js' => $report->getSelectionControllerPath(),
20: 'xml' => $report->getSelectionViewPath(),
21: default => throw new NotFoundHttpException("Unsupported file extension [$extension]."),
22: };
23:
24: if (!is_file($path)) {
25: throw new NotFoundHttpException("Report file [$path] not found.");
26: }
27:
28: $mime = match ($extension) {
29: 'js' => 'application/javascript',
30: 'xml' => 'application/xml',
31: default => 'text/plain',
32: };
33:
34: return response(File::get($path), 200, [
35: 'Content-Type' => $mime,
36: ]);
37: }
38: }
39: