From a39da042af81a1b5256db829badcc5f143232a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 19 Mar 2024 20:26:38 +0100 Subject: [PATCH] Add the option to set headers and middleware in TenantAssetController --- src/Controllers/TenantAssetController.php | 28 ++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Controllers/TenantAssetController.php b/src/Controllers/TenantAssetController.php index 96fc6374..66ef55b0 100644 --- a/src/Controllers/TenantAssetController.php +++ b/src/Controllers/TenantAssetController.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace Stancl\Tenancy\Controllers; +use Closure; use Exception; +use Illuminate\Http\Request; use Illuminate\Routing\Controllers\HasMiddleware; use Illuminate\Routing\Controllers\Middleware; use Symfony\Component\HttpFoundation\BinaryFileResponse; @@ -12,22 +14,42 @@ use Throwable; class TenantAssetController implements HasMiddleware // todo@docs this was renamed from TenantAssetsController { + /** + * Used for adding custom headers to the response. + * todo@tests add a test for this + * + * @var (Closure(Request): array)|null + */ + public static Closure|null $headers; + + /** + * Additional middleware to be used on the route to this controller. + * + * @var array + */ + public static array $middleware = []; + public static function middleware() { return [ - new Middleware(tenancy()->defaultMiddleware()), + new Middleware(array_merge( + tenancy()->defaultMiddleware(), + static::$middleware, + )), ]; } /** * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ - public function __invoke(string $path = null): BinaryFileResponse + public function __invoke(Request $request, string $path = null): BinaryFileResponse { $this->validatePath($path); try { - return response()->file(storage_path("app/public/$path")); + $headers = isset(static::$headers) ? (static::$headers)($request) : []; + + return response()->file(storage_path("app/public/$path"), $headers); } catch (Throwable) { abort(404); }