| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Middleware; |
| 4: | |
| 5: | use Closure; |
| 6: | use Illuminate\Http\Request; |
| 7: | use LaravelUi5\Core\Contracts\Ui5Context; |
| 8: | use LaravelUi5\Core\Exceptions\OutdatedVersionException; |
| 9: | |
| 10: | /** |
| 11: | * Ensures that only the latest version of a UI5 app artifact is accessible. |
| 12: | * |
| 13: | * This middleware checks whether the requested version of the UI5 application |
| 14: | * (as provided in the route parameters) matches the currently registered version |
| 15: | * in the resolved Ui5RuntimeContext. If the version does not match, a 410 Gone |
| 16: | * response is returned to indicate that the requested artifact version is no longer available. |
| 17: | * |
| 18: | * Note: This middleware requires that the Ui5RuntimeContext has already been resolved |
| 19: | * and registered in the service container. It should therefore be placed |
| 20: | * **after** the `ResolveUi5RuntimeContext` middleware in the route or middleware stack. |
| 21: | * |
| 22: | * This middleware is **not applied by default** and should be explicitly included |
| 23: | * for routes that require strict version enforcement. |
| 24: | * |
| 25: | * @package LaravelUi5\Core\Middleware |
| 26: | */ |
| 27: | class EnsureFrontendVersionIsLatest |
| 28: | { |
| 29: | public function handle(Request $request, Closure $next) |
| 30: | { |
| 31: | /** @var Ui5Context|null $context */ |
| 32: | $context = app(Ui5Context::class); |
| 33: | |
| 34: | if ($context && $context->artifact && $request->route('version')) { |
| 35: | $requestedVersion = $request->route('version'); |
| 36: | $registeredVersion = $context->artifact->getVersion(); |
| 37: | |
| 38: | if ($requestedVersion !== $registeredVersion) { |
| 39: | throw new OutdatedVersionException( |
| 40: | $context->artifact->getNamespace(), |
| 41: | $requestedVersion, |
| 42: | $registeredVersion |
| 43: | ); |
| 44: | } |
| 45: | } |
| 46: | |
| 47: | return $next($request); |
| 48: | } |
| 49: | } |
| 50: |