private static function buildVhost(Host $host, callable $bootLoader) : Vhost { try { $hostExport = $host->export(); $interfaces = $hostExport["interfaces"]; $name = $hostExport["name"]; $actions = $hostExport["actions"]; $middlewares = []; $applications = []; foreach ($actions as $key => $action) { if ($action instanceof Bootable) { $action = $bootLoader($action); } if ($action instanceof Middleware) { $middlewares[] = [$action, "do"]; } elseif (is_array($action) && $action[0] instanceof Middleware) { $middlewares[] = [$action[0], "do"]; } if (is_callable($action)) { $applications[] = $action; } } if (empty($applications)) { $application = function (Request $request, Response $response) { $response->end("<html><body><h1>It works!</h1></body></html>"); }; } elseif (count($applications) === 1) { $application = current($applications); } else { // Observe the Server in our stateful multi-responder so if a shutdown triggers // while we're iterating over our coroutines we can send a 503 response. This // obviates the need for applications to pay attention to server state themselves. $application = $bootLoader(new class($applications) implements Bootable, ServerObserver { private $applications; private $isStopping = false; public function __construct(array $applications) { $this->applications = $applications; } public function boot(Server $server, Logger $logger) { $server->attach($this); } public function update(Server $server) : Promise { if ($server->state() === Server::STOPPING) { $this->isStopping = true; } return new Success(); } public function __invoke(Request $request, Response $response) { foreach ($this->applications as $action) { $out = $action($request, $response); if ($out instanceof \Generator) { yield from $out; } if ($response->state() & Response::STARTED) { return; } if ($this->isStopping) { $response->setStatus(HTTP_STATUS["SERVICE_UNAVAILABLE"]); $response->setReason("Server shutting down"); $response->setHeader("Aerys-Generic-Response", "enable"); $response->end(); return; } } } public function __debugInfo() { return ["applications" => $this->applications]; } }); } $vhost = new Vhost($name, $interfaces, $application, $middlewares); if ($crypto = $hostExport["crypto"]) { $vhost->setCrypto($crypto); } return $vhost; } catch (\Throwable $previousException) { throw new \DomainException("Failed building Vhost instance", $code = 0, $previousException); } }
private function buildVhost(Host $host, callable $bootLoader) : Vhost { try { $hostExport = $host->export(); $address = $hostExport["address"]; $port = $hostExport["port"]; $name = $hostExport["name"]; $actions = $hostExport["actions"]; list($application, $middlewares) = $this->bootApplication($actions, $bootLoader); $vhost = new Vhost($name, $address, $port, $application, $middlewares); if ($crypto = $hostExport["crypto"]) { $vhost->setCrypto($crypto); } return $vhost; } catch (\Throwable $previousException) { throw new \DomainException("Failed building Vhost instance", $code = 0, $previousException); } }