单例模式，获取一个指定的实例

// 加载默认实例
$db = Database::instance();

// 指定实例名称和配置
$db = Database::instance('custom', $config);

@param  string $name   实例名
@param  array  $config 配置参数
@return Connection
519936
public static function instance($name = null, array $config = null)
    {
        if (null === $name)
        {
            $name = Db::$default;
        }

        if ( ! isset(Db::$instances[$name]))
        {
            // 读取配置
            if (null === $config)
            {
                $config = (array) Config::load(self::$configFile)->get($name);
                Base::getLog()->debug(__METHOD__ . ' get default config', [
                    'name' => $name,
                ]);
            }

            // 合并默认配置
            if (isset(self::$defaultConfig[Arr::get($config, 'driver')]))
            {
                $config = Arr::merge(self::$defaultConfig[Arr::get($config, 'driver')], $config);
                Base::getLog()->debug(__METHOD__ . ' merge config', [
                    'name' => $name
                ]);
            }

            $conn = DriverManager::getConnection($config);
            Base::getLog()->debug(__METHOD__ . ' create dbal connection', [
                'name' => $name
            ]);

            // 额外注册字段类型
            if (isset(self::$mappingType[Arr::get($config, 'driver')]))
            {
                $platform = $conn->getDatabasePlatform();
                foreach (self::$mappingType[Arr::get($config, 'driver')] as $dbType => $doctrineType)
                {
                    if ( ! $platform->hasDoctrineTypeMappingFor($dbType))
                    {
                        Base::getLog()->debug(__METHOD__ . ' add dbal mapping type', [
                            'raw'  => $dbType,
                            'dbal' => $doctrineType,
                        ]);
                        $platform->registerDoctrineTypeMapping($dbType, $doctrineType);
                    }
                }
            }

            Db::$instances[$name] = $conn;
            Base::getLog()->debug(__METHOD__ . ' save db instance', [
                'name' => $name
            ]);
        }

        return Db::$instances[$name];
    }
544028
public function connect($config = array())
    {
        // TODO: Implement connect() method.
        if (isset($config['database']['cache'])) {
            $configCache = $config['database']['cache'];
            $this->conn = new \Memcache();
            if ($this->conn->connect($configCache['host'], $configCache['port'], $configCache['timeout'])) {
                $this->connected = true;
                return true;
            }
        }
        return false;
    }
1247607
public function connect(array $config)
    {
        return new DatabaseQueue(
            $this->connections->connection(array_get($config, 'connection')),
            array_get($config, 'table', 'queues'),
            array_get($config, 'queue', null),
            array_get($config, 'expire', 60),
            array_get($config, 'lock_type', 60)
        );
    }
302578
protected function createSingleConnection(array $config)
    {
        $pdo = $this->createConnector($config)->connect($config);

        return $this->createConnection($config['driver'], $pdo, $config['database'], $config['prefix'], $config);
    }
680510
protected function createSingleConnection(array $config)
    {
        $pdo = $this->createPdoResolver($config);

        return $this->createConnection(
            $config['driver'], $pdo, $config['database'], $config['prefix'], $config
        );
    }
1287332
public function connect($config = [], $name = false)
    {
        $this->connection = Connection::instance($config, $name);
        $query            = $this->connection->getConfig('query');

        if (__CLASS__ != trim($query, '\\')) {
            return new $query($this->connection);
        }

        $this->prefix = $this->connection->getConfig('prefix');

        return $this;
    }
675411
public function connect(array $config)
    {
        return new DatabaseQueue(
            $this->connections->connection($config['connection'] ?? null),
            $config['table'],
            $config['queue'],
            $config['retry_after'] ?? 60
        );
    }
236358
public function addConnection(array $config, $name = 'default')
    {
        $connections = $this->container['config']['database.connections'];

        $connections[$name] = $config;

        $this->container['config']['database.connections'] = $connections;
    }
