Create a new aggregate client supporting sharding.

@param  array  $servers
@return array
340324
protected function createAggregateClient(array $servers)
	{
		$servers = array_except($servers, array('cluster'));

		$options = $this->getClientOptions($servers);

		return array('default' => new Client(array_values($servers), $options));
	}
1081926
private function createCommand(Server $server)
    {
        $cmd = [
            'aggregate' => isset($this->collectionName) ? $this->collectionName : 1,
            'pipeline' => $this->pipeline,
        ];
        $cmdOptions = [];

        $cmd['allowDiskUse'] = $this->options['allowDiskUse'];

        if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
            $cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
        }

        foreach (['comment', 'explain', 'maxTimeMS'] as $option) {
            if (isset($this->options[$option])) {
                $cmd[$option] = $this->options[$option];
            }
        }

        if (isset($this->options['collation'])) {
            $cmd['collation'] = (object) $this->options['collation'];
        }

        if (isset($this->options['hint'])) {
            $cmd['hint'] = is_array($this->options['hint']) ? (object) $this->options['hint'] : $this->options['hint'];
        }

        if (isset($this->options['maxAwaitTimeMS'])) {
            $cmdOptions['maxAwaitTimeMS'] = $this->options['maxAwaitTimeMS'];
        }

        if ($this->options['useCursor']) {
            $cmd['cursor'] = isset($this->options["batchSize"])
                ? ['batchSize' => $this->options["batchSize"]]
                : new stdClass;
        }

        return new Command($cmd, $cmdOptions);
    }
401664
public static function async(array $tasks): array
    {
        $server = App::$server->getServer();

        $result = [];
        foreach ($tasks as $task) {
            if (! isset($task['type']) || ! isset($task['name']) || ! isset($task['method']) || ! isset($task['params'])) {
                App::error(sprintf('Task %s format error.', $task['name'] ?? '[UNKNOWN]'));
                continue;
            }

            if ($task['type'] !== static::TYPE_ASYNC) {
                App::error(sprintf('Task %s is not a asynchronous task.', $task['name']));
                continue;
            }

            $data = TaskHelper::pack($task['name'], $task['method'], $task['params'], $task['type']);
            $result[] = $server->task($data);
        }

        return $result;
    }
1137119
public static function createFromArray(array $array): self
    {
        $aggregations = new self(
            $array['total_elements'] ?? 0
        );

        if (isset($array['aggregations'])) {
            foreach ($array['aggregations'] as $aggregationName => $aggregation) {
                $aggregations->addAggregation(
                    $aggregationName,
                    Aggregation::createFromArray($aggregation)
                );
            }
        }

        return $aggregations;
    }
1138045
public function addServersFromFiles($files = [])
    {

        // Since we expect an array let us turn a string (i.e. single file) into an array
        if (!is_array($files)) {
            $files = [$files];
        }

        // Iterate over the file(s) and add them
        foreach ($files as $file) {
            // Check to make sure the file exists and we can read it
            if (!file_exists($file) || !is_readable($file)) {
                continue;
            }

            // See if this file is JSON
            if (($servers = json_decode(file_get_contents($file), true)) === null
                && json_last_error() !== JSON_ERROR_NONE
            ) {
                // Type not supported
                continue;
            }

            // Add this list of servers
            $this->addServers($servers);
        }

        return $this;
    }
10493
protected function format(array $services): array
    {
        return Collection::make($services)->map(function (array $service) {
            return [
                'id'   => $service['ID'],
                'name' => $service['Service'],
                'host' => $service['Address'],
                'port' => $service['Port'],
            ];
        })->groupBy('name')->toArray();
    }
449442
protected function createRedisClusterInstance(array $servers, array $options)
    {
        return new RedisCluster(
            null,
            array_values($servers),
            $options['timeout'] ?? 0,
            $options['read_timeout'] ?? 0,
            isset($options['persistent']) && $options['persistent']
        );
    }
389321
protected function createSingleClients(array $servers, array $options = []) {
        $clients = array();

        foreach ($servers as $key => $server) {
        	if ($key === 'cluster') continue;

            $redis = new Redis();

            $host    = empty($server['host'])    ? '127.0.0.1' : $server['host'];
            $port    = empty($server['port'])    ? '6379'      : $server['port'];
            $timeout = empty($server['timeout']) ? 0           : $server['timeout'];

            if (isset($server['persistent']) && $server['persistent']) {
            	$redis->pconnect($host, $port, $timeout);
            } else {
            	$redis->connect($host, $port, $timeout);
            }

            if (!empty($server['prefix'])) {
            	$redis->setOption(Redis::OPT_PREFIX, $server['prefix']);
            }

            if (!empty($server['database'])) {
            	$redis->select($server['database']);
            }

            if (!empty($server['serializer'])) {
            	$serializer = Redis::SERIALIZER_NONE;
            	if ($server['serializer'] === 'php') {
            		$serializer = Redis::SERIALIZER_PHP;
            	} else if ($server['serializer'] === 'igbinary') {
            		if (defined('Redis::SERIALIZER_IGBINARY')) {
                        $serializer = Redis::SERIALIZER_IGBINARY;
                    } else {
                        $serializer = Redis::SERIALIZER_PHP;
                    }
            	}
            	$redis->setOption(Redis::OPT_SERIALIZER, $serializer);
            }

            $clients[$key] = $redis;
        }

        return $clients;
    }
386522
public function server(array $server)
    {
        foreach ((array) $server as $key => $value)
        {
            $string = strtolower(substr($key, 5));

            $key = str_replace('_', '-', $string);

            if (strpos($key, 'HTTP_') === 0)
            {
                $this->headers[$key] = $value;
            }
        }
    }
563223
public function addBulk(array $tasks) {

        $result = [];

        foreach($tasks as $name => $task) {

            if ( empty($task['class']) ) {

                $this->logger->warning("Missing class for task $name");
                $result[] = false;

            } else {

                $result[] = $this->add($name, $task['class'], empty($task['description']) ? null : $task['description']);

            }

        }

        return $result;

    }
671350
protected function createAggregateClient()
	{
		$servers = array_values($this->servers);
		foreach ($servers as $k => $server)
		{
			$servers[$k] = (array) $server;
		}

		return array('default' => new Client($servers, $this->options));
	}
847482
public static function make(array $server = null)
    {
        if (empty($server)) {
            $server = $_SERVER;
        }
        $headers = [];
        $content = [
            'CONTENT_TYPE'   => 'Content-Type',
            'CONTENT_LENGTH' => 'Content-Length',
            'CONTENT_MD5'    => 'Content-Md5',
        ];
        foreach ($server as $key => $value) {
            if (substr($key, 0, 5) === 'HTTP_') {
                $key = substr($key, 5);
                if (! isset($content[$key]) || ! isset($server[$key])) {
                    $key = str_replace('_', ' ', $key);
                    $key = ucwords(strtolower($key));
                    $key = str_replace(' ', '-', $key);

                    $headers[$key] = $value;
                }
            } elseif (isset($content[$key])) {
                $headers[$content[$key]] = $value;
            }
        }

        return $headers;
    }
130180
private static function aggregateUriFromServer($server)
    {
        $uri = new Uri();

        $scheme = 'http';
        $uri = $uri->withScheme($scheme);

        if (isset($server['SERVER_NAME'])) {
            $uri = $uri->withHost($server['SERVER_NAME']);
        }

        if (isset($server['SERVER_PORT'])) {
            $uri = $uri->withPort((int)$server['SERVER_PORT']);
        }

        if (isset($server['REQUEST_URI'])) {
            $q = explode('?', $server['REQUEST_URI']);

            if (isset($q[0])) {
                $uri = $uri->withPath($q[0]);
            }
        }

        if (isset($server['QUERY_STRING'])) {
            $uri = $uri->withQuery($server['QUERY_STRING']);
        }

        return $uri;
    }
1265708
public static function createIndex($shards = null, $replicas = null)
    {
        $instance = new static;

        $client = $instance->getElasticSearchClient();

        $index = array(
            'index' => $instance->getIndexName(),
        );

        $settings = $instance->getIndexSettings();
        if (!is_null($settings)) {
            $index['body']['settings'] = $settings;
        }

        if (!is_null($shards)) {
            $index['body']['settings']['number_of_shards'] = $shards;
        }

        if (!is_null($replicas)) {
            $index['body']['settings']['number_of_replicas'] = $replicas;
        }

        $mappingProperties = $instance->getMappingProperties();
        if (!is_null($mappingProperties)) {
            $index['body']['mappings'][$instance->getTypeName()] = [
                '_source' => array('enabled' => true),
                'properties' => $mappingProperties,
            ];
        }

        return $client->indices()->create($index);
    }
1104529
public function shards()
    {
        $request = $this->createRequestInstance(self::CAT_SHARDS, null, null);

        return $this->client->send($request);
    }
1217634
public static function createUri(array $server = [])
    {
        $server += $_SERVER;
        $server = static::normalizeServer($server);
        $headers = static::marshalHeaders($server);

        return static::marshalUriFromServer($server, $headers);
    }
1344652
function setServers(array $servers)
    {
        foreach($servers as &$svr)
        {
            self::formatServerConfig($svr);
        }
        $this->servers = $servers;
    }