3 // Copyright (c) 2016 Blue Static
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
17 namespace hoplite\http2
;
19 require_once HOPLITE_ROOT
. '/http2/middleware.php';
20 require_once HOPLITE_ROOT
. '/http2/pipeline.php';
21 require_once HOPLITE_ROOT
. '/http2/route_map.php';
24 The Router is a middleware that operates on a RouteMap object. If a Request
25 matches, the value in the RouteMap is treated as a Middleware to be built by
26 Pipeline::buildMiddleware().
28 If the secondary Pipeline Router::subpipe is provided, the matched value will
29 be treated as if it were Pipeline::add()ed immediately before being built.
30 This allows additional middleware to execute IFF a route match occurs.
32 If no route mach is made, then the Router runs the next middleware.
34 class Router
extends Middleware
{
38 public function __construct(Middleware
$next,
40 Pipeline
$subpipe=NULL
) {
41 parent
::__construct($next);
43 $this->subpipe
= $subpipe ?
$subpipe : new Pipeline();
46 public function execute(Request
$request) {
47 // The query rewriter module of the webserver rewrites a request from:
48 // http://example.com/webapp/user/view/42
50 // http://example.com/webapp/index.php/user/view/42
51 // ... which then becomes accessible from PATH_INFO.
52 if (isset($_SERVER['PATH_INFO']))
53 $url = $_SERVER['PATH_INFO'];
57 $url = substr($url, 1);
60 $route_result = $this->map
->match($request);
63 $request->context
[self
::class] = $route_result;
64 $next = $this->subpipe
->buildWithTail($route_result['result']);
65 return $next->execute($request);
68 return $this->next
->execute($request);