export() public method

Retrieve an associative array summarizing the host definition
public export ( ) : array
return array
Esempio n. 1
0
 private function checkValidity(Host $host)
 {
     $details = $host->export();
     if (!$this->isListeningOn(443, $details["interfaces"])) {
         throw new \InvalidArgumentException("Host isn't listening on port 443, host not allowed!");
     }
     if (!empty($details["crypto"])) {
         throw new \InvalidArgumentException("Host must not have crypto settings already!");
     }
     if (empty($details["actions"])) {
         $this->showActionWarning = true;
     }
 }
Esempio n. 2
0
 private static function buildVhost(Host $host, callable $bootLoader) : Vhost
 {
     try {
         $hostExport = $host->export();
         $interfaces = $hostExport["interfaces"];
         $name = $hostExport["name"];
         $actions = $hostExport["actions"];
         $middlewares = [];
         $applications = [];
         $monitors = [];
         foreach ($actions as $key => $action) {
             if ($action instanceof Bootable) {
                 $action = $bootLoader($action);
             } elseif (is_array($action) && $action[0] instanceof Bootable) {
                 $bootLoader($action[0]);
             }
             if ($action instanceof Middleware) {
                 $middlewares[] = [$action, "do"];
             } elseif (is_array($action) && $action[0] instanceof Middleware) {
                 $middlewares[] = [$action[0], "do"];
             }
             if ($action instanceof Monitor) {
                 $monitors[get_class($action)][] = $action;
             } elseif (is_array($action) && $action[0] instanceof Monitor) {
                 $monitors[get_class($action[0])][] = $action[0];
             }
             if (is_callable($action)) {
                 $applications[] = $action;
             }
         }
         if (empty($applications)) {
             $application = static 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, PsrLogger $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, $monitors, $hostExport["httpdriver"]);
         if ($crypto = $hostExport["crypto"]) {
             $vhost->setCrypto($crypto);
         }
         return $vhost;
     } catch (\Throwable $previousException) {
         throw new \DomainException("Failed building Vhost instance", $code = 0, $previousException);
     }
 }