Aerys\Host::use PHP Method

use() public method

Host actions are invoked to service requests in the order in which they are added.
public use ( callable | aerys\Middleware | aerys\Bootable | aerys\Monitor $action ) : Host
$action callable | aerys\Middleware | aerys\Bootable | aerys\Monitor
return Host
    public function use($action) : Host
    {
        $isAction = is_callable($action) || $action instanceof Middleware || $action instanceof Bootable || $action instanceof Monitor;
        $isDriver = $action instanceof HttpDriver;
        if (!$isAction && !$isDriver) {
            throw new \InvalidArgumentException(__METHOD__ . " requires a callable action or Bootable or Middleware or HttpDriver instance");
        }
        if ($isAction) {
            $this->actions[] = $action;
        }
        if ($isDriver) {
            if ($this->httpDriver) {
                throw new \LogicException("Impossible to define two HttpDriver instances for one same Host; an instance of " . get_class($this->httpDriver) . " has already been defined as driver");
            }
            $this->httpDriver = $action;
        }
        return $this;
    }

Usage Example

Example #1
0
        if (!$sslCert) {
            throw new InvalidConfigurationException('Invalid SSL certificate path');
        }
        $sslKey = null;
        if (isset($config['web-api']['ssl']['key-path']) && !($sslKey = realpath($config['web-api']['ssl']['key-path']))) {
            throw new InvalidConfigurationException('Invalid SSL key path');
        }
        $sslContext = $config['web-api']['ssl']['context'] ?? [];
        $host->encrypt($sslCert, $sslKey, $sslContext);
    }
    $bindAddr = $config['web-api']['bind-addr'] ?? '127.0.0.1';
    $bindPort = (int) ($config['web-api']['bind-port'] ?? ($sslEnabled ? 1337 : 1338));
    $host->expose($bindAddr, $bindPort);
    if (isset($config['web-api']['host'])) {
        $host->name($config['web-api']['host']);
    }
    /** @var WebAPIServer $api */
    $api = $injector->make(WebAPIServer::class);
    $host->use($api->getRouter());
    (new Bootstrapper(function () use($host) {
        return [$host];
    }))->init(new AerysLogger($injector->make(Logger::class)))->start();
}
onError(function (\Throwable $e) {
    fwrite(STDERR, "\nAn exception was not handled:\n\n{$e}\n\n");
});
try {
    run();
} catch (\Throwable $e) {
    fwrite(STDERR, "\nSomething went badly wrong:\n\n{$e}\n\n");
}