Initial implementation of a new HTTP library based on pipelines.
[hoplite.git] / http2 / pipeline.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2016 Blue Static
4 //
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.
8 //
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
12 // more details.
13 //
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/>.
16
17 namespace hoplite\http2;
18
19 require_once HOPLITE_ROOT . '/http2/middleware.php';
20 require_once HOPLITE_ROOT . '/http2/request.php';
21 require_once HOPLITE_ROOT . '/http2/response.php';
22
23 class Pipeline {
24 private $middleware = [];
25
26 public function add($middleware) {
27 /*
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');
33 }
34 */
35 $this->middleware[] = $middleware;
36 return $this;
37 }
38
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);
49 } else {
50 $chain = new $args($this, $chain);
51 }
52 }
53
54 return $chain;
55 }
56
57 public function run(Middleware $chain) {
58 $response = $chain->execute(new Request());
59 $response->generate();
60 }
61
62 public function buildAndRun() {
63 $this->run($this->build());
64 }
65 }
66
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;
75 }
76
77 public function execute(Request $request) {
78 return $this->subpipe->build()->execute($request);
79 }
80 }