Return a instance of Url from a server array

@param array $server the server array

@return static

@throws RuntimeException If the URL can not be parse
515761
public static function createFromServer(array $server)
    {
        $scheme = self::fetchServerScheme($server);
        $host = self::fetchServerHost($server);
        $port = self::fetchServerPort($server);
        $request = self::fetchServerRequestUri($server);

        return self::createFromUrl($scheme.$host.$port.$request);
    }
880859
public static function fromServer(Server $server): ServerForm
    {
        return new self(array_merge($server->getAttributes(), ['server' => $server->name, 'scenario' => 'update'], [
            'ips' => implode(',', ArrayHelper::getColumn($server->ips, 'ip')),
        ]));
    }
673334
protected function findAuthHeader(array $server) {
        if (array_has($server, 'HTTP_AUTHORIZATION')) {
            return array_get($server, 'HTTP_AUTHORIZATION');
        } else if (array_has($server, 'REDIRECT_HTTP_AUTHORIZATION')) {
            return array_get($server, 'REDIRECT_HTTP_AUTHORIZATION');
        }

        return null;
    }
1373282
public static function make(array $server = null)
    {
        $scheme = UriSchemeFactory::make($server);
        $host   = UriHostFactory::make($server);
        $port   = UriPortFactory::make($server);
        $path   = UriPathFactory::make($server);
        $query  = UriQueryFactory::make($server);

        $url = '';
        if ($host) {
            $url .= $scheme . '://' . $host . ':' . $port;
            $path = '/' . ltrim($path, '/');
        }
        $url .= $path;

        if ($query) {
            $url .= '?' . $query;
        }

        $uri = new Uri($url);

        if ($host) {
            return $uri;
        }

        return $uri->withScheme($scheme)->withPort($port);
    }
31119
private static function serverToHeaders(array $server)
    {
        $headers = array();
        foreach ($server as $key => $value) {
            if (strpos($key, 'HTTP_') === 0) {
                $key = strtolower(str_replace("_", '-', substr($key, 5)));
                $headers[$key] = $value;
            }
        }
        return $headers;
    }
9861
private static function getProtocolVersion(array $server)
    {
        if (!isset($server['SERVER_PROTOCOL'])) {
            return '1.1';
        }

        if (!preg_match('/^(HTTP\/)?(\d+(?:\.\d+)+)/', $server['SERVER_PROTOCOL'], $matches)) {
            throw new \UnexpectedValueException(
                sprintf(
                    'Invalid protocol version format (%s)',
                    $server['SERVER_PROTOCOL']
                )
            );
        }

        return $matches[2];
    }
1058023
public function getServer($server)
    {
        if (! array_key_exists($server, $this->servers)) {
            throw new \Exception('Server ['.$server.'] is not defined.');
        }

        return Arr::get($this->servers, $server);
    }
864626
public function connect(array $servers)
	{
		$memcache = $this->getMemcache();

		// For each server in the array, we'll just extract the configuration and add
		// the server to the Memcached connection. Once we have added all of these
		// servers we'll verify the connection is successful and return it back.
		foreach ($servers as $server)
		{
			$memcache->addServer(
				$server['host'], $server['port'], $server['weight']
			);
		}

		if ($memcache->getVersion() === false)
		{
			throw new RuntimeException("Could not establish Memcache connection.");
		}

		return $memcache;
	}
420817
public static function instance($server = 'redis') {
        if(is_string($server)) {
            $server = conf($server);
            $server or $server = Config::getx('redis');
        }
        $key = $server['host'] . $server['port'].$server['db'];
        if($obj = Pool::get($key)) {
            return $obj;
        }
        $pconnect = isset($server['connect'])?$server['connect']:false;
        $obj = get_called_class()?:'nb\\utility\\Redis';
        return Pool::object($key,$obj,[
            $server['host'],$server['port'],$server['db'],$pconnect
        ]);
    }
86211
protected static function parseUrl($url)
    {
        $components = @parse_url($url);
        if (! empty($components)) {
            return $components;
        }

        if (is_null(static::$is_parse_url_bugged)) {
            static::$is_parse_url_bugged = ! is_array(@parse_url("//example.org:80"));
        }

        //bugfix for https://bugs.php.net/bug.php?id=68917
        if (static::$is_parse_url_bugged &&
            strpos($url, '/') === 0 &&
            is_array($components = @parse_url('http:'.$url))
        ) {
            unset($components['scheme']);
            return $components;
        }
        throw new RuntimeException(sprintf("The given URL: `%s` could not be parse", $url));
    }
