. namespace hoplite\http2; require_once HOPLITE_ROOT . '/http2/middleware.php'; require_once HOPLITE_ROOT . '/http2/pipeline.php'; require_once HOPLITE_ROOT . '/http2/route_map.php'; /*! The Router is a middleware that operates on a RouteMap object. If a Request matches, the value in the RouteMap is treated as a Middleware to be built by Pipeline::buildMiddleware(). If the secondary Pipeline Router::subpipe is provided, the matched value will be treated as if it were Pipeline::add()ed immediately before being built. This allows additional middleware to execute IFF a route match occurs. If no route mach is made, then the Router runs the next middleware. */ class Router extends Middleware { private $map; private $subpipe; public function __construct(Middleware $next, RouteMap $map, Pipeline $subpipe=NULL) { parent::__construct($next); $this->map = $map; $this->subpipe = $subpipe ? $subpipe : new Pipeline(); } public function execute(Request $request) { // The query rewriter module of the webserver rewrites a request from: // http://example.com/webapp/user/view/42 // to: // http://example.com/webapp/index.php/user/view/42 // ... which then becomes accessible from PATH_INFO. if (isset($_SERVER['PATH_INFO'])) $url = $_SERVER['PATH_INFO']; else $url = '/'; if ($url[0] == '/') $url = substr($url, 1); $request->url = $url; $route_result = $this->map->match($request); if ($route_result) { $request->context[self::class] = $route_result; $next = $this->subpipe->buildWithTail($route_result['result']); return $next->execute($request); } return $this->next->execute($request); } }