mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:34:04 +00:00
* feat(UniversalRoutes): Stop overwriting the (maybe) customized onFail method and just call it in case of an exception * throw correct exception when `$originalOnFail()` is null * Update DomainTest.php * convert test to pest and renamed * Update tests/DomainTest.php Co-authored-by: Samuel Štancl <samuel@archte.ch> Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com> Co-authored-by: Samuel Štancl <samuel@archte.ch>
59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Features;
|
|
|
|
use Closure;
|
|
use Illuminate\Routing\Route;
|
|
use Illuminate\Support\Facades\Route as Router;
|
|
use Stancl\Tenancy\Contracts\Feature;
|
|
use Stancl\Tenancy\Middleware;
|
|
use Stancl\Tenancy\Tenancy;
|
|
|
|
class UniversalRoutes implements Feature
|
|
{
|
|
public static $middlewareGroup = 'universal';
|
|
|
|
public static $identificationMiddlewares = [
|
|
Middleware\InitializeTenancyByDomain::class,
|
|
Middleware\InitializeTenancyBySubdomain::class,
|
|
];
|
|
|
|
public function bootstrap(Tenancy $tenancy): void
|
|
{
|
|
foreach (static::$identificationMiddlewares as $middleware) {
|
|
$originalOnFail = $middleware::$onFail;
|
|
|
|
$middleware::$onFail = function ($exception, $request, $next) use ($originalOnFail) {
|
|
if (static::routeHasMiddleware($request->route(), static::$middlewareGroup)) {
|
|
return $next($request);
|
|
}
|
|
|
|
if ($originalOnFail) {
|
|
return $originalOnFail($exception, $request, $next);
|
|
}
|
|
|
|
throw $exception;
|
|
};
|
|
}
|
|
}
|
|
|
|
public static function routeHasMiddleware(Route $route, $middleware): bool
|
|
{
|
|
if (in_array($middleware, $route->middleware(), true)) {
|
|
return true;
|
|
}
|
|
|
|
// Loop one level deep and check if the route's middleware
|
|
// groups have the searched middleware group inside them
|
|
$middlewareGroups = Router::getMiddlewareGroups();
|
|
foreach ($route->gatherMiddleware() as $inner) {
|
|
if (! $inner instanceof Closure && isset($middlewareGroups[$inner]) && in_array($middleware, $middlewareGroups[$inner], true)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|