/**
* Get the read configuration for a read / write connection.
*
* @param array $config
* @return array
*/
protected function getReadConfig(array $config)
{// Get the read configuration for a read / write connection.
$readConfig = $this->getReadWriteConfig($config, 'read');// get the read config
if (isset($readConfig['host']) && is_array($readConfig['host'])) {
// if this read config host is a array
$readConfig['host'] = count($readConfig['host']) > 1
? $readConfig['host'][array_rand($readConfig['host'])]
: $readConfig['host'][0];
}// a very bad array structure that i think
return $this->mergeReadWriteConfig($config, $readConfig);
}// merge read write config with the read config. ha ha
/**
* Get the read configuration for a read / write connection.
*
* @param array $config
* @return array
*/
protected function getWriteConfig(array $config)
{// get the write config throw set option like "write" by this function
$writeConfig = $this->getReadWriteConfig($config, 'write');
return $this->mergeReadWriteConfig($config, $writeConfig);
}// merge the config
/**
* Get a read / write level configuration.
*
* @param array $config
* @param string $type
* @return array
*/
protected function getReadWriteConfig(array $config, $type)
{// get a read or write level configuration
if (isset($config[$type][0])) {
return $config[$type][array_rand($config[$type])];
}//if set the type 0 return this rand type
return $config[$type];// return this all
}
/**
* Merge a configuration for a read / write connection.
*
* @param array $config
* @param array $merge
* @return array
*/
protected function mergeReadWriteConfig(array $config, array $merge)
{// merge a configuration for a read / write connection.
return Arr::except(array_merge($config, $merge), ['read', 'write']);
}//return Arr::except this function is too powerful
/**
* Parse and prepare the database configuration.
*
* @param array $config
* @param string $name
* @return array
*/
protected function parseConfig(array $config, $name)
{//Parse and prepare the database configuration
return Arr::add(Arr::add($config, 'prefix', ''), 'name', $name);
}
/**
* Create a connector instance based on the configuration.
*
* @param array $config
* @return \Illuminate\Database\Connectors\ConnectorInterface
*
* @throws \InvalidArgumentException
*/
public function createConnector(array $config)
{// Create a connector instance based on the configuration
if (! isset($config['driver'])) {// no driver throw exception
throw new InvalidArgumentException('A driver must be specified.');
}
if ($this->container->bound($key = "db.connector.{$config['driver']}")) {
return $this->container->make($key);
}// get the driver and make it
switch ($config['driver']) {
case 'mysql':
return new MySqlConnector;
case 'pgsql':
return new PostgresConnector;
case 'sqlite':
return new SQLiteConnector;
case 'sqlsrv':
return new SqlServerConnector;
}// too low ,can you use another type to show you power or smarty
throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]");
// default get the exception to return
}
/**
* Create a new connection instance.
*
* @param string $driver
* @param \PDO|\Closure $connection
* @param string $database
* @param string $prefix
* @param array $config
* @return \Illuminate\Database\Connection
*
* @throws \InvalidArgumentException
*/
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
{// Create a new connection instance
if ($this->container->bound($key = "db.connection.{$driver}")) {
return $this->container->make($key, [$connection, $database, $prefix, $config]);
}// if has it just return it
switch ($driver) {
case 'mysql':
return new MySqlConnection($connection, $database, $prefix, $config);
case 'pgsql':
return new PostgresConnection($connection, $database, $prefix, $config);
case 'sqlite':
return new SQLiteConnection($connection, $database, $prefix, $config);
case 'sqlsrv':
return new SqlServerConnection($connection, $database, $prefix, $config);
}// other return what you want by you self,
throw new InvalidArgumentException("Unsupported driver [$driver]");
// default throw wrong message.
}
}