241872
public static function connect($name = 'default') {
        if ($name instanceof DatabaseConnection) {
            return $name;
        }
        if (is_null($name)) {
            $name = 'default';
        }
        if (defined('ARTISAN_TASK_PID')) {
            $pid = '@' . @posix_getpid();
        } else {
            $pid = '';
        }
        $config = $pname = false;
        if (is_array($name)) {
            $tmpname = implode('_', $name) . $pid;
            if (isset (self::$dbs [ $tmpname ])) {
                return self::$dbs [ $tmpname ];
            }
            $config = $name;
            $pname  = $tmpname;
        } else if (is_string($name)) {
            $pname = $name . $pid;
            if (isset (self::$dbs [ $pname ])) {
                return self::$dbs [ $pname ];
            }
            $config = App::cfgLoader()->loadDatabaseConfig($name);
        } else if ($name instanceof DatabaseConfiguration) {
            $config = $name;
            $pname  = $config->__toString() . $pid;
        }
        if ($config) {
            $dialect = DatabaseDialect::getDialect($config);
            if ($dialect) {
                $db                   = new DatabaseConnection($dialect, $name);
                $db->name             = $pname;
                self::$dbs [ $pname ] = $db;

                return $db;
            }
        }
        throw new DialectException('cannot connect the database server!');
    }
1242096
public static function initPdoDatabase($config, $name)
    {
        try {
            $pdo = static::createPdoConnectionByConfig($config);
        } catch (PDOException $e) {
            throw new Exception('Cannon instantiate database connection: ' . $e->getMessage());
        }

        $pdo->uniqueName = $config['host'] . "-" . $config['db'];

        static::checkDebugBar($pdo, $name);

        return new RepositoryPDO($pdo, $name);
    }
937582
protected function makeConnection($name, array $config)
    {
        $timeout = isset($config['timeout']) ? $config['timeout'] : 10;

        $this->setOutput($connection = new Connection(

            $name, $config['host'], $config['username'], $this->getAuth($config), null, $timeout

        ));

        return $connection;
    }
1291547
public function connect(array $config)
    {
        $options = $this->getOptions($config);

        return $this->createConnection($this->getDsn($config), $config, $options);
    }
542559
protected function getConfig( $name )
	{
		$name = $name ?: $this->getDefaultConnection();

		// To get the database connection configuration, we will just pull each of the
		// connection configurations and get the configurations for the given name.
		// If the configuration doesn't exist, we'll throw an exception and bail.
		$connections = Framework::config()->get( 'database.connections' );

		if ( is_null( $config = Arr::get( $connections, $name ) ) )
			throw new InvalidArgumentException( "Database [$name] not configured." );

		return $config;
	}
519252
public function connect(array $config)
    {
        $options = $this->getOptions($config);

        if ($config['database'] == ':memory:') {
            return $this->createConnection('sqlite::memory:', $config, $options);
        }

        $path = realpath($config['database']);

        if ($path === false) {
            throw new \InvalidArgumentException("Database does not exist.");
        }

        return $this->createConnection("sqlite:{$path}", $config, $options);
    }
1059455
public static function set($name, $driver, array $config = [])
    {
        self::$connections[$name] = new Connection($name, $driver, $config);
    }
311431
public function connect(array $config){

		// Defaults
		$defaults = array(
			'resource' => null,
			'charset' => 'utf8',
			'username' => ini_get('mysql.default_user'),
			'password' => ini_get('mysql.default_password'),
			'host' => ini_get('mysql.default_host'),
			'port' => ini_get('mysql.default_port'),
			'persistent' => false,
			'unbuffered' => false
		);

		$config += $defaults;
		if(isset($config['port']))
			$host = $config['host'] . ':' . $config['port'];
		else
			$host = $config['host'];

		// Connect
		if(is_resource($config['resource']))
			$connection = $config['resource'];
		elseif($config['persistent'])
			$connection = @mysql_pconnect($host, $config['username'], $config['password']);
		else
			$connection = @mysql_connect($host, $config['username'], $config['password']);

		if(!is_resource($connection))
			throw new DriverException("Connection to host '$host' failed.");

		// Select DB
		if(isset($config['database'])){
			$db = mysql_select_db($config['database']);
			if(!$db)
				throw new DriverException("Could not select database '$config[database]'.");
		}

		$this->resource = $connection;

		//Set charset
		if(is_resource($connection)){
			if(function_exists('mysql_set_charset'))
				@mysql_set_charset($config['charset'], $connection);
			else
				$this->runQuery("SET NAMES " . $config['charset']);
		}

		$this->unbuffered = $config['unbuffered'];
	}
985147
protected function createForConnection($name, array $config = [])
    {
        if (isset($this->customCreators[$name])) {
            return $this->callCustom($name, compact('config'));
        }
    }