121442
public static function capture(array $server = null)
    {
        if (!$server) {
            $server = $_SERVER;
        }

        $source = 'http:';
        if (isset($server['HTTPS']) && $server['HTTPS'] && $server['HTTPS'] != 'off') {
            $source = 'https:';
        }
        if (isset($server['HTTP_HOST'])) {
            $source .= '//' . $server['HTTP_HOST'];
        }
        if (isset($server['REQUEST_URI'])) {
            $source .= $server['REQUEST_URI'];
        }

        $url = new static($source);
        $url->username = $server['PHP_AUTH_USER'] ?? null;
        $url->password = $server['PHP_AUTH_PW'] ?? null;

        return $url;
    }
970773
private function processServerVariables(array $server)
    {
        foreach ($server as $key => $value) {
            $this->server[$key] = $value;

            if (strpos($key, 'HTTP_') === 0) {
                $key = str_replace('_', '-', substr($key, 5));

                $this->headers[$key] = $value;
            }
        }

        $uri     = $server['REQUEST_URI'];
        $uriPath = parse_url($uri, PHP_URL_PATH);

        $this->server['REQUEST_URI']      = $uri;
        $this->server['REQUEST_URI_PATH'] = $uriPath;

        $this->isEncrypted = !(empty($server['HTTPS']) || strcasecmp($server['HTTPS'], 'off') === 0);

        $this->server['SERVER_PROTOCOL'] = substr($server['SERVER_PROTOCOL'], -3);

        if (isset($server['CONTENT_LENGTH'])) {
            $this->headers['CONTENT-LENGTH'] = $server['CONTENT_LENGTH'];
        }

        if (isset($server['CONTENT_TYPE'])) {
            $this->headers['CONTENT-TYPE'] = $server['CONTENT_TYPE'];
        }
    }
123829
public function querySingleServer($server)
    {
        // Check if we using the Secure HTTPS proctol.
        $scheme = $this->isSecure ? 'https://' : 'http://';

        // Build a query string from the queries array.
        $numQueries = count($this->queries);
        $queryKeys  = array_keys($this->queries);
        if ($this->debug) {
            echo "Number of query keys {$numQueries}\n";
        }

        $queryString = '';

        for ($i = 0; $i < $numQueries; $i++) {
            /**
             * For each element in the array, append the key
             * and value of the element to the query string.
             */
            $key   = $queryKeys[$i];
            $value = $this->queries[$key];

            if ($this->debug) {
                echo " query key {$key} query value {$value}\n";
            }

            $queryString .= $key . '=' . $value;
            if ($i < $numQueries - 1) {
                $queryString .= '&';
            }
        }

        $url     = $scheme . $server . "/" . $this->url;

        // Check if the curl module exists.
        if (extension_loaded('curl')) {
            // Use curl.
            if ($this->debug) {
                echo "Using curl\n";
            }

            // Open curl.
            $ch = curl_init();

            // Set curl options
            if ($this->debug) {
                echo "url {$url}\n";
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

            if ($this->curlCaInfo) {
                curl_setopt($ch, CURLOPT_CAINFO, $this->curlCaInfo);
            }

            if ($this->curlProxy) {
                curl_setopt($ch, CURLOPT_PROXY, $this->curlProxy);
            }

            // This option lets you store the result in a string.
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);

            // Get the content.
            $content = curl_exec($ch);

            curl_close($ch);
        } else {
            /**
             * The curl extension is not loaded.
             * Use the fsockopen, fgets, and fclose functions.
             */
            if ($this->debug) {
                echo "Using fsockopen for querySingleServer\n";
            }

            $url .= "?{$queryString}";
            if ($this->debug) {
                echo "url {$url}\n";
            }

            // Check if we are using regular HTTP.
            if ($this->isSecure == false) {
                //parse the url to get host, path and query.
                $url3  = parse_url($url);
                $host  = $url3["host"];
                $path  = $url3["path"];
                $query = $url3["query"];

                // Open the connection.
                $fp = fsockopen($host, 80, $errno, $errstr, $this->timeout);

                // There was a problem opening the connection.
                if (!$fp) {
                    return false;
                }

                // Send the request.
                $post = "POST $path HTTP/1.0\n"
                      . "Host: {$host}\n"
                      . "Content-type: application/x-www-form-urlencoded\n"
                      . "User-Agent: Mozilla 4.0\n"
                      . "Content-length: "
                      .     strlen($query)
                      . "\nConnection: close\n\n"
                      . $query;

                fputs($fp, $post);
                $buf = '';
                while (!feof($fp)) {
                    $buf .= fgets($fp, 128);
                }
                $lines = explode("\n", $buf);

                // Get the content.
                $content = $lines[count($lines) - 1];

                // Close the connection.
                fclose($fp);
            } else {
                // Secure HTTPS requires CURL
                echo 'Error: you need to install curl if you want secure HTTPS '
                   . 'or specify the variable to be $ccfs->isSecure = false';
                return false;
            }
        }

        if ($this->debug) {
            echo "content = {$content}\n";
        }

        if (empty($content)) {
            echo "Returned content is empty!\n";
            return false;
        }

        if ($this->useUtf8) {
            $content = utf8_encode($content);
        }

        /**
         * Get the keys and values from the string content
         * and store them in the outputstr array.
         */

        // Split content into pairs containing both the key and the value.
        $keyValuePairs = explode(';', $content);

        // Get the number of key and value pairs.
        $numKeyValuePairs = count($keyValuePairs);

        // For each pair store key and value into the outputstr array.
        $this->outputstr = array();
        for ($i = 0; $i < $numKeyValuePairs; $i++) {
            // Split the pair into a key and a value.
            list($key, $value) = explode('=', $keyValuePairs[$i]);
            if ($this->debug) {
                echo " output {$key} = {$value}\n";
            }

            $this->outputstr[$key] = $value;
        }

        // One other way to do it.
        if (!array_key_exists($this->check_field, $this->outputstr)) {
            return false;
        }

        return true;
    }
