/** * 配置初始化函数 * @param $conf array 配置项如下: * guid_db 可选 * guid_table 可选 * splits 可选 * log_func 可选 * test_mode 可选 * db_pool=>array( * 'db11'=>array( * 'ip'=>'ip', * 'port'=>3306, * 'user'=>'user', * 'pass'=>'pass', * 'charset'=>'charset' * ), * 'db2'=>xxx * .... * ), * 'dbs'=>array( * 'dbname'=>'db1', * 'dbname'=>array('master'=>'db1','slave'=>array('db2','db3')) * ) * @throws \Exception */ static function init($conf) { //check db conf format foreach ($conf['dbs'] as $db => $dbconf) { if (is_string($dbconf)) { if (!isset($conf['db_pool'][$dbconf])) { throw new \Exception('db.ConfError ' . $dbconf . ' no such pool in db_pool'); } } else { if (!isset($dbconf['master']) || !isset($dbconf['slave'])) { throw new \Exception('db.ConfError missing master|slave conf ' . $db); } $master = $dbconf['master']; $slaves = $dbconf['slave']; if (!isset($conf['db_pool'][$master])) { throw new \Exception('db.ConfError ' . $master . ' no such pool in db_pool'); } foreach ($slaves as $slave) { if (!isset($conf['db_pool'][$slave])) { throw new \Exception('db.ConfError ' . $slave . ' no such pool in db_pool'); } } } } DbContext::$db_pool = $conf['db_pool']; DBContext::$dbconf = $conf['dbs']; DbContext::$guidDB = empty($conf['guid_db']) ? null : $conf['guid_db']; DbContext::$guidTable = empty($conf['guid_table']) ? null : $conf['guid_table']; DbContext::$defaultDB = empty($conf['default_db']) ? null : $conf['default_db']; //转换成小写的,免得因为大小写问题比较不成功 DbContext::$splits = empty($conf['splits']) ? array() : $conf['splits']; DbContext::$logFunc = empty($conf['log_func']) ? null : $conf['log_func']; DbContext::$testMode = !empty($conf['test_mode']); DbContext::$longQueryTime = empty($conf['long_query_time']) ? 0 : $conf['long_query_time']; }