1: <?php
2:
3: namespace LaravelUi5\Core\Controllers;
4:
5: use Illuminate\Http\Response;
6: use Illuminate\Routing\Controller;
7: use Illuminate\Support\Facades\Blade;
8: use LaravelUi5\Core\Contracts\ExecutableInvokerInterface;
9: use LaravelUi5\Core\Contracts\Ui5ContextInterface;
10: use LaravelUi5\Core\Ui5\Capabilities\DataProviderInterface;
11: use LaravelUi5\Core\Ui5\Contracts\Ui5CardInterface;
12:
13: /**
14: * Controller for serving UI5 Card manifests with embedded data.
15: *
16: * This controller is invoked only for routes that resolve to UI5 Cards.
17: * The Ui5Context middleware guarantees that `$context->artifact` is
18: * an instance of {@see Ui5CardInterface}.
19: *
20: * Responsibilities:
21: * - Locate the Blade-based card manifest by convention
22: * (`ui5/{app}/resources/ui5/cards/{slug}.blade.php`).
23: * - Resolve the associated {@see DataProviderInterface}.
24: * - Execute the provider and render the manifest with the resulting data.
25: * - Return the compiled manifest as JSON to the client.
26: *
27: * Notes:
28: * - Cards are read-only artifacts; providers must not mutate application state.
29: * - The manifest Blade file is expected to output valid JSON.
30: * - Missing manifests result in HTTP 404; execution always returns 200,
31: * even for empty data arrays.
32: */
33: class CardController extends Controller
34: {
35: public function __invoke(Ui5ContextInterface $context, ExecutableInvokerInterface $invoker): Response
36: {
37: /** @var Ui5CardInterface $card */
38: $card = $context->artifact();
39:
40: $data = $invoker->invoke(
41: $card->getProvider(),
42: 'provide'
43: );
44:
45: $compiled = Blade::render($card->getManifest(), [
46: 'card' => $card,
47: 'data' => $data,
48: ]);
49:
50: return response($compiled, 200)->header('Content-Type', 'application/json');
51: }
52: }
53: