Generates an updated slots map fetching the cluster configuration using
the CLUSTER SLOTS command against the specified node or a random one from
the pool.

@param Client|null $connection Optional connection instance.

@return array
178667
public function askSlotsMap(Client $connection = null)
    {
        $connection = $connection ?? $this->getRandomConnection();
        if (!$connection) {
            return [];
        }

        $response = $this->queryClusterNodeForSlotsMap($connection);

        $this->hosts = $this->slotsMap = [];
        foreach ($response as $slots) {
            // We only support master servers for now, so we ignore subsequent
            // elements in the $slots array identifying slaves.
            [$start, $end, $master] = $slots;

            if ($master[0] === '') {
                $this->setSlots($start, $end, (string) $connection);
            } else {
                $this->setSlots($start, $end, "{$master[0]}:{$master[1]}");
            }
        }

        return $this->slotsMap;
    }
942250
public function map(array $configuration)
	{
		return [
			'driver' => $this->driver($configuration['driver']),
			'host' => $configuration['host'],
			'dbname' => $configuration['database'],
			'user' => $configuration['username'],
			'password' => $configuration['password'],
			'charset' => $configuration['charset'],
        	'prefix' => @$configuration['prefix'] ? $configuration['prefix'] : null
		];
	}
1098319
protected function prepare( Connection $connection )
	{
		$connection->setFetchMode( Config::get( 'database.fetch' ) );

		// Here we'll set a reconnector callback. This reconnector can be any callable
		// so we will set a Closure to reconnect from this manager with the name of
		// the connection, which will allow us to reconnect from the connections.
		$connection->setReconnector( function ( $connection )
		{
			$this->reconnect( $connection->getName() );
		} );

		return $connection;
	}
369559
public function getAll(Connection $connection): array
    {
        foreach ($this->registry as $types) {
            foreach (array_keys($types) as $type) {
                $this->get($connection, $type);
            }
        }

        return $this->instances[$connection->driver()];
    }
1047517
private function readMigrated(Connection $connection): array
    {
        $builder  = $connection->createQueryBuilder();
        $migrated = [];

        if ($connection->getSchemaManager()->tablesExist([static::MIGRATIONS_TABLE]) === true) {
            $migrations = $builder
                ->select(static::MIGRATIONS_COLUMN_ID, static::MIGRATIONS_COLUMN_CLASS)
                ->from(static::MIGRATIONS_TABLE)
                ->orderBy(static::MIGRATIONS_COLUMN_ID)
                ->execute()
                ->fetchAll();
            foreach ($migrations as $migration) {
                $index            = $migration[static::MIGRATIONS_COLUMN_ID];
                $class            = $migration[static::MIGRATIONS_COLUMN_CLASS];
                $migrated[$index] = $class;
            }
        }

        return $migrated;
    }
236889
protected function updateConnection($connection)
    {
        if ($this->connection !== $connection) {
            $this->connection  = $connection;
            $this->schemaSetUp = false;
        }

        return $this;
    }
189832
protected function guessNode($slot)
    {
        if (isset($this->slotsMap[$slot])) {
            return $this->slotsMap[$slot];
        }

        $count = \count($this->hosts);
        $index = \min((int) ($slot / (int) (16384 / $count)), $count - 1);
        $nodes = \array_values($this->hosts);

        return $nodes[$index];
    }
425339
protected function getDatabaseConnectionConfig($connection)
    {
        $connections = $this->proxy->get('database.connections');
        if (!isset($connections[$connection])) {
            throw new Exception('Unknown database connection: ' . $connection);
        }
        $config = $connections[$connection];
        if (!in_array($config['driver'], ['mysql', 'pgsql'])) {
            throw new Exception('Currently only MySQL and PostgreSQL databases are supported using the laravel config');
        }
        return $config;
    }
384582
public function syncConnectionCredentials(ConnectionKey $connection = null, array $credentials = [])
    {
        // Store credentials if any
        if (!$credentials) {
            return;
        }

        $key = 'connections.'.$connection->name.'.servers.'.$connection->server;

        $filtered = $this->filterUnsavableCredentials($connection, $credentials);
        $this->localStorage->set($key, $filtered);

        // Merge and save
        $current = (array) $this->config->get($key);
        $credentials = array_replace_recursive($current, $credentials);
        $this->config->set($key, $credentials);

        // Reset connections
        $this->connections->disconnect();

        return $credentials;
    }
922810
public function getDbConverters(ConnectionInterface $connection)
    {
        if ($this->getTargetField()->getName() === $this->getOutputField()->getName()) {
            return $this->getOutputField()->getDbConverters($connection);
        } else {
            return array_merge(
                $this->getOutputField()->getDbConverters($connection),
                $this->getTargetField()->getDbConverters($connection)
            );
        }
    }
