1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 23:34:03 +00:00

Make universal routes work for controller middleware (#1151)

* Make universal routes work for controller middleware

* add a fallback

---------

Co-authored-by: chillbram <7299762+chillbram@users.noreply.github.com>
Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
chillbram 2024-01-25 22:34:47 +01:00 committed by GitHub
parent d268a06f5d
commit 5fe8825f13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 1 deletions

View file

@ -35,7 +35,7 @@ class UniversalRoutes implements Feature
public static function routeHasMiddleware(Route $route, $middleware): bool
{
if (in_array($middleware, $route->middleware(), true)) {
if (in_array($middleware, $route->computedMiddleware ?? $route->middleware(), true)) {
return true;
}

View file

@ -63,4 +63,46 @@ class UniversalRouteTest extends TestCase
->assertSuccessful()
->assertSee('acme');
}
/** @test */
public function universal_route_works_when_middleware_is_inserted_via_controller_middleware()
{
Route::middlewareGroup('universal', []);
config(['tenancy.features' => [UniversalRoutes::class]]);
Route::get('/foo', [UniversalRouteController::class, 'show']);
$this->get('http://localhost/foo')
->assertSuccessful()
->assertSee('Tenancy is not initialized.');
$tenant = Tenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'acme.localhost',
]);
$this->get('http://acme.localhost/foo')
->assertSuccessful()
->assertSee('Tenancy is initialized.');
}
}
class UniversalRouteController
{
public function getMiddleware()
{
return array_map(fn($middleware) => [
'middleware' => $middleware,
'options' => [],
], ['universal', InitializeTenancyByDomain::class]);
}
public function show()
{
return tenancy()->initialized
? 'Tenancy is initialized.'
: 'Tenancy is not initialized.';
}
}