Skip to content

Commit 5133def

Browse files
authored
Merge pull request #363 from bowphp/refactor/code-base
Fix domain definition
2 parents 6d0cb9c + 8e6a28a commit 5133def

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

src/Router/Router.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function setPrefix(string $prefix): void
173173
* @param string $prefix
174174
* @param callable $cb
175175
* @return Router
176-
* @throws
176+
* @throws RouterException
177177
*/
178178
public function prefix(string $prefix, callable $cb): Router
179179
{
@@ -195,17 +195,18 @@ public function prefix(string $prefix, callable $cb): Router
195195
/**
196196
* Add a domain constraint for a group of routes
197197
*
198-
* @param string $domainPattern
198+
* @param string $domain_pattern
199199
* @param callable $cb
200200
* @return Router
201+
* @throws RouterException
201202
*/
202-
public function domain(string $domainPattern): Router
203+
public function domain(string $domain_pattern, callable $cb): Router
203204
{
204-
$previousDomain = $this->domain;
205+
$this->domain = $domain_pattern;
205206

206-
$this->domain = $domainPattern;
207+
call_user_func_array($cb, [$this]);
207208

208-
$this->domain = $previousDomain;
209+
$this->domain = null;
209210

210211
return $this;
211212
}
@@ -252,6 +253,10 @@ public function route(array $definition): void
252253
$route->middleware($definition['middleware']);
253254
}
254255

256+
if (isset($definition['domain'])) {
257+
$route->withDomain($definition['domain']);
258+
}
259+
255260
$route->where($where);
256261
}
257262

tests/Routing/RouteTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,56 @@ public function test_angle_bracket_param_with_wildcard_in_domain()
217217
$this->assertTrue($route->match('/foo', 'app.api.example.com'));
218218
$this->assertEquals('app', $route->getParameter('sub'));
219219
}
220+
221+
public function test_router_route_method_with_domain_definition()
222+
{
223+
$router = \Bow\Router\Router::getInstance();
224+
225+
$router->route([
226+
'path' => '/api/domain-test',
227+
'method' => 'GET',
228+
'handler' => fn() => 'domain route',
229+
'domain' => 'api.example.com'
230+
]);
231+
232+
$routes = $router->getRoutes();
233+
$route = end($routes['GET']);
234+
235+
$this->assertTrue($route->match('/api/domain-test', 'api.example.com'));
236+
$this->assertFalse($route->match('/api/domain-test', 'other.example.com'));
237+
}
238+
239+
public function test_router_route_method_with_wildcard_domain()
240+
{
241+
$router = \Bow\Router\Router::getInstance();
242+
243+
$router->route([
244+
'path' => '/api/wildcard-domain',
245+
'method' => 'GET',
246+
'handler' => fn() => 'wildcard domain',
247+
'domain' => '*.example.com'
248+
]);
249+
250+
$routes = $router->getRoutes();
251+
$route = end($routes['GET']);
252+
253+
$this->assertTrue($route->match('/api/wildcard-domain', 'api.example.com'));
254+
$this->assertTrue($route->match('/api/wildcard-domain', 'www.example.com'));
255+
$this->assertFalse($route->match('/api/wildcard-domain', 'example.com'));
256+
}
257+
258+
public function test_router_domain_group_method()
259+
{
260+
$router = \Bow\Router\Router::getInstance();
261+
262+
$router->domain('admin.example.com', function ($router) {
263+
$router->get('/admin/dashboard', fn() => 'admin dashboard');
264+
});
265+
266+
$routes = $router->getRoutes();
267+
$route = end($routes['GET']);
268+
269+
$this->assertTrue($route->match('/admin/dashboard', 'admin.example.com'));
270+
$this->assertFalse($route->match('/admin/dashboard', 'other.example.com'));
271+
}
220272
}

0 commit comments

Comments
 (0)