/** * @param ServerRequestInterface $request * @return Route */ public function match(ServerRequestInterface $request) : Route { $match = $this->routerContainer->getMatcher()->match($request); if (!$match) { throw new NotFound(); } return $match; }
/** * Route the incoming request to its handler, or call the next middleware if no route was found. */ public function __invoke(ServerRequestInterface $request, callable $next) : ResponseInterface { $matcher = $this->routerContainer->getMatcher(); $route = $matcher->match($request); if ($route === false) { // Call the next middleware return $next($request); } foreach ($route->attributes as $key => $val) { $request = $request->withAttribute($key, $val); } return $this->dispatch($route->handler, $request); }
public function __construct($routes) { $routerContainer = new RouterContainer(); $this->collection = $routerContainer->getMap(); foreach ($routes as $identifier => $route) { $this->collection->get($identifier, $route[1]); } /** @var Matcher $matcher */ $this->matcher = $routerContainer->getMatcher(); }
<?php use Aura\Router\RouterContainer; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; chdir(dirname(__DIR__)); //require 'bootstrap/server.php'; /** * /authenticate/\<application_id> * /is-authentication-still-valid/\<application_id>/\<token> * /register-user/\<application_id> * /unregister-user/\<application_id> */ require 'vendor/autoload.php'; $container = new RouterContainer(); $map = $container->getMap(); $matcher = $container->getMatcher(); $route = $container->getRoute(); $map->get('route.authenticate.get', '/authenticate/{application_id}', function (RequestInterface $request, ResponseInterface $response) { echo $request->getUri() . PHP_EOL; $response->getBody()->write($request->getUri()); return $response; }); $matcher->match($request); foreach ($route->attributes as $key => $val) { $request = $request->withAttribute($key, $val); }