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\Ui5Context;
8: use LaravelUi5\Core\Ui5\Contracts\Ui5ReportInterface;
9: use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10:
11: class ReportResourceController
12: {
13: public function __invoke(Ui5Context $context, string $slug, string $extension): Response
14: {
15: /** @var Ui5ReportInterface $report */
16: $report = $context->artifact;
17:
18: if (null === $report) {
19: throw new NotFoundHttpException("Report with key [$slug] not found.");
20: }
21:
22: $path = match ($extension) {
23: 'js' => $report->getSelectionControllerPath(),
24: 'xml' => $report->getSelectionViewPath(),
25: default => throw new NotFoundHttpException("Unsupported file extension [$extension]."),
26: };
27:
28: if (!is_file($path)) {
29: throw new NotFoundHttpException("Report file [$path] not found.");
30: }
31:
32: $mime = match ($extension) {
33: 'js' => 'application/javascript',
34: 'xml' => 'application/xml',
35: default => 'text/plain',
36: };
37:
38: return response(File::get($path), 200, [
39: 'Content-Type' => $mime,
40: ]);
41: }
42: }
43: