首页 > 代码库 > [李景山php]每天laravel-20161118|ConnectionFatory.php-2
[李景山php]每天laravel-20161118|ConnectionFatory.php-2
/** * 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. } }
本文出自 “专注php 群号:414194301” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1853487
[李景山php]每天laravel-20161118|ConnectionFatory.php-2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。