757328
protected static function getWebServiceURL($server)
    {
        $urls = [
            static::USE_PRODUCTION_SERVER  => 'https://api.authorize.net/xml/v1/request.api',
            static::USE_DEVELOPMENT_SERVER => 'https://apitest.authorize.net/xml/v1/request.api',
            static::USE_AKAMAI_SERVER      => 'https://api2.authorize.net/xml/v1/request.api'
        ];
        if (array_key_exists($server, $urls)) {
            return $urls[$server];
        }
        throw new AuthnetInvalidServerException('You did not provide a valid server.');
    }
890547
public static function createFromUrl($url)
    {
        $instance = new static();

        if (self::$generic_file->fetchUrl($url, null, $instance)) {
            return $instance;
        }

        return;
    }
268648
public function connect( array $servers )
	{
		$memcached = $this->getMemcached();

		// For each server in the array, we'll just extract the configuration and add
		// the server to the Memcached connection. Once we have added all of these
		// servers we'll verify the connection is successful and return it back.
		foreach ( $servers as $server )
			$memcached->addServer( $server['host'], $server['port'], $server['weight'] );

		$memcachedStatus = $memcached->getVersion();

		if ( !is_array( $memcachedStatus ) )
			throw new RuntimeException( 'No Memcached servers added.' );

		if ( in_array( '255.255.255', $memcachedStatus ) && count( array_unique( $memcachedStatus ) ) === 1 )
			throw new RuntimeException( 'Could not establish Memcached connection.' );

		return $memcached;
	}
6284
public static function getHeadersFromServer(array $server): array
    {
        $headers = [];
        foreach ($server as $key => $value) {
            // Apache prefixes environment variables with REDIRECT_
            // if they are added by rewrite rules
            if (0 === \strpos($key, 'REDIRECT_')) {
                $key = \substr($key, 9);

                // We will not overwrite existing variables with the
                // prefixed versions, though
                if (\array_key_exists($key, $server)) {
                    continue;
                }
            }

            if ($value && 0 === \strpos($key, 'HTTP_')) {
                $name = \strtr(\strtolower(\substr($key, 5)), '_', '-');
                $headers[$name] = $value;

                continue;
            }

            if ($value && 0 === \strpos($key, 'CONTENT_')) {
                $name = 'content-'.\strtolower(\substr($key, 8));
                $headers[$name] = $value;

                continue;
            }
        }

        return $headers;
    }