为了账号安全,请及时绑定邮箱和手机立即绑定

ConnectionFatory.php-2

标签:
PHP

/**

     * 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.

    }

}

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消