1: <?php
2:
3: namespace LaravelUi5\Core\Controllers;
4:
5: use Illuminate\Routing\Controller;
6: use LaravelUi5\Core\Contracts\Ui5Context;
7: use LaravelUi5\Core\Exceptions\MissingAssetException;
8: use LaravelUi5\Core\Ui5\Contracts\HasAssetsInterface;
9: use Symfony\Component\HttpFoundation\BinaryFileResponse;
10:
11: /**
12: * Delivers static assets for UI5 applications or libraries (e.g., JS, CSS, i18n).
13: *
14: * Assets are resolved via the Ui5Registry based on artifact slug and type.
15: * The version segment is accepted in the URL for cache-busting but currently
16: * ignored in path resolution.
17: *
18: * Example routes:
19: * - GET /app/users/1.0.0/Component.js
20: * - GET /lib/core/1.0.0/resources/i18n/i18n.properties
21: */
22: class AssetController extends Controller
23: {
24: public function __invoke(
25: Ui5Context $context,
26: string $type,
27: string $slug,
28: string $version,
29: string $file
30: ): BinaryFileResponse
31: {
32:
33: $artifact = $context->artifact;
34:
35: if ($artifact instanceof HasAssetsInterface) {
36: $path = $artifact->getAssetPath($file);
37:
38: if ($path && file_exists($path)) {
39: $mime = match (true) {
40: str_ends_with($file, '.js') => 'application/javascript',
41: str_ends_with($file, '.js.map') => 'application/json',
42: str_ends_with($file, '.json') => 'application/json',
43: str_ends_with($file, '.css') => 'text/css',
44: str_ends_with($file, '.properties') => 'text/plain; charset=utf-8',
45: default => 'application/octet-stream',
46: };
47:
48: return response()->file($path, ['Content-Type' => $mime]);
49: }
50: }
51:
52: throw new MissingAssetException($type, $slug, $version, $file);
53: }
54: }
55: