mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:14:04 +00:00
Universal routes
This commit is contained in:
parent
7fcfeb187c
commit
ed200ab733
3 changed files with 113 additions and 0 deletions
48
src/Features/UniversalRoutes.php
Normal file
48
src/Features/UniversalRoutes.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
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\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
class UniversalRoutes implements Feature
|
||||
{
|
||||
public static $identificationMiddlewares = [
|
||||
InitializeTenancyByDomain::class,
|
||||
];
|
||||
|
||||
public function bootstrap(Tenancy $tenancy): void
|
||||
{
|
||||
foreach (static::$identificationMiddlewares as $middleware) {
|
||||
$middleware::$onFail = function ($exception, $request, $next) {
|
||||
if (static::routeHasMiddleware($request->route(), 'universal')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
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 searhced 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue