| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Middleware; |
| 4: | |
| 5: | use Closure; |
| 6: | use Flat3\Lodata\Endpoint; |
| 7: | use Flat3\Lodata\Model; |
| 8: | use Illuminate\Http\Request; |
| 9: | use Illuminate\Support\Facades\App; |
| 10: | use LaravelUi5\Core\Exceptions\InvalidODataException; |
| 11: | use LaravelUi5\Core\Exceptions\MissingArtifactException; |
| 12: | use LaravelUi5\Core\Exceptions\UndefinedEndpointException; |
| 13: | use LaravelUi5\Core\Ui5\Contracts\Ui5RegistryInterface; |
| 14: | |
| 15: | final class ResolveODataEndpoint |
| 16: | { |
| 17: | public function __construct( |
| 18: | protected Ui5RegistryInterface $registry, |
| 19: | ) |
| 20: | { |
| 21: | } |
| 22: | |
| 23: | public function handle(Request $request, Closure $next) |
| 24: | { |
| 25: | $namespace = $request->route('namespace'); |
| 26: | $version = $request->route('version'); |
| 27: | |
| 28: | if (!$namespace || !$version) { |
| 29: | throw new InvalidODataException(); |
| 30: | } |
| 31: | |
| 32: | $key = $this->registry->pathToNamespace($namespace); |
| 33: | |
| 34: | $artifact = $this->registry->get($key); |
| 35: | |
| 36: | if (!$artifact) { |
| 37: | throw new MissingArtifactException($key); |
| 38: | } |
| 39: | |
| 40: | if (!$artifact instanceof Endpoint) { |
| 41: | throw new UndefinedEndpointException($key); |
| 42: | } |
| 43: | |
| 44: | App::instance(Endpoint::class, $artifact); |
| 45: | |
| 46: | |
| 47: | $model = $artifact->discover(new Model()); |
| 48: | |
| 49: | App::instance(Model::class, $model); |
| 50: | |
| 51: | return $next($request); |
| 52: | } |
| 53: | } |
| 54: | |