mirror of
https://github.com/archtechx/tenancy.git
synced 2026-08-06 05:54:03 +00:00
universal routes capture existing onfail callback (fix #1470)
This commit is contained in:
parent
ab64f4599d
commit
b0d8318d0e
2 changed files with 26 additions and 1 deletions
|
|
@ -23,11 +23,18 @@ class UniversalRoutes implements Feature
|
||||||
public function bootstrap(Tenancy $tenancy): void
|
public function bootstrap(Tenancy $tenancy): void
|
||||||
{
|
{
|
||||||
foreach (static::$identificationMiddlewares as $middleware) {
|
foreach (static::$identificationMiddlewares as $middleware) {
|
||||||
$middleware::$onFail = function ($exception, $request, $next) {
|
// preserve any onFail callback set before universal routes bootstraps
|
||||||
|
$customOnFail = $middleware::$onFail;
|
||||||
|
|
||||||
|
$middleware::$onFail = function ($exception, $request, $next) use ($customOnFail) {
|
||||||
if (static::routeHasMiddleware($request->route(), static::$middlewareGroup)) {
|
if (static::routeHasMiddleware($request->route(), static::$middlewareGroup)) {
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($customOnFail instanceof Closure) {
|
||||||
|
return $customOnFail($exception, $request, $next);
|
||||||
|
}
|
||||||
|
|
||||||
throw $exception;
|
throw $exception;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,24 @@ class UniversalRouteTest extends TestCase
|
||||||
->assertSuccessful()
|
->assertSuccessful()
|
||||||
->assertSee('Tenancy is initialized.');
|
->assertSee('Tenancy is initialized.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function universal_route_runs_custom_failed_initialization_override()
|
||||||
|
{
|
||||||
|
InitializeTenancyByDomain::$onFail = function () {
|
||||||
|
return 'custom failed logic.';
|
||||||
|
};
|
||||||
|
|
||||||
|
Route::middlewareGroup('universal', []);
|
||||||
|
config(['tenancy.features' => [UniversalRoutes::class]]);
|
||||||
|
|
||||||
|
Route::get('/foo', fn () => 'Tenant route')
|
||||||
|
->middleware([InitializeTenancyByDomain::class]);
|
||||||
|
|
||||||
|
$this->get('http://acme.localhost/foo')
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertSee('custom failed logic.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UniversalRouteController
|
class UniversalRouteController
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue