forked from itziy/phpframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel.class.php
More file actions
605 lines (531 loc) · 14.5 KB
/
Kernel.class.php
File metadata and controls
605 lines (531 loc) · 14.5 KB
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
<?php
/**
* 核心类Kernel类
* @filename Kernel.class.php
* @touch date 2014-07-23 16:47:33
* @author Rain<563268276@qq.com>
* @copyright 2014 http://www.94cto.com/
* @license http://www.apache.org/licenses/LICENSE-2.0 LICENSE-2.0
* @package Rain PHP Frame(RPF)
*/
defined('RPF_PATH') or exit();
/**
* 核心类Kernel类
*/
class Kernel
{
/**
* 配置信息存储变量
*/
static $_conf = array();
/**
* 语言包信息存储变量
*/
static $_lang = array();
/**
* controller名称存储变量
*/
static $_controller = null;
/**
* action名称存储变量
*/
static $_action = null;
/**
* app名称存储变量
*/
static $_app = null;
/**
* 获取当前的action名称
*/
public static function getAction()
{
return self::$_action;
}
/**
* 获取当前的controller名称
*/
public static function getController()
{
return self::$_controller;
}
/**
* 获取当前的app名称
*/
public static function getApp()
{
return self::$_app;
}
/**
* Kernel类的核心启动框架的方法
*/
public static function start()
{
//加载配置信息,用户自定义的配置会覆盖系统的配置
self::loadConf();
//加载语言包
self::loadLang();
//自动加载类库
spl_autoload_register('Kernel::autoload');
//自动加载用户自定义的函数库
self::loadfunc();
//初始化session
self::session();
//自动的解析URL分发
self::parseurl();
//脚本退出注册函数
register_shutdown_function('shutdown');
//目录检测和自动生成,为了效率此函数仅仅执行一次
self::mkdirs();
//是否创建demo例子程序
$lockfile = TEMP_PATH.'build_demo.lock';
if (C_DEMO && !is_file($lockfile))
{
Demo::cdemo();
touch($lockfile);
}
$con_name = self::$_controller.self::$_conf['C_NAME'];
$act_name = self::$_controller.'_'.self::$_action.self::$_conf['A_NAME'];
if (!class_exists($con_name))
{
if (DEBUG)
die(self::$_lang['_SYS_LANG_CLASS_NOT_FIND'].' : '.$con_name);
else
die(self::$_lang['_SYS_LANG_CLASS_NOT_FIND']);
}
if (!class_exists($act_name))
{
if (DEBUG)
die(self::$_lang['_SYS_LANG_CLASS_NOT_FIND'].' : '.$act_name);
else
die(self::$_lang['_SYS_LANG_CLASS_NOT_FIND']);
}
$con_obj = new $con_name();
$act_obj = new $act_name();
$con_obj->init();
$act_obj->init();
$act_obj->run();
}
/**
* 初始化session
*/
private static function session()
{
//init session save type
if (extension_loaded('memcache') && self::$_conf['SESSION_SAVE_TYPE'] == 'm')
{
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://'.self::$_conf['MEM_HOST'].':'.self::$_conf['MEM_PORT']);
}
Session::sid(self::$_conf['S_ID']);
Session::name(self::$_conf['S_NAME']);
Session::expire(self::$_conf['S_EXPIRE']);
session_start();
}
/**
* 递归创建框架所需的目录
*/
private static function mkdirs()
{
$lockfile = TEMP_PATH.'build_dir.lock';
if (is_file($lockfile))
return;
$dirArr = array(
APP_CONF,
APP_LANG,
APP_FUNC,
HTML_PATH,
TEMP_PATH,
DATA_PATH,
CACHE_PATH,
APP_M,
APP_V,
APP_C,
APP_A,
APP_P,
);
mkdirs($dirArr);
//拷贝配置文件到应用程序的配置目录下
copy(SYS_CONF.'main'.CONF_EXT, APP_CONF.'main'.CONF_EXT);
touch($lockfile);
}
/**
* 该方法实现了自动加载类库的功能,在使用类似new操作时候,将自动调用此方法
* @param string $cls 类名称
* @return bool 返回同import函数的返回值 , 加载类库失败程序停止执行
*/
public static function autoload($cls)
{
$sysClassArr = self::sysClassCache();
if (isset($sysClassArr[$cls]))
return import($sysClassArr[$cls]);
if (!self::loadUserClass($cls))
{
if (DEBUG)
die(self::$_lang['_SYS_LANG_CLASS_NOT_FIND'].' : '.$cls);
else
die(self::$_lang['_SYS_LANG_CLASS_NOT_FIND']);
}
}
/**
* 实现URL的解析和分发
*/
private static function parseurl()
{
switch (self::$_conf['URL_MODEL'])
{
case URL_COMMON: //普通URL模式
self::url_common();
break;
case URL_PATHINFO: //PATHINFO模式
self::url_pathinfo();
break;
case URL_REWRITE: //REWRITE模式
self::url_rewrite();
break;
default: //默认使用兼容模式URL_COMPAT
self::url_compat();
}
}
/**
* 解析URL为rewrite模式,由于未找到合适的判断rewrite模块是否支持函数,所以仅仅对使用apache的服务器做了rewrite检测,使用nginx/iis等服务器的请自己测试
*/
private static function url_rewrite()
{
//apache_get_modules
if ($_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != $_SERVER['PHP_SELF'] && (function_exists('apache_get_modules') && !in_array('mod_rewrite', apache_get_modules())))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_ERROR']);
self::$_controller = 'Index';
self::$_action = 'index';
self::$_app = 'index';
if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != $_SERVER['PHP_SELF'])
{
$tmpArr = array_values(array_filter(explode('/', str_replace('?', '/', str_replace('=', '/', str_replace('&', '/', trim($_SERVER['REQUEST_URI'])))))));
if (count($tmpArr) < 3)
die(self::$_lang['_SYS_LANG_URL_PARAMETER_ERROR']);
self::$_app = trim($tmpArr[0]);
self::$_controller = ucfirst(trim($tmpArr[1]));
self::$_action = trim($tmpArr[2]);
unset($tmpArr[0], $tmpArr[1], $tmpArr[2]);
if (!isword(self::$_app))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.self::$_app);
//判断入口文件是否存在
if (!is_file(str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php'))
{
if (DEBUG)
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND'].': '.str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php');
else
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND']);
}
//end
if (!empty($tmpArr) && count($tmpArr) > 0)
{
$tmpArr = array_values($tmpArr);
foreach ($tmpArr as $k => $v)
{
if ($k % 2 == 0)
{
//判断key的部分
if (!isword($v))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.$v);
if (!isset($_GET[$v]))
{
if (isset($tmpArr[$k + 1]))
$_GET[$v] = $tmpArr[$k + 1];
else
$_GET[$v] = null;
if (!isset($_REQUEST[$v]))
$_REQUEST[$v] = $_GET[$v];
}
}
}
}
unset($tmpArr);
}
}
/**
* 解析URL为兼容模式
*/
private static function url_compat()
{
if ($_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != $_SERVER['PHP_SELF'] && !isset($_GET['s']))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_ERROR']);
self::$_controller = 'Index';
self::$_action = 'index';
self::$_app = 'index';
if (isset($_GET['s']))
{
$tmpArr = array_values(array_filter(explode('/', trim($_GET['s']))));
if (count($tmpArr) < 3)
die(self::$_lang['_SYS_LANG_URL_PARAMETER_ERROR']);
self::$_controller = ucfirst(trim($tmpArr[1]));
self::$_action = trim($tmpArr[2]);
self::$_app = trim($tmpArr[0]);
unset($tmpArr[0], $tmpArr[1], $tmpArr[2]);
if (!isword(self::$_app))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.self::$_app);
//判断入口文件是否存在
if (!is_file(str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php'))
{
if (DEBUG)
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND'].': '.str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php');
else
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND']);
}
//end
if (!empty($tmpArr) && count($tmpArr) > 0)
{
$tmpArr = array_values($tmpArr);
foreach ($tmpArr as $k => $v)
{
if ($k % 2 == 0)
{
//判断key的部分
if (!isword($v))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.$v);
if (!isset($_GET[$v]))
{
if (isset($tmpArr[$k + 1]))
$_GET[$v] = $tmpArr[$k + 1];
else
$_GET[$v] = null;
if (!isset($_REQUEST[$v]))
$_REQUEST[$v] = $_GET[$v];
}
}
}
}
unset($tmpArr, $_GET['s']);
}
}
/**
* 解析URL为普通模式
*/
private static function url_common()
{
if ($_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != $_SERVER['PHP_SELF'] && (!isset($_GET['act']) || !isset($_GET['app']) || !isset($_GET['con'])))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_ERROR']);
self::$_controller = 'Index';
self::$_action = 'index';
self::$_app = 'index';
if (isset($_GET['con']))
{
self::$_controller = trim($_GET['con']);
if (!isword(self::$_controller))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.self::$_controller);
}
if (isset($_GET['app']))
{
self::$_app = trim($_GET['app']);
if (!isword(self::$_app))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.self::$_app);
//判断入口文件是否存在
if (!is_file(str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php'))
{
if (DEBUG)
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND'].': '.str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php');
else
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND']);
}
//end
}
if (isset($_GET['act']))
{
self::$_action = trim($_GET['act']);
if (!isword(self::$_action))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.self::$_action);
}
}
/**
* 解析URL为pathinfo模式,如果服务器不支持pathinfo模式,停止执行程序
*/
private static function url_pathinfo()
{
if ($_SERVER['REQUEST_URI'] != '/' && $_SERVER['REQUEST_URI'] != $_SERVER['PHP_SELF'] && !isset($_SERVER['PATH_INFO']))
die(self::$_lang['_SYS_LANG_NOT_SUPPORT_PATHINFO']);
self::$_controller = 'Index';
self::$_action = 'index';
self::$_app = 'index';
if (isset($_SERVER['PATH_INFO']))
{
$tmpArr = array_values(array_filter(explode('/',str_replace('?', '/', str_replace('=', '/', str_replace('&', '/', trim($_SERVER['PATH_INFO'])))))));
if (count($tmpArr) < 3)
die(self::$_lang['_SYS_LANG_URL_PARAMETER_ERROR']);
self::$_app = trim($tmpArr[0]);
self::$_controller = ucfirst(trim($tmpArr[1]));
self::$_action = trim($tmpArr[2]);
unset($tmpArr[0], $tmpArr[1], $tmpArr[2]);
if (!isword(self::$_app))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.self::$_app);
//判断入口文件是否存在
if (!is_file(str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php'))
{
if (DEBUG)
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND'].': '.str_replace(APP_NAME.'/', '', APP_PATH).self::$_app.'.php');
else
die(self::$_lang['_SYS_LANG_FILE_NOT_FIND']);
}
//end
if (!empty($tmpArr) && count($tmpArr) > 0)
{
$tmpArr = array_values($tmpArr);
foreach ($tmpArr as $k => $v)
{
if ($k % 2 == 0)
{
//判断key的部分
if (!isword($v))
die(self::$_lang['_SYS_LANG_URL_PARAMETER_VALUE_INVALID'].': '.$v);
if (!isset($_GET[$v]))
{
if (isset($tmpArr[$k + 1]))
$_GET[$v] = $tmpArr[$k + 1];
else
$_GET[$v] = null;
if (!isset($_REQUEST[$v]))
$_REQUEST[$v] = $_GET[$v];
}
}
}
}
unset($tmpArr);
}
}
/**
* 自动加载配置项
*/
private static function loadConf()
{
if (is_array(self::$_conf) && !empty(self::$_conf))
return;
static $confPath = array(
SYS_CONF, // 系统的默认配置的目录
APP_CONF, // 应用程序的默认配置的目录
);
foreach ($confPath as $cp)
{
$dirArr = read_dir($cp);
if (is_array($dirArr) && !empty($dirArr))
{
foreach ($dirArr as $file)
{
if (strstr($file, CONF_EXT) != CONF_EXT)
continue;
self::$_conf = array_merge(self::$_conf, import($file));
}
}
unset($dirArr);
}
}
/**
* 自动加载语言包
*/
private static function loadLang()
{
if (is_array(self::$_lang) && !empty(self::$_lang))
return;
static $confPath = array(
SYS_LANG, // 系统的默认语言包目录
APP_LANG, // 应用程序的默认语言包目录
);
foreach ($confPath as $cp)
{
$dirArr = read_dir($cp);
if (is_array($dirArr) && !empty($dirArr))
{
foreach ($dirArr as $file)
{
if (strstr($file, self::$_conf['LANG'].CONF_EXT) != self::$_conf['LANG'].CONF_EXT)
continue;
self::$_lang = array_merge(self::$_lang, import($file));
}
}
unset($dirArr);
}
}
/**
* 自动加载用户自定义的类库文件
* @param string $cls 类名称
* @return bool 成功返回true,失败返回false
*/
private static function loadUserClass($cls)
{
//判断是否model
if (strpos($cls, self::$_conf['M_NAME']) !== false)
{
$file = APP_M.str_replace(self::$_conf['M_NAME'], '', $cls).CLS_M_EXT;
if (!is_file($file))
return false;
import($file);
return true;
}
//判断是否controller
if (strpos($cls, self::$_conf['C_NAME']) !== false)
{
$file = APP_C.str_replace(self::$_conf['C_NAME'], '', $cls).CLS_C_EXT;
if (!is_file($file))
return false;
import($file);
return true;
}
//判断是否action
if (strpos($cls, self::$_conf['A_NAME']) !== false)
{
$file = APP_A.self::$_controller.'/'.str_replace(self::$_conf['A_NAME'], '', $cls).CLS_A_EXT;
if (!is_file($file))
return false;
import($file);
return true;
}
return false;
}
/**
* 自动加载用户自定义的函数库文件
*/
private static function loadfunc()
{
$dirArr = read_dir(APP_FUNC);
if (is_array($dirArr) && !empty($dirArr))
{
foreach ($dirArr as $file)
{
if (strstr($file, FUNC_EXT) != FUNC_EXT)
continue;
import($file);
}
}
unset($dirArr);
}
/**
* 该方法实现了自动缓存系统类库代码路径到内存
*/
private static function sysClassCache()
{
static $classArr = array();
static $sys_dir = array(
SYS_KERNEL, // 加载系统核心代码目录SYS_KERNEL下的所有class文件,支持多级目录递归,区分大小写
SYS_LIB, // 加载类库代码SYS_LIB
SYS_CORE, // 核心类库代码SYS_CORE
SYS_VENDOR, // 框架引入的第三方类库代码SYS_VENDOR
);
if (!DEBUG && !empty($classArr))
return $classArr;
foreach ($sys_dir as $sd)
{
if (is_dir($sd))
{
$dirArr = read_dir($sd);
if (is_array($dirArr) && !empty($dirArr))
{
foreach ($dirArr as $file)
{
if (strstr($file, CLS_EXT) != CLS_EXT)
continue;
$classArr[basename($file, CLS_EXT)] = $file;
}
}
unset($dirArr);
}
}
return $classArr;
}
}