Skip to content

Commit

Permalink
Make universal routes work for controller middleware (#1151)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Jan 25, 2024
1 parent d268a06 commit 5fe8825
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Features/UniversalRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function bootstrap(Tenancy $tenancy): void

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;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/UniversalRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,46 @@ public function making_one_route_universal_doesnt_make_all_routes_universal()
->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.';
}
}

0 comments on commit 5fe8825

Please sign in to comment.