diff --git a/src/Router/Router.php b/src/Router/Router.php index 9e4fe818..698e597a 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -173,7 +173,7 @@ public function setPrefix(string $prefix): void * @param string $prefix * @param callable $cb * @return Router - * @throws + * @throws RouterException */ public function prefix(string $prefix, callable $cb): Router { @@ -195,17 +195,18 @@ public function prefix(string $prefix, callable $cb): Router /** * Add a domain constraint for a group of routes * - * @param string $domainPattern + * @param string $domain_pattern * @param callable $cb * @return Router + * @throws RouterException */ - public function domain(string $domainPattern): Router + public function domain(string $domain_pattern, callable $cb): Router { - $previousDomain = $this->domain; + $this->domain = $domain_pattern; - $this->domain = $domainPattern; + call_user_func_array($cb, [$this]); - $this->domain = $previousDomain; + $this->domain = null; return $this; } @@ -252,6 +253,10 @@ public function route(array $definition): void $route->middleware($definition['middleware']); } + if (isset($definition['domain'])) { + $route->withDomain($definition['domain']); + } + $route->where($where); } diff --git a/tests/Routing/RouteTest.php b/tests/Routing/RouteTest.php index 528f6d01..e4183046 100644 --- a/tests/Routing/RouteTest.php +++ b/tests/Routing/RouteTest.php @@ -217,4 +217,56 @@ public function test_angle_bracket_param_with_wildcard_in_domain() $this->assertTrue($route->match('/foo', 'app.api.example.com')); $this->assertEquals('app', $route->getParameter('sub')); } + + public function test_router_route_method_with_domain_definition() + { + $router = \Bow\Router\Router::getInstance(); + + $router->route([ + 'path' => '/api/domain-test', + 'method' => 'GET', + 'handler' => fn() => 'domain route', + 'domain' => 'api.example.com' + ]); + + $routes = $router->getRoutes(); + $route = end($routes['GET']); + + $this->assertTrue($route->match('/api/domain-test', 'api.example.com')); + $this->assertFalse($route->match('/api/domain-test', 'other.example.com')); + } + + public function test_router_route_method_with_wildcard_domain() + { + $router = \Bow\Router\Router::getInstance(); + + $router->route([ + 'path' => '/api/wildcard-domain', + 'method' => 'GET', + 'handler' => fn() => 'wildcard domain', + 'domain' => '*.example.com' + ]); + + $routes = $router->getRoutes(); + $route = end($routes['GET']); + + $this->assertTrue($route->match('/api/wildcard-domain', 'api.example.com')); + $this->assertTrue($route->match('/api/wildcard-domain', 'www.example.com')); + $this->assertFalse($route->match('/api/wildcard-domain', 'example.com')); + } + + public function test_router_domain_group_method() + { + $router = \Bow\Router\Router::getInstance(); + + $router->domain('admin.example.com', function ($router) { + $router->get('/admin/dashboard', fn() => 'admin dashboard'); + }); + + $routes = $router->getRoutes(); + $route = end($routes['GET']); + + $this->assertTrue($route->match('/admin/dashboard', 'admin.example.com')); + $this->assertFalse($route->match('/admin/dashboard', 'other.example.com')); + } }