/
DataProvider.php
357 lines (351 loc) · 8.27 KB
/
DataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* 获取数据源
* @author bluedxNemo
*/
class DataProvider{
/**
* 连接配置表
* @var array
*/
private static $providers;
/**
* 单例
* @var DataProvider
*/
private static $instance;
/**
* 连接池
* @var array
*/
private $pool;
/**
* 初始化连接池
*/
private function __construct(){
$this->pool = array();
}
/**
* 生成单例并注册销毁函数
*/
private static function init(){
if(!self::$instance){
self::$instance = new DataProvider();
register_shutdown_function(array(
self::$instance,
'shutdown'
));
}
}
/**
* 获取单例
* @return DataProvider
*/
public static function getInstance(){
self::init();
return self::$instance;
}
/**
* 销毁函数
*/
public function shutdown(){
foreach($this->pool as $provider){
$provider->close();
}
}
/**
* 应用连接配置
* @param array $config
*/
public static function applyConfig($config){
self::$providers = $config;
}
/**
* 获取数据源
* @param string $key
* @return iAccessWrapper
*/
public static function getDataProvider($key){
self::init();
if(!isset(self::$providers[$key])){
return false;
}
$config = self::$providers[$key];
if(!isset(self::$instance->pool[$key])){
self::$instance->pool[$key] = new AccessWrapper($key, $config);
}
return self::$instance->pool[$key];
}
/**
* 直接调用数据操作方法
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments){
$key = array_shift($arguments);
$provider = self::getDataProvider($key);
if($provider){
return call_user_func_array(array(
$provider,
$name
), $arguments);
}
}
}
/**
* 数据源接口
* @author bluedxNemo
*/
interface iAccessWrapper{
/**
* 初始化函数
* @param string $name 连接名
* @param array $config 连接配置
* @throws Exception
*/
public function __construct($name, $config);
/**
* 关闭连接
*/
public function close();
}
/**
* Redis数据源实现
* @author bluedxNemo
*/
class AccessWrapper implements iAccessWrapper{
/**
* 连接池
* @var array
*/
private static $connections = array();
/**
* redis连接
* @var Redis
*/
private $connection;
/**
* 连接名
* @var string
*/
private $name;
/**
* 数据库
* @var int
*/
private $db;
/**
* 数据库
* @var boolen
*/
const IS_DEBUG = TRUE;
/**
* 初始化函数
* @param string $name 连接名
* @param array $config 连接配置
* @throws Exception
*/
public function __construct($name, $config){
$host = $config['host'];
$port = $config['port'];
$db = isset($config['db']) ? $config['db'] : 0;
$conn = &self::findServer($config);
$this->db = $db;
$this->name = $name;
if(!$conn){
switch(strtolower($config['type'])){
case 'mysql':
$user = $config['user'];
$pwd = $config['pwd'];
$timeout = 2;//秒
if(!empty($config['timeout'])){
$timeout = +$config['timeout'];
}
try{
$conn = array(
'type' => $config['type'],
'host'=>$host,
'port'=>$port,
'db'=>$db,
'name'=>$name,
'ctime'=>microtime(true),
'handle' => new PDO("mysql:host={$host};port={$port};dbname={$db};charset=utf8","{$user}","{$pwd}", array(
//PDO::ATTR_TIMEOUT => $timeout,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
))
);
//php5.3.6以下版本会忽略DSN中的charset设定 这里手动指定字符集
$conn['handle']->query('set names utf8;');
//禁用prepared statements的仿真效果 防止SQL注入
$conn['handle']->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
Log("MysqlConnected: {$this->name} $host:$port $db");
self::$connections[] = &$conn;
} catch(PDOException $e){
$message = $e->getMessage();
Log("MysqlError: $message at {$this->name} connect");
}
break;
default://默认redis
$conn = array(
'type' => $config['type'],
'host'=>$host,
'port'=>$port,
'db'=>0,
'name'=>$name,
'handle'=>new Redis()
);
$timeout = 0;
if(!empty($config['timeout'])){
$timeout = +$config['timeout'];
}
try{
$start_time = microtime(true);
if(!$conn['handle']->connect($host, $port, $timeout)){
$took = microtime(true) - $start_time;
throw new Exception("Connection failed, took {$took}s.");
}
$conn['ctime'] = microtime(true);
Log("RedisConnected: {$this->name} $host:$port");
self::$connections[] = &$conn;
} catch(Exception $e){
$message = $e->getMessage();
Log("RedisError: $message at {$this->name} connect");
}
break;
}
}
$this->connection = &$conn;
}
/**
* 查找既存连接 兼容MYSQL
* @param string $host
* @param int $port
* @return array
*/
private static function &findServer($config, $ret = false){
foreach(self::$connections as &$conn){
if($conn['type'] == 'mysql'){
if($conn['host'] === $config['host'] && $conn['port'] === $config['port'] && $conn['db'] === $config['db']){
return $conn;
}
}else{
//redis
if($conn['host'] === $config['host'] && $conn['port'] === $config['port']){
return $conn;
}
}
}
return $ret;
}
/**
* 关闭连接
* @see iAccessWrapper::close()
*/
public function close(){
if($this->connection['type'] == 'mysql'){
$this->connection['handle'] = null;
$time = number_format(microtime(true) - $this->connection['ctime'], 8);
Bingo_Log::notice("MysqlClosed: $time {$this->name}", self::LOG_MODULE);
}else{
if($this->connection['name'] == $this->name){
try{
$this->connection['handle']->close();
$time = number_format(microtime(true) - $this->connection['ctime'], 8);
Bingo_Log::notice("RedisClosed: $time {$this->name}", self::LOG_MODULE);
} catch(Exception $e){
$message = $e->getMessage();
Bingo_Log::warning("RedisError: $message at {$this->name} close", self::LOG_MODULE);
}
}
}
}
/**
* 确保当前访问的是配置文件中的数据库
* 只适用于redis
* @throws Exception
*/
private function ensureDB(){
if($this->connection['db'] != $this->db){
try{
if(!$this->connection['handle']->select($this->db)){
throw new Exception('Select Failed');
}
$this->connection['db'] = $this->db;
Bingo_Log::notice("RedisSelect: {$this->name} {$this->db}", self::LOG_MODULE);
} catch(Exception $e){
$message = $e->getMessage();
Bingo_Log::warning("RedisError: $message at {$this->name} select", self::LOG_MODULE);
}
}
}
/**
* 判断是否调试模式
* @return bool
*/
private static function isDebugMode(){
return self::IS_DEBUG;
}
/**
* 调用Redis库函数
* @param string $name
* @param array $arguments
* @throws Exception
* @return mixed
*/
public function __call($name, $arguments){
try{
if(method_exists($this->connection['handle'], $name)){
if($this->connection['type'] == 'redis') $this->ensureDB();
$time_start = microtime(true);
$ret = call_user_func_array(array(
$this->connection['handle'],
$name
), $arguments);
if(self::isDebugMode()){
$time = number_format(microtime(true) - $time_start, 8);
$args = array();
foreach($arguments as $arg){
$arg = (string)$arg;
if(strlen($arg) > 50){
$arg = substr($arg, 0, 50) . '...';
}
$args[] = $arg;
}
$args = implode(', ', $args);
LOG("handleCall: $time {$this->name} $name($args)");
}
return $ret;
} else{
throw new Exception('undefined method');
}
} catch(Exception $e){
$message = $e->getMessage();
LOG("handleError: $message at {$this->name} $name");
}
}
public function pdo_query($sql, $param){
$stmt = null;
$s = $sql;
if($param){
foreach($param as $k => $v){
$s = str_replace($k, $v, $s);
}
}
try{
$time_start = microtime(true);
$stmt = $this->connection['handle']->prepare($sql);
$stmt->execute($param);
if(self::isDebugMode()){
$time = number_format(microtime(true) - $time_start, 8);
LOG("sql_exec:$time {$this->name} [$s]");
}
}catch(Exception $e){
$message = $e->getMessage();
LOG("mysqlError: $message at {$this->name} [$s]");
}
return $stmt;
}
}
?>