/
index.php
1199 lines (998 loc) · 28.8 KB
/
index.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
@error_reporting(E_ALL^E_NOTICE);
date_default_timezone_set("PRC");
// 单文件入口
IndexPHP::run();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 框架入口类
* 包含自动加载处理、错误处理等
*/
class IndexPHP
{
/**
* 执行
*/
public static function run()
{
// 设定错误和异常处理
set_error_handler('IndexPHP::_error');
set_exception_handler('IndexPHP::_exception');
register_shutdown_function('IndexPHP::_shutdown');
// 定义常量
defined('PATH_APP') or define('PATH_APP', './app/');
defined('PATH_APP_CTRL') or define('PATH_APP_CTRL', PATH_APP . 'ctrl/');
defined('PATH_APP_VIEW') or define('PATH_APP_VIEW', PATH_APP . 'view/');
defined('PATH_APP_LIB') or define('PATH_APP_LIB', PATH_APP . 'lib/');
defined('PATH_APP_LOG') or define('PATH_APP_LOG', PATH_APP . 'log/');
defined('FILE_APP_CONF') or define('FILE_APP_CONF', PATH_APP . '/conf.php');
defined('FILE_APP_COMM') or define('FILE_APP_COMM', PATH_APP . '/common.php');
define('IS_POST', Param::server('REQUEST_METHOD') =='POST' ? true : false);
define('HTTP_HOST', Param::server('HTTP_HOST'));
// 初始化框架
self::_init();
// 导入配置
Config::set(self::import(FILE_APP_CONF));
Config::get('ENABLE_SESSION') && session_start();
// 路由处理
$ca = explode('/', trim(Param::server('PATH_INFO', Config::get('DEFAULT_CTRL_ACTION')), '/'));
define('CTRL_NAME', strtolower(Param::get(Config::get('PARAM_CTRL', 'c'), !empty($ca[0]) ? $ca[0] : 'index')));
define('ACTION_NAME', strtolower(Param::get(Config::get('PARAM_ACTION', 'a'), !empty($ca[1]) ? $ca[1] : 'index')));
// URL参数按顺序绑定变量
preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) {$_GET[$match[1]]=strip_tags($match[2]);}, trim(Param::server('PATH_INFO'), '/'));
// 导入控制器文件
if (!self::import(PATH_APP_CTRL . CTRL_NAME . Config::get('FILE_EXTENSION_CTRL', '.class.php'))) {
throw new Exception('没有控制器:' . CTRL_NAME);
}
// 控制器、方法名称变换处理
$c = self::camelize(CTRL_NAME) . Config::get('POSTFIX_CTRL', 'Controller');
$a = lcfirst(self::camelize(ACTION_NAME)) . Config::get('POSTFIX_ACTION', '');
// 控制器类判断是否存在
if (class_exists($c)) {
// 导入公共函数库
self::import(FILE_APP_COMM);
// 自动加载外部库
spl_autoload_register('self::_autoload');
// 调用控制器方法
call_user_func(array(new $c(), $a));
}
}
/**
* 导入文件
* @param $file
* @return bool|mixed
*/
public static function import($file)
{
// 判断文件是否存在,存在则导入
if (file_exists($file)) {
return include $file;
}
return false;
}
/**
* 驼峰命名转下划线命名
* @param $word
* @return mixed
*/
public static function decamelize($word)
{
//return preg_replace('/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? "\\1_\\2" : "\\2")', $word);
return preg_replace_callback('/(^|[a-z])([A-Z])/', function($r){return strtolower(strlen($r[1]) ? "{$r[1]}_{$r[2]}" : $r[2]);}, $word);
}
/**
* 下划线命名转驼峰命名
* @param $word
* @return mixed
*/
public static function camelize($word)
{
//return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word);
return preg_replace_callback('/(^|_)([a-z])/', function($r){return strtoupper($r[2]);}, $word);
}
/**
* 初始化处理
*/
private static function _init()
{
if (!is_dir(PATH_APP)) {
mkdir(PATH_APP, 0755);
mkdir(PATH_APP_CTRL, 0755);
mkdir(PATH_APP_VIEW, 0755);
mkdir(PATH_APP_LIB, 0755);
mkdir(PATH_APP_LOG, 0755);
// file_put_contents(FILE_APP_CONF, "<?php\nreturn array(\n\t//'配置项'=>'配置值'\n);");
file_put_contents(PATH_APP_CTRL . "index.class.php", "<?php\nclass IndexController extends Controller {\n\n public function index(){\n \$this->show('<h2>Hi IndexPHP~</h2>');\n }\n}");
file_put_contents(".htaccess", "<IfModule mod_rewrite.c>\n RewriteEngine on\n RewriteCond %{REQUEST_FILENAME} !-d\n RewriteCond %{REQUEST_FILENAME} !-f\n RewriteRule ^(.*)$ index.php/$1 [QSA,PT]\n</IfModule>");
// 压缩源文件
($min = php_strip_whitespace(__FILE__)) && (strlen($min) < filesize(__FILE__)) && (file_put_contents('index.min.php', $min));
}
return true;
}
/**
* 自动导入外部库
* @param string $clazz 类名
* @return bool
*/
private static function _autoload($clazz)
{
return self::import(PATH_APP_LIB . self::decamelize($clazz) . Config::get('FILE_EXTENSION_LIB', '.class.php'));
}
/**
* 错误处理
*/
public static function _error($errno, $errmsg, $errfile, $errline)
{
Logger::error("_error : [$errno] $errmsg File: $errfile Line: $errline");
}
/**
* 异常处理
* @param $ex
*/
public static function _exception($ex)
{
$errno = $ex->getCode();
$errmsg = $ex->getMessage();
$errfile = $ex->getFile();
$errline = $ex->getLine();
$errstr = $ex->getTraceAsString();
Logger::error("_exception : [$errno] $errmsg File: $errfile Line: $errline \n $errstr");
}
/**
* 关闭处理
*/
public static function _shutdown()
{
// 判断是否存在错误,存在则写入Log
if ($e = error_get_last()) {
Logger::error("_shutdown : [{$e['type']}] {$e['message']} File: {$e['file']} Line: {$e['line']}");
}
Logger::save();
}
}
/**
* 控制器
*/
class Controller
{
/**
* @var array
*/
private $_p = array();
/**
* 主题
* @var
*/
protected $_theme;
/**
* Controller constructor.
*/
public function __construct() {
$this->_init();
}
/**
* 魔术方法 有不存在的操作的时候执行
* @access public
* @param string $method 方法名
* @param array $args 参数
* @return mixed
*/
public function __call($method, $args) {
// 存在_empty 空方法时则执行_empty方法
if (method_exists($this, '_empty')) {
// 如果定义了_empty操作 则调用
return $this->_empty($method, $args);
}
// 直接渲染模板
$this->display();
}
/**
* 初始化处理
*/
protected function _init() {
}
/**
* 模板赋值
* @param $name
* @param $value
*/
protected function assign($name, $value) {
$this->_p[$name] = $value;
}
/**
* 显示内容
* @param $text
* @param null $params
*/
protected function show($text, $params = null) {
$params = $params ? array_merge($this->_p, $params) : $this->_p;
View::render($text, $params, null);
}
/**
* 显示模板
* @param null $tpl
* @param null $params
* @param null $layout
*/
protected function display($tpl = null, $params = null, $layout = null) {
is_array($tpl) && list($tpl, $params, $layout) = array(null, $tpl, $params);
strpos($tpl, '/') || $tpl = CTRL_NAME . '/' . ($tpl ?: ACTION_NAME);
$this->_theme && $tpl = $this->_theme . '/' . $tpl; // 主题
$params = $params ? array_merge($this->_p, $params) : $this->_p;
// 如果存在布局参数,则将模板数据写到content参数中,然后通过布局输出
if ($layout) {
$params['content'] = View::fetch($tpl, $params, PATH_APP_VIEW);
$tpl = $this->_theme ? $this->_theme . '/' . $layout : $layout; // 主题
}
View::render($tpl, $params);
}
/**
* 正常输出
* @param $msg
* @param null $data
*/
protected function ok($msg, $data = null) {
is_array($msg) && list($msg, $data) = array(null, $msg);
$this->json(1, $msg, $data);
}
/**
* 错误输出
* @param $msg
*/
protected function error($msg) {
$this->json(0, $msg);
}
/**
* 返回json
* @param $status
* @param $msg
* @param null $data
*/
protected function json($status, $msg, $data = null) {
View::render(json_encode(array('status' => $status, 'msg' => $msg, 'data' => $data)), null, null, 'application/json');
exit;
}
/**
* 重定向至指定url
* @param string $url 要跳转的url
* @param void
*/
protected function redirect($url) {
header("Location: $url");exit;
}
}
/**
* 视图类
*/
class View
{
/**
* 渲染模板或者数据,通过参数返回
* @param $file
* @param null $data
* @param null $path
* @return string
*/
public static function fetch($file, $data = null, $path = null) {
// 页面缓存
ob_start();
ob_implicit_flush(0);
// 模板阵列变量分解成为独立变量
$data && extract($data);
// 直接载入PHP模板
isset($path) ? include $path . $file . Config::get('FILE_EXTENSION_VIEW', '.html') : eval('?>' . $file);
// 获取并清空缓存
$content = ob_get_clean();
// 输出模板文件
return $content;
}
/**
* 显示模板或数据内容
* @param $file
* @param null $data
* @param string $path
* @param string $contentType
*/
public static function render($file, $data = null, $path = PATH_APP_VIEW, $contentType = 'text/html') {
$html = self::fetch($file, $data, $path);
header('Content-Type:' . $contentType . '; charset=' . Config::get('HTML_CHARSET', 'utf-8'));
header('X-Powered-By:index-php.top');
echo $html;
}
}
/**
* 数据模型
* Class Model
*/
class Model {
/**
* 数据
* @var array
*/
public $_model = array();
/**
* 数据表&文件
* @var null
*/
private $_t = null;
/**
* 操作
* @var null
*/
private $_s = null; // db & file
/**
* 构造函数
* Model constructor
* @param $table
* @param $storage
*/
private function __construct($table, $storage) {
$this->_t = $table;
$this->_s = $storage;
}
/**
* 魔术方法,调用存储对象的方法
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args) {
// 第一个参数与Model数据合并
$this->_model && $args[0] = ($args[0] ? array_merge($this->_model, $args[0]) : $this->_model);
// 存在数据表参数时将数据表参数作为第一个参数值
$this->_t && array_unshift($args, $this->_t);
// 调用数据库/文件方法
$data = call_user_func_array(array($this->_s, $name), $args);
// 如果返回数据为数组,则保存到Model属性_model中
is_array($data) && $this->_model = $data;
return $data;
}
/**
* 通过数据库对象实例化Model
* @param null $table
* @param bool $db_debug
* @return Model
*/
public static function db($table = null, $db_debug = false) {
(!isset($db_debug) || is_bool($db_debug)) && $db_debug = DB::instance(null, $db_debug);
return new self($table, $db_debug);
}
/**
* 通过文件操作对象实例化Model
* @param $file
* @return Model
*/
public static function file($file) {
return new self($file, File::instance());
}
/**
* 设置数据对象的值
* @access public
* @param string $name 名称
* @param mixed $value 值
* @return void
*/
public function __set($name, $value) {
// 设置数据对象属性
$this->_model[$name] = $value;
}
/**
* 获取数据对象的值
* @access public
* @param string $name 名称
* @return mixed
*/
public function __get($name) {
return $this->_model[$name];
}
/**
* 检测数据对象的值
* @access public
* @param string $name 名称
* @return boolean
*/
public function __isset($name) {
return isset($this->_model[$name]);
}
/**
* 销毁数据对象的值
* @access public
* @param string $name 名称
* @return void
*/
public function __unset($name) {
unset($this->_model[$name]);
}
}
/**
* 配置处理
* Class Config
*/
class Config {
/**
* 配置缓存
* @var array
*/
private static $_c = array();
/**
* 取得配置
* @param $key
* @param null $default
* @return array|null
*/
public static function get($key = null, $default = null) {
// 返回单个配置
if (is_string($key)) {
return isset(self::$_c[$key]) ? self::$_c[$key] : $default;
}
// 返回一组配置
if (is_array($key)) {
$ret = array();
foreach ($key as $k) {
$ret[$k] = isset(self::$_c[$k]) ? self::$_c[$k] : $default;
}
return $ret;
}
// 返回所有配置
return self::$_c;
}
/**
* 设置配置
* @param $key
* @param null $value
* @return array
*/
public static function set($key, $value = null) {
// 设置单个配置
if (is_string($key)) {
self::$_c[$key] = $value;
}
// 多个配置设置合并
if (is_array($key)) {
self::$_c = array_merge(self::$_c, $key);
}
return self::$_c;
}
}
/**
* 参数处理
* Class Param
*/
class Param
{
public static function url($params = null, $action = ACTION_NAME, $ctrl = CTRL_NAME)
{
//is_array($params) && $params = http_build_query($params);
//return "/{$ctrl}/{$action}?{$params}";
$url = "/index.php/{$ctrl}/{$action}";
if($params) {
foreach ($params as $key => $val) {
$url .= "/{$key}/{$val}";
}
}
return Config::get('SYS_MODULE','').$url;
}
/**
* 取得$_GET键对应的值,键值为空返回所有
* @param null $key
* @param null $default
* @return null
*/
public static function get($key = null, $default = null)
{
return self::_param($_GET, $key, $default);
}
/**
* 取得$_POST键对应的值,键值为空返回所有
* @param null $key
* @param null $default
* @return null
*/
public static function post($key = null, $default = null)
{
return self::_param($_POST, $key, $default);
}
/**
* 取得$_REQUEST键对应的值,键值为空返回所有
* @param null $key
* @param null $default
* @return null
*/
public static function request($key = null, $default = null)
{
return self::_param($_REQUEST, $key, $default);
}
/**
* 取得$_SERVER键对应的值,键值为空返回所有
* @param null $key
* @param null $default
* @return null
*/
public static function server($key = null, $default = null)
{
return self::_param($_SERVER, $key, $default);
}
/**
* 取得$_SESSION键对应的值,键值为空返回所有
* @param null $key
* @param null $default
* @return null
*/
public static function session($key = null, $default = null, $isset = false)
{
$isset && $_SESSION[$key] = $default;
return self::_param($_SESSION, $key, $default);
}
/**
* 取得$_COOKIE键对应的值,键值为空返回所有
* @param null $key
* @param null $default
* @return null
*/
public static function cookie($key = null, $default = null, $expire = null)
{
$expire && setcookie($key, $default, $expire);
return self::_param($_COOKIE, $key, $default);
}
/**
* 取得$data键对应的值,键值为空返回所有
* @param $data
* @param null $key
* @param null $default
* @return null
*/
private static function _param($data, $key = null, $default = null)
{
// 如果Key值为空,返回参数数组
if (!isset($key)) {
return $data;
}
// 返回Key对应的参数值,空则返回默认值
return isset($data[$key]) ? $data[$key] : $default;
}
}
/**
* 日志处理
* Class Logger
*/
class Logger
{
const INFO = 1; // 日志类型 信息
const DEBUG = 2; // 日志类型 调试
const WARNING = 3; // 日志类型 警告
const ERROR = 4; // 日志类型 错误
// 类型对应文字
private static $LEVEL = array(self::INFO => 'INFO', self::DEBUG => 'DEBUG', self::WARNING => 'WARNING', self::ERROR => 'ERROR');
// 日志列表
private static $_log = array();
/**
* 保存日志
*/
public static function save()
{
if (count(self::$_log) > 0) {
file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . PATH_APP_LOG . date('Ymd') . '.log', implode(PHP_EOL, self::$_log) . PHP_EOL, FILE_APPEND);
self::$_log = array();
}
}
/**
* 日志输出
* @param $msg
* @param int $level
*/
public static function log($msg, $level = self::DEBUG)
{
$level = self::$LEVEL[$level];
self::$_log[] = date('[ Y-m-d H:i:s ]') . " [{$level}] " . $msg;
//file_put_contents(PATH_APP_LOG . date('Ymd') . '.log', $msg, FILE_APPEND);
}
/**
* 错误日志
* @param $msg
*/
public static function error($msg)
{
self::log($msg, self::ERROR);
}
/**
* 警告日志
* @param $msg
*/
public static function warning($msg)
{
self::log($msg, self::WARNING);
}
/**
* 调试日志
* @param $msg
*/
public static function debug($msg)
{
self::log($msg, self::DEBUG);
}
/**
* 信息日志
* @param $msg
*/
public static function info($msg)
{
self::log($msg, self::INFO);
}
}
/**
* 数据库操作
* Class DB
*/
class DB
{
private static $OP = array('=', '!=', '<>', '>', '>=', '<', '<=');
/**
* 是否输出调试日志
* @var
*/
private $_debug;
/**
* 表前缀
* @var
*/
private $_pre;
//
// /**
// * @var PDO
// */
// private $_pdo;
/**
* @param null $config
* @param bool|false $debug
* @return mixed
*/
public static function instance($config = null, $debug = false)
{
$config || $config = Config::get();
$key = md5($config['DB_TYPE'] . $config['DB_HOST'] . $config['DB_USER'] . $debug);
static $_c = array();
//isset($_c[$key]) || $_c[$key] = new self($config, $debug);
if(!isset($_c[$key])) {
$_c[$key] = ($config['DB_TYPE'] == 'sqlite') ? new Sqlite($config, $debug) : new Mysql($config, $debug);
}
return $_c[$key];
}
/**
* DB constructor.
* @param $config
* @param $debug
*/
protected function __construct($config, $debug = false)
{
$this->_pre = $config['DB_PREFIX'];
$this->_debug = $debug;
}
/**
* 调试信息
* @param $query
*/
protected function debug($query) {
$this->_debug && Logger::debug($query);
// Logger::debug($query);
}
public function query($query) {}
public function exec($query) {}
protected function lastId() {}
protected function escape($query) {}
/**
* @param $table
* @param null $where
* @param null $option
* @return bool
*/
public function find($table, $where = null, $option = null)
{
$limit = array('_limit' => 1);
!is_array($option) && $option = array('_column' => $option);
$option = array_merge($option, $limit);
unset($option['_count']);
$items = $this->select($table, $where, $option);
return $items ? $items[0] : false;
}
/**
* 选择
* @param $table
* @param null $where
* @param null $option
* @return mixed
*/
public function select($table, $where = null, $option = null)
{
ctype_alnum($where) && $where = array('id' => $where);
$where = $this->_where($where);
!is_array($option) && $option = array('_column' => $option);
$column = isset($option['_column']) ? $option['_column'] : '*';
$group = isset($option['_group']) ? ' GROUP BY ' . $option['_group'] : '';
$order = isset($option['_order']) ? ' ORDER BY ' . $option['_order'] : '';
$limit = isset($option['_limit']) ? ' LIMIT ' . $option['_limit'] : '';
$rows = $this->query('SELECT ' . $column . ' FROM ' . $this->_pre . $table . $where . $group . $order . $limit);
if (!isset($option['_count'])) {
return $rows;
}
$count = $this->query('SELECT COUNT(' . $option['_count'] . ') _count FROM ' . $this->_pre . $table . $where);
return array($rows, $count[0]['_count']);
}
/**
* 插入
* @param $table
* @param $data
* @return mixed
*/
public function insert($table, $data)
{
isset($data[0]) || $data = array($data);
$ids = array();
foreach($data as $item)
{
$item = $this->_escape($item);
$this->exec('INSERT INTO ' . $this->_pre . $table . ' (' . implode(', ', array_keys($item)) . ') VALUES (' . implode(array_values($item), ', ') . ')');
$ids[] = $this->lastId();
}
return (count($ids) == 1) ? $ids[0] : $ids;
}
/**
* 更新
* @param $table
* @param $data
* @param null $where
* @return mixed
*/
public function update($table, $data, $where = null)
{
$where || $where = array('id' => $data['id']);
$sets = array();
$data = $this->_escape($data);
foreach ($data as $key => $val) {
$sets[] = "$key = $val";
}
return $this->exec('UPDATE ' . $this->_pre . $table . ' SET ' . implode(', ', $sets) . $this->_where($where));
}
/**
* 删除
* @param $table
* @param null $where
* @return mixed
*/
public function delete($table, $where = null)
{
ctype_alnum($where) && $where = array('id' => $where);
return $this->exec('DELETE FROM ' . $this->_pre . $table . $this->_where($where));
}
/**
* WHERE条件
* @param $where
* @param string $prefix
* @return string
*/
private function _where($where, $prefix = ' WHERE ')
{
if (empty($where) || is_string($where)) {
return empty($where) ? '' : $prefix . $where;
}
$logic = isset($where['_logic']) ? strtoupper($where['_logic']) : 'AND';
isset($where['_where']) && $_where = $this->_where($where['_where'], '');
unset($where['_logic'], $where['_where']);
$items = $this->_escape($where);
$where = array();
foreach ($items as $key => $val) {
// column operator
list($col, $op) = array_pad(explode(' ', $key), 2, null);
$op = isset($op) ? strtoupper($op) : '=';
// [~]BETWEEN/IN/IS/LIKE
$not = (strpos($op, '~') === 0) ? ' NOT ' : null;
$not && ($op = substr($op, 1));
switch($op){
case 'BETWEEN':
$op = $not .$op;
$val = "{$val[0]} AND {$val[1]}";
break;
case 'IN':
$op = $not .$op;
$val = '('. (is_array($val) ? implode(',', $val) : $val) . ')';
break;
case 'IS':
$op = $op .$not;
$val = ($val == "''") ? 'NULL' : $val;
break;
case 'LIKE':
$op = $not .$op;
$val = "CONCAT('%', {$val}, '%')";
break;
default:
!in_array($op, self::$OP) && ($op = false);
break;
}
$op && $where[] = "$col $op $val";
}
isset($_where) && $where[] = $_where;
return $where ? $prefix . '(' . implode(" {$logic} ", $where) . ')' : '';
}
/**
* 参数转义,根据字段判断是否需要转义
* #column => value 不需要转义
* column => value 需要转义
* @param $data
* @return array
*/
private function _escape($data)
{
$items = array();
$fun_quote = array($this, 'escape');
foreach($data as $col => $val)
{
// 如果字段以#开头则不需要转义,否则需要转义
(strpos($col, '#') === 0) ? ($col = substr($col, 1)) : ($val = is_array($val) ? array_map($fun_quote, $val) : $this->escape($val));
$items[$col] = $val;
}
return $items;
}
}