Aerys\VhostContainer::use PHP Method

use() public method

Add a virtual host to the collection
public use ( Vhost $vhost ) : void
$vhost Vhost
return void
    public function use(Vhost $vhost)
    {
        $vhost = clone $vhost;
        // do not allow change of state after use()
        $this->preventCryptoSocketConflict($vhost);
        foreach ($vhost->getIds() as $id) {
            $this->vhosts[$id] = $vhost;
        }
        $this->addHttpDriver($vhost);
        $this->cachedVhostCount++;
    }

Usage Example

Example #1
0
 /**
  * Initializes the server directly without config file inclusion
  *
  * @param PsrLogger $logger
  * @param array $options Aerys options array
  * @return Server
  */
 public function init(PsrLogger $logger, array $options = []) : Server
 {
     if (!array_key_exists("debug", $options)) {
         $options["debug"] = false;
     }
     $options = $this->generateOptionsObjFromArray($options);
     $vhosts = new VhostContainer(new Http1Driver());
     $ticker = new Ticker($logger);
     $server = new Server($options, $vhosts, $logger, $ticker);
     $bootLoader = static function (Bootable $bootable) use($server, $logger) {
         $booted = $bootable->boot($server, $logger);
         if ($booted !== null && !$booted instanceof Middleware && !is_callable($booted)) {
             throw new \InvalidArgumentException("Any return value of " . get_class($bootable) . '::boot() must return an instance of Aerys\\Middleware and/or be callable, got ' . gettype($booted) . ".");
         }
         return $booted ?? $bootable;
     };
     $hosts = \call_user_func($this->hostAggregator) ?: [new Host()];
     foreach ($hosts as $host) {
         $vhost = self::buildVhost($host, $bootLoader);
         $vhosts->use($vhost);
     }
     return $server;
 }