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/request.php';
21 require_once HOPLITE_ROOT
. '/http2/response.php';
24 private $middleware = [];
26 public function add($middleware) {
28 if (!($middleware instanceof Middleware ||
29 $middleware instanceof \Closure ||
30 is_array($middleware))) {
31 // TODO(php7): TypeError
32 throw new \Exception(__FUNCTION__ . ' requires a Middleware or Closure');
35 $this->middleware
[] = $middleware;
39 public function build() {
40 $chain = new Sentinel(new NotFoundResponse());
41 for ($i = count($this->middleware
) - 1; $i >= 0; --$i) {
42 $args = $this->middleware
[$i];
43 if ($args instanceof \Closure
) {
44 $chain = new ClosureMiddleware($this, $chain, $args);
45 } else if (is_array($args)) {
46 $class = array_shift($args);
47 array_unshift($args, $this, $chain);
48 $chain = new $class(...$args);
50 $chain = new $args($this, $chain);
57 public function run(Middleware
$chain) {
58 $response = $chain->execute(new Request());
59 $response->generate();
62 public function buildAndRun() {
63 $this->run($this->build());
67 class SubPipeline
extends Middleware
{
68 public function __construct(Pipeline
$subpipe) {
69 // Skip calling super since this is a new pipeline and the
70 // next middleware will be selected by building the subpipe.
71 // It would be possible to build the middleware chain here, but
72 // doing it lazily on execution is more efficient, since SubPipeline
73 // should be used to partition applications.
74 $this->subpipe
= $subpipe;
77 public function execute(Request
$request) {
78 return $this->subpipe
->build()->execute($request);