| 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\Ui5Context; |
| 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(Ui5Context $context, string $slug, string $version): Response |
| 16: | { |
| 17: | |
| 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: | ] |
| 43: | ] |
| 44: | ]; |
| 45: | } |
| 46: | |
| 47: | $manifest['laravel.ui5'] = $app->getLaravelUiManifest()->getFragment($slug); |
| 48: | |
| 49: | return response()->json( |
| 50: | $this->emptyArraysToObjects($manifest), |
| 51: | 200, |
| 52: | [], |
| 53: | JSON_UNESCAPED_SLASHES |
| 54: | ); |
| 55: | } |
| 56: | |
| 57: | private function emptyArraysToObjects(array $input): array |
| 58: | { |
| 59: | foreach ($input as $key => &$value) { |
| 60: | if (is_array($value)) { |
| 61: | $value = empty($value) |
| 62: | ? new \stdClass() |
| 63: | : $this->emptyArraysToObjects($value); |
| 64: | } |
| 65: | } |
| 66: | return $input; |
| 67: | } |
| 68: | } |
| 69: | |