1: <?php
2:
3: namespace LaravelUi5\Core\Controllers;
4:
5: use Flat3\Lodata\Interfaces\ServiceEndpointInterface;
6: use Illuminate\Routing\Controller;
7: use Illuminate\Support\Facades\File;
8: use LaravelUi5\Core\Contracts\Ui5ContextInterface;
9: use LaravelUi5\Core\Exceptions\MissingManifestException;
10: use LaravelUi5\Core\Ui5\Contracts\Ui5AppInterface;
11: use Symfony\Component\HttpFoundation\Response;
12:
13: class ManifestController extends Controller
14: {
15: public function __invoke(Ui5ContextInterface $context, string $slug, string $version): Response
16: {
17: /** @var Ui5AppInterface $app */
18: $app = $context->artifact();
19:
20: $manifestPath = $app->getManifestPath();
21: if (!File::exists($manifestPath)) {
22: throw new MissingManifestException($manifestPath);
23: }
24:
25: $manifest = json_decode(File::get($manifestPath), true);
26:
27: if ($app instanceof ServiceEndpointInterface) {
28: $manifest['sap.app']['dataSources'] = [
29: 'mainService' => [
30: 'uri' => $app->endpoint(),
31: 'type' => 'OData',
32: 'settings' => [
33: 'odataVersion' => '4.0'
34: ]
35: ]
36: ];
37: $manifest['sap.ui5']['models'] = [
38: '' => [
39: 'dataSource' => 'mainService',
40: 'settings' => [
41: 'operationMode' => 'Server',
42: 'earlyRequests' => true,
43: ]
44: ]
45: ];
46: }
47:
48: $manifest['laravel.ui5'] = $app->getLaravelUiManifest()->getFragment($app->getNamespace());
49:
50: return response()->json(
51: $this->emptyArraysToObjects($manifest),
52: 200,
53: [],
54: JSON_UNESCAPED_SLASHES
55: );
56: }
57:
58: private function emptyArraysToObjects(array $input): array
59: {
60: foreach ($input as $key => &$value) {
61: if (is_array($value)) {
62: $value = empty($value)
63: ? new \stdClass()
64: : $this->emptyArraysToObjects($value);
65: }
66: }
67: return $input;
68: }
69: }
70: