From fdd401fc8f8ffe0d1c96da76a57b193532392c37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 27 Mar 2024 16:44:17 +0100 Subject: [PATCH] Fix tenant asset controller middleware() logic --- src/Controllers/TenantAssetController.php | 19 +++++++++++-------- tests/TenantAssetTest.php | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Controllers/TenantAssetController.php b/src/Controllers/TenantAssetController.php index ece0d9ef..add14545 100644 --- a/src/Controllers/TenantAssetController.php +++ b/src/Controllers/TenantAssetController.php @@ -12,14 +12,14 @@ use Illuminate\Routing\Controllers\Middleware; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Throwable; -class TenantAssetController implements HasMiddleware // todo@docs this was renamed from TenantAssetsController +class TenantAssetController implements HasMiddleware { /** * Used for adding custom headers to the response. * - * @var (Closure(Request): array)|null + * @var (Closure(Request): array)|array */ - public static Closure|null $headers; + public static Closure|array $headers = []; /** * Additional middleware to be used on the route to this controller. @@ -30,12 +30,13 @@ class TenantAssetController implements HasMiddleware // todo@docs this was renam public static function middleware() { - return [ - new Middleware(array_merge( + return array_map( + fn ($middleware) => new Middleware($middleware), + array_merge( [tenancy()->defaultMiddleware()], static::$middleware, - )), - ]; + ), + ); } /** @@ -46,7 +47,9 @@ class TenantAssetController implements HasMiddleware // todo@docs this was renam $this->validatePath($path); try { - $headers = isset(static::$headers) ? (static::$headers)($request) : []; + $headers = static::$headers instanceof Closure + ? (static::$headers)($request) + : static::$headers; return response()->file(storage_path("app/public/$path"), $headers); } catch (Throwable) { diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 0299af78..72612aa0 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -25,6 +25,7 @@ beforeEach(function () { TenancyUrlGenerator::$prefixRouteNames = false; TenancyUrlGenerator::$passTenantParameterToRoutes = true; + TenantAssetController::$headers = []; /** @var CloneRoutesAsTenant $cloneAction */ $cloneAction = app(CloneRoutesAsTenant::class); @@ -137,7 +138,7 @@ test('TenantAssetController headers are configurable', function () { $response->assertSuccessful(); $response->assertHeader('X-Foo', 'Bar'); - TenantAssetController::$headers = null; // reset static property + TenantAssetController::$headers = []; // reset static property }); test('global asset helper returns the same url regardless of tenancy initialization', function () {