php连接数据库类 静态,关于mysql:PHP:以静态方法连接到数据库

use Illuminate\Cache\CacheManager;

use Illuminate\Cache\MemcachedConnector;

use Illuminate\Database\Capsule\Manager as Capsule;

use INSP\Config;

use INSP\Core;

use INSP\Di;

/**

* Class ConnectionManager

*

* @package INSP\Database

*/

class ConnectionManager

{

/**

* @var \Illuminate\Database\Connection

*/

protected static $instance;

/**

* Loads database configuration from static file if it exists if not it loads from

* Wordpress defaults

*

* @param bool $config

*/

public function __construct($config = false)

{

/*

* If config is not provided in the constructor check for a config file in the

* config directory, if that doesn't exist use the database config from wp_config

*/

if (!$config && file_exists(Core::baseDir() . '/Config/database.php')) {

$config = include(Core::baseDir() . '/Config/database.php');

} else {

if (!defined('DB_HOST')) {

if (file_exists(Core::baseDir() . '/local-config.php')) {

define('WP_LOCAL_DEV', true);

require_once(Core::baseDir() . '/local-config.php');

} else {

require_once(Core::baseDir() . '/production-config.php');

}

}

$config = array(

'driver'    => 'mysql',

'host'      => DB_HOST,

'database'  => DB_NAME,

'username'  => DB_USER,

'password'  => DB_PASSWORD,

'charset'   => DB_CHARSET,

'collation' => 'utf8_unicode_ci',

);

}

return self::connect($config);

}

/**

* This method is in charge or setting up all connection's including

* the k2 and mocked testing connection(uTest)

*

* @param $config

*

* @return \Illuminate\Database\Connection

*/

public static function connect($config)

{

$capsule = new Capsule();

// Load some ancillary configurations

$k2Config = Config::getAll('Apis/k2');

$unitConfig = array(

'driver'   => 'sqlite',

'database' => ':memory:',

'prefix'   => ''

);

// Add all needed configurations to connections and name them if needed

$capsule->addConnection($config);

$capsule->addConnection($k2Config, 'k2');

$capsule->addConnection($unitConfig, 'uTest');

// Add capsule to global namespace so we can use it later

$capsule->setAsGlobal();

// Start the caching library so we can use the remember(ttl) method in our queries

$container = $capsule->getContainer();

$cacheConfig = Config::getAll('cache');

$memcachedServers = Config::get('cache.memcached');

$container['memcached.connector'] = new MemcachedConnector();

$container['config']['cache.driver'] = $cacheConfig['driver'];

$container['config']['cache.path'] = $cacheConfig['file']['directory'];

$container['config']['cache.connection'] = null;

$container['config']['cache.table'] = 'cache';

$container['config']['cache.memcached'] = $memcachedServers;

$container['config']['cache.prefix'] = $cacheConfig['prefix'];

// If Memcached is not installed default to file storage

if (!class_exists('Memcached', false)) {

$container['config']['cache.driver'] = 'file';

}

// Start Dependency Injection if it hasn't already been started

Di::init();

$cacheManager = new CacheManager($container);

Di::set('cacheManager', $cacheManager);

$capsule->setCacheManager($cacheManager);

return $capsule->connection();

}

/**

* @param bool $config

*

* @return ConnectionManager

*/

public static function init($config = false)

{

if (!is_object(self::$instance)) {

self::$instance = new ConnectionManager($config);

}

return self::$instance;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值