From ed200ab7338b64391662f4fa728858196c073e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 26 May 2020 13:25:24 +0200 Subject: [PATCH] Universal routes --- assets/config.php | 1 + src/Features/UniversalRoutes.php | 48 ++++++++++++++++++++++++ tests/UniversalRouteTest.php | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/Features/UniversalRoutes.php create mode 100644 tests/UniversalRouteTest.php diff --git a/assets/config.php b/assets/config.php index 8d17f8a9..de6f785e 100644 --- a/assets/config.php +++ b/assets/config.php @@ -162,6 +162,7 @@ return [ 'features' => [ // Stancl\Tenancy\Features\UserImpersonation::class, // Stancl\Tenancy\Features\TelescopeTags::class, + // Stancl\Tenancy\Features\UniversalRoutes::class, // Stancl\Tenancy\Features\TenantConfig::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-config/ // Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-redirect/ ], diff --git a/src/Features/UniversalRoutes.php b/src/Features/UniversalRoutes.php new file mode 100644 index 00000000..c0307d77 --- /dev/null +++ b/src/Features/UniversalRoutes.php @@ -0,0 +1,48 @@ +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; + } +} diff --git a/tests/UniversalRouteTest.php b/tests/UniversalRouteTest.php new file mode 100644 index 00000000..08579f23 --- /dev/null +++ b/tests/UniversalRouteTest.php @@ -0,0 +1,64 @@ + [UniversalRoutes::class]]); + + Route::get('/foo', function () { + return tenancy()->initialized + ? 'Tenancy is initialized.' + : 'Tenancy is not initialized.'; + })->middleware(['universal', InitializeTenancyByDomain::class]); + + $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.'); + } + + /** @test */ + public function making_one_route_universal_doesnt_make_all_routes_universal() + { + Route::get('/bar', function () { + return tenant('id'); + })->middleware(InitializeTenancyByDomain::class); + + $this->a_route_can_work_in_both_central_and_tenant_context(); + tenancy()->end(); + + $this->get('http://localhost/bar') + ->assertStatus(500); + + $this->get('http://acme.localhost/bar') + ->assertSuccessful() + ->assertSee('acme'); + } +} \ No newline at end of file