mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-09-03 05:41:38 +08:00
PSR规范调整
This commit is contained in:
@@ -14,10 +14,11 @@ namespace think;
|
||||
/**
|
||||
* ThinkPHP 数据库中间层实现类
|
||||
*/
|
||||
class Db {
|
||||
class Db
|
||||
{
|
||||
|
||||
static private $instance = []; // 数据库连接实例
|
||||
static private $_instance = null; // 当前数据库连接实例
|
||||
private static $instance = []; // 数据库连接实例
|
||||
private static $_instance = null; // 当前数据库连接实例
|
||||
|
||||
/**
|
||||
* 取得数据库类实例
|
||||
@@ -27,16 +28,17 @@ class Db {
|
||||
* @param boolean $lite 是否lite方式
|
||||
* @return Object 返回数据库驱动类
|
||||
*/
|
||||
static public function instance($config=[],$lite=false) {
|
||||
$md5 = md5(serialize($config));
|
||||
if(!isset(self::$instance[$md5])) {
|
||||
public static function instance($config = [], $lite = false)
|
||||
{
|
||||
$md5 = md5(serialize($config));
|
||||
if (!isset(self::$instance[$md5])) {
|
||||
// 解析连接参数 支持数组和字符串
|
||||
$options = self::parseConfig($config);
|
||||
$options = self::parseConfig($config);
|
||||
// 如果采用lite方式 仅支持原生SQL 包括query和execute方法
|
||||
$class = $lite? 'think\db\lite' : 'think\\db\\driver\\'.strtolower($options['type']);
|
||||
self::$instance[$md5] = new $class($options);
|
||||
$class = $lite ? 'think\db\lite' : 'think\\db\\driver\\' . strtolower($options['type']);
|
||||
self::$instance[$md5] = new $class($options);
|
||||
}
|
||||
self::$_instance = self::$instance[$md5];
|
||||
self::$_instance = self::$instance[$md5];
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
@@ -47,13 +49,14 @@ class Db {
|
||||
* @param mixed $config
|
||||
* @return array
|
||||
*/
|
||||
static private function parseConfig($config){
|
||||
if(empty($config)) {
|
||||
$config = Config::get('database');
|
||||
private static function parseConfig($config)
|
||||
{
|
||||
if (empty($config)) {
|
||||
$config = Config::get('database');
|
||||
}
|
||||
if(is_string($config)) {
|
||||
if (is_string($config)) {
|
||||
return self::parseDsn($config);
|
||||
}else{
|
||||
} else {
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
@@ -66,32 +69,34 @@ class Db {
|
||||
* @param string $dsnStr
|
||||
* @return array
|
||||
*/
|
||||
static private function parseDsn($dsnStr) {
|
||||
if( empty($dsnStr) ){return false;}
|
||||
private static function parseDsn($dsnStr)
|
||||
{
|
||||
if (empty($dsnStr)) {return false;}
|
||||
$info = parse_url($dsnStr);
|
||||
if(!$info) {
|
||||
if (!$info) {
|
||||
return false;
|
||||
}
|
||||
$dsn = [
|
||||
'type' => $info['scheme'],
|
||||
'username' => isset($info['user']) ? $info['user'] : '',
|
||||
'password' => isset($info['pass']) ? $info['pass'] : '',
|
||||
'hostname' => isset($info['host']) ? $info['host'] : '',
|
||||
'hostport' => isset($info['port']) ? $info['port'] : '',
|
||||
'database' => isset($info['path']) ? substr($info['path'],1) : '',
|
||||
'charset' => isset($info['fragment'])?$info['fragment']:'utf8',
|
||||
'type' => $info['scheme'],
|
||||
'username' => isset($info['user']) ? $info['user'] : '',
|
||||
'password' => isset($info['pass']) ? $info['pass'] : '',
|
||||
'hostname' => isset($info['host']) ? $info['host'] : '',
|
||||
'hostport' => isset($info['port']) ? $info['port'] : '',
|
||||
'database' => isset($info['path']) ? substr($info['path'], 1) : '',
|
||||
'charset' => isset($info['fragment']) ? $info['fragment'] : 'utf8',
|
||||
];
|
||||
|
||||
if(isset($info['query'])) {
|
||||
parse_str($info['query'],$dsn['params']);
|
||||
}else{
|
||||
$dsn['params'] = [];
|
||||
|
||||
if (isset($info['query'])) {
|
||||
parse_str($info['query'], $dsn['params']);
|
||||
} else {
|
||||
$dsn['params'] = [];
|
||||
}
|
||||
return $dsn;
|
||||
}
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
static public function __callStatic($method, $params){
|
||||
public static function __callStatic($method, $params)
|
||||
{
|
||||
return call_user_func_array([self::$_instance, $method], $params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user