274090
public function getChannel($connection)
    {
        if (isset($this->channels[$connection])) {
            return $this->channels[$connection];
        }

        if (!isset($this->connections[$connection])) {
            throw new \InvalidArgumentException(sprintf(
                'Unknown connection "%s". Available: [%s]',
                $connection,
                implode(', ', array_keys($this->connections))
            ));
        }

        if (!isset($this->channels[$connection])) {
            $this->channels[$connection] = array();
        }

        if (isset($this->connections[$connection]['ssl']) && $this->connections[$connection]['ssl']) {
            if (empty($this->connections[$connection]['ssl_options'])) {
                $ssl_opts = array(
                    'verify_peer' => true,
                );
            } else {
                $ssl_opts = array();
                foreach ($this->connections[$connection]['ssl_options'] as $key => $value) {
                    if (!empty($value)) {
                        $ssl_opts[$key] = $value;
                    }
                }
            }

            $conn = new AMQPSSLConnection(
                $this->connections[$connection]['host'],
                $this->connections[$connection]['port'],
                $this->connections[$connection]['login'],
                $this->connections[$connection]['password'],
                $this->connections[$connection]['vhost'],
                $ssl_opts
            );
        } else {
            $conn = new AMQPConnection(
                $this->connections[$connection]['host'],
                $this->connections[$connection]['port'],
                $this->connections[$connection]['login'],
                $this->connections[$connection]['password'],
                $this->connections[$connection]['vhost']
            );
        }

        $this->channels[$connection] = $conn->channel();

        return $this->channels[$connection];
    }
478033
public function queryBuilder($connection = null)
    {
        $key = $connection ?? $this->defaultConnection;

        if (!isset($this->queryBuilders[$key]))
        {
            $this->connection($key); // init connection
            $this->queryBuilders[$key] = new QueryBuilder($this->adapterProviders[$key]);
        }

        return $this->queryBuilders[$key];
    }
95105
protected function buildClusterSeed($server)
    {
        $parameters = [];

        foreach (['database', 'timeout', 'prefix'] as $parameter) {
            if (! empty($server[$parameter])) {
                $parameters[$parameter] = $server[$parameter];
            }
        }

        if (! empty($server['password'])) {
            $parameters['auth'] = $server['password'];
        }

        return $server['host'].':'.$server['port'].'?'.http_build_query($parameters);
    }
1096109
public function getEventQueue(ConnectionInterface $connection)
    {
        if (!isset($this->queues[$connection])) {
            $this->queues[$connection] = new EventQueue;
        }
        return $this->queues[$connection];
    }
913486
protected function getNextJob( $connection, $queue )
	{
		if ( is_null( $queue ) )
			return $connection->pop();

		foreach ( explode( ',', $queue ) as $queue )
			if ( !is_null( $job = $connection->pop( $queue ) ) )
				return $job;

		return null;
	}
185032
public function newFromBuilder($attributes = [], $connection = null)
    {
        $model = $this->newInstance([], true);

        $model->setRawAttributes((array) $attributes, true);

        $model->setConnection($connection ?: $this->getConnectionName());

        $model->fireModelEvent('retrieved', false);

        return $model;
    }
693727
private function getDbalConnectionsNode()
    {
        $treeBuilder = new TreeBuilder('connections');

        if (method_exists($treeBuilder, 'getRootNode')) {
            $node = $treeBuilder->getRootNode();
        } else {
            // BC layer for symfony/config 4.1 and older
            $node = $treeBuilder->root('connections');
        }

        /** @var ArrayNodeDefinition $connectionNode */
        $connectionNode = $node
            ->requiresAtLeastOneElement()
            ->useAttributeAsKey('name')
            ->prototype('array');

        $this->configureDbalDriverNode($connectionNode);

        $connectionNode
            ->fixXmlConfig('option')
            ->fixXmlConfig('mapping_type')
            ->fixXmlConfig('slave')
            ->fixXmlConfig('shard')
            ->fixXmlConfig('default_table_option')
            ->children()
                ->scalarNode('driver')->defaultValue('pdo_mysql')->end()
                ->scalarNode('platform_service')->end()
                ->booleanNode('auto_commit')->end()
                ->scalarNode('schema_filter')->end()
                ->booleanNode('logging')->defaultValue($this->debug)->end()
                ->booleanNode('profiling')->defaultValue($this->debug)->end()
                ->scalarNode('server_version')->end()
                ->scalarNode('driver_class')->end()
                ->scalarNode('wrapper_class')->end()
                ->scalarNode('shard_manager_class')->end()
                ->scalarNode('shard_choser')->end()
                ->scalarNode('shard_choser_service')->end()
                ->booleanNode('keep_slave')->end()
                ->arrayNode('options')
                    ->useAttributeAsKey('key')
                    ->prototype('scalar')->end()
                ->end()
                ->arrayNode('mapping_types')
                    ->useAttributeAsKey('name')
                    ->prototype('scalar')->end()
                ->end()
                ->arrayNode('default_table_options')
                    ->info("This option is used by the schema-tool and affects generated SQL. Possible keys include 'charset','collate', and 'engine'.")
                    ->useAttributeAsKey('name')
                    ->prototype('scalar')->end()
                ->end()
            ->end();

        $slaveNode = $connectionNode
            ->children()
                ->arrayNode('slaves')
                    ->useAttributeAsKey('name')
                    ->prototype('array');
        $this->configureDbalDriverNode($slaveNode);

        $shardNode = $connectionNode
            ->children()
                ->arrayNode('shards')
                    ->prototype('array')
                    ->children()
                        ->integerNode('id')
                            ->min(1)
                            ->isRequired()
                        ->end()
                    ->end();
        $this->configureDbalDriverNode($shardNode);

        return $node;
    }