Skip to content

Commit f44e979

Browse files
committed
new method with isolated matching logic
1 parent 8037162 commit f44e979

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

src/Router.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Sunrise\Http\Router\Exception\RouteAlreadyExistsException;
2525
use Sunrise\Http\Router\Exception\RouteNotFoundException;
2626
use Sunrise\Http\Router\Loader\LoaderInterface;
27+
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler;
2728
use Sunrise\Http\Router\RequestHandler\QueueableRequestHandler;
2829

2930
/**
@@ -275,6 +276,28 @@ public function match(ServerRequestInterface $request) : RouteInterface
275276
throw new RouteNotFoundException('Route Not Found');
276277
}
277278

279+
/**
280+
* Runs the router
281+
*
282+
* @param ServerRequestInterface $request
283+
*
284+
* @return ResponseInterface
285+
*
286+
* @since 2.8.0
287+
*/
288+
public function run(ServerRequestInterface $request) : ResponseInterface
289+
{
290+
// lazy resolving of the given request...
291+
$routing = new CallableRequestHandler(function (ServerRequestInterface $request) : ResponseInterface {
292+
return $this->match($request)->handle($request);
293+
});
294+
295+
$handler = new QueueableRequestHandler($routing);
296+
$handler->add(...$this->getMiddlewares());
297+
298+
return $handler->handle($request);
299+
}
300+
278301
/**
279302
* {@inheritDoc}
280303
*/
@@ -295,7 +318,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
295318
{
296319
try {
297320
return $this->handle($request);
298-
} catch (MethodNotAllowedException | RouteNotFoundException $e) {
321+
} catch (MethodNotAllowedException|RouteNotFoundException $e) {
299322
$request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e);
300323

301324
return $handler->handle($request);

tests/RouterTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
use Sunrise\Http\Router\Exception\MiddlewareAlreadyExistsException;
1313
use Sunrise\Http\Router\Exception\RouteAlreadyExistsException;
1414
use Sunrise\Http\Router\Exception\RouteNotFoundException;
15+
use Sunrise\Http\Router\Middleware\CallableMiddleware;
1516
use Sunrise\Http\Router\Loader\LoaderInterface;
1617
use Sunrise\Http\Router\Route;
1718
use Sunrise\Http\Router\RouteCollection;
1819
use Sunrise\Http\Router\Router;
20+
use Sunrise\Http\Message\ResponseFactory;
1921
use Sunrise\Http\ServerRequest\ServerRequestFactory;
2022

2123
/**
@@ -299,6 +301,84 @@ public function testMatchForUndefinedRoute() : void
299301
}
300302
}
301303

304+
/**
305+
* @return void
306+
*/
307+
public function testRun() : void
308+
{
309+
$routes = [
310+
new Fixture\TestRoute(),
311+
new Fixture\TestRoute(),
312+
new Fixture\TestRoute(),
313+
];
314+
315+
$router = new Router();
316+
$router->addRoute(...$routes);
317+
$router->run((new ServerRequestFactory)
318+
->createServerRequest(
319+
$routes[1]->getMethods()[1],
320+
$routes[1]->getPath()
321+
));
322+
323+
$this->assertTrue($routes[1]->getRequestHandler()->isRunned());
324+
}
325+
326+
/**
327+
* @return void
328+
*/
329+
public function testRunWithNotAllowedMethod() : void
330+
{
331+
$routes = [
332+
new Fixture\TestRoute(),
333+
new Fixture\TestRoute(),
334+
new Fixture\TestRoute(),
335+
];
336+
337+
$router = new Router();
338+
$router->addRoute(...$routes);
339+
340+
$router->addMiddleware(new CallableMiddleware(function ($request, $handler) {
341+
try {
342+
return $handler->handle($request);
343+
} catch (MethodNotAllowedException $e) {
344+
return (new ResponseFactory)->createResponse(405);
345+
}
346+
}));
347+
348+
$response = $router->run((new ServerRequestFactory)
349+
->createServerRequest('UNKNOWN', $routes[1]->getPath()));
350+
351+
$this->assertSame(405, $response->getStatusCode());
352+
}
353+
354+
/**
355+
* @return void
356+
*/
357+
public function testRunWithNotFoundRoute() : void
358+
{
359+
$routes = [
360+
new Fixture\TestRoute(),
361+
new Fixture\TestRoute(),
362+
new Fixture\TestRoute(),
363+
];
364+
365+
$router = new Router();
366+
$router->addRoute(...$routes);
367+
368+
$router->addMiddleware(new CallableMiddleware(function ($request, $handler) {
369+
try {
370+
return $handler->handle($request);
371+
} catch (RouteNotFoundException $e) {
372+
return (new ResponseFactory)->createResponse(404);
373+
}
374+
}));
375+
376+
$response = $router->run((new ServerRequestFactory)
377+
->createServerRequest($routes[1]->getMethods()[1], '/unknown'));
378+
379+
$this->assertSame(404, $response->getStatusCode());
380+
}
381+
302382
/**
303383
* @return void
304384
*/

0 commit comments

Comments
 (0)