. namespace hoplite\http2; require_once HOPLITE_ROOT . '/http2/middleware.php'; require_once HOPLITE_ROOT . '/http2/request.php'; require_once HOPLITE_ROOT . '/http2/response.php'; class Pipeline { private $middleware = []; public function add($middleware) { array_push($this->middleware, $middleware); return $this; } public function build() { return $this->_build(new Sentinel(new NotFoundResponse())); } public function buildWithTail($tail) { $chain = new Sentinel(new NotFoundResponse()); $chain = $this->buildMiddleware($tail, $chain); return $this->_build($chain); } public function buildMiddleware($args, $next) { if ($args instanceof \Closure) { return new ClosureMiddleware($this, $next, $args); } else if ($args instanceof Pipeline) { return $args->_build($next); } else if (is_array($args)) { $class = array_shift($args); array_unshift($args, $this, $next); return new $class(...$args); } else { return new $args($this, $next); } } private function _build($chain) { for ($i = count($this->middleware) - 1; $i >= 0; --$i) { $chain = $this->buildMiddleware($this->middleware[$i], $chain); } return $chain; } public function run(Middleware $chain) { $response = $chain->execute(new Request()); $response->generate(); } public function buildAndRun() { $this->run($this->build()); } }