-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.php
280 lines (250 loc) · 9.4 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
<?php
/**
* Phx - A Micro RESTful PHP Framework For Beginners with one day learning curve
*
* @author PHOENIX <gopher.huang@gmail.com>
* @link https://github.com/phoenixg/phx
*
* ///, //// /
* \ /, / >.
* \ /, _/ /.
* \_ /_/ /. It's more like an eagle than phoenix,
* \__/_ < if you find a better ascii picture,
* /<<< \_\_ it's welcome to make a pull request
* /,)^>>_._ \
* (/ \\ /\\\
* // ```` Now let's fly!
*/
/*
*---------------------------------------------------------------
* DEFINE ALL CONSTANTS WE NEED
*---------------------------------------------------------------
* 常量定义的命名约定:
* 文件夹以PATH_* 开头,文件以FILE_* 开头,依此类推
*/
// 定义文件夹的路径
define('DS', DIRECTORY_SEPARATOR);
define('PATH_BASE', __DIR__ . DS);
define('PATH_APP', PATH_BASE . 'app' . DS);
define('PATH_APP_C', PATH_BASE . 'app' . DS . 'mvc' . DS . 'controllers' . DS);
define('PATH_APP_M', PATH_BASE . 'app' . DS . 'mvc' . DS . 'models' . DS);
define('PATH_ASSETS', PATH_BASE . 'assets' . DS);
define('PATH_LOGS', PATH_APP . 'logs' . DS);
define('PATH_CORE', PATH_BASE . 'core' . DS);
define('PATH_CORE_LIBS', PATH_BASE . 'core' . DS . 'libs' . DS);
define('PATH_CORE_HELPERS', PATH_BASE . 'core' . DS . 'helpers' . DS);
define('PATH_CORE_DEBUG', PATH_BASE . 'core' . DS . 'debugger' . DS);
define('PATH_CORE_COMMONS', PATH_BASE . 'core' . DS . 'commons' . DS);
define('PATH_CORE_PLUGINS', PATH_BASE . 'core' . DS . 'plugins' . DS);
// 定义文件的路径
define('EXT', '.php');
define('FILE_BASE', PATH_BASE . 'index' . EXT);
define('FILE_LOG', PATH_LOGS . date('Y-m-d') . '.log');
// 定义跨平台的行尾结束符
define('EOL', PHP_EOL);
/*
*---------------------------------------------------------------
* INCLUDE COMMON FUNCTIONS AND LET'S DO SOME SAFETY JOBS FIRST
*---------------------------------------------------------------
*/
require PATH_CORE_COMMONS . 'commons.php';
// turn off register_globals
unregister_GLOBALS();
// turn off magic quotes
@set_magic_quotes_runtime(0);
if(get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : (isset($value) ? stripslashes($value) : null);
return $value;
}
$_POST = stripslashes_deep($_POST);
$_GET = stripslashes_deep($_GET);
$_COOKIE = stripslashes_deep($_COOKIE);
}
/*
*---------------------------------------------------------------
* INCLUDE ALL CORE LIB FILES
*---------------------------------------------------------------
*/
$lib_files = glob(PATH_CORE_LIBS . '*' . EXT);
foreach ($lib_files as $file) {
require $file;
unset($file);
}
unset ($lib_files);
/*
*---------------------------------------------------------------
* RETRIEVE CONFIGURATION ARRAY FROM CONFIGURATION FILES
*---------------------------------------------------------------
*/
$config_files = glob(PATH_APP . 'config' . DS . '*' . EXT);
$config = array();
foreach ($config_files as $config_file) {
$key = substr(strrchr($config_file, DS), 1, -strlen(EXT));
$config[$key] = include $config_file;
unset($key);
unset($config_file);
}
unset($config_files);
/*
*---------------------------------------------------------------
* INITIALIZE CONFIG CLASS
*---------------------------------------------------------------
* Retrieve config item via: $CFG::get('application.aaa.ddd.eee');
* 使用了单例模式
* 一旦实例化了配置类,我们就能做很多事了
*/
$CFG = Config::getInstance($config);
unset($config);
/*
*---------------------------------------------------------------
* SET DEFAULT TIMEZONE
*---------------------------------------------------------------
*/
date_default_timezone_set($CFG::get('application.timezone'));
/*
*---------------------------------------------------------------
* SET ERROR REPORTING LEVEL
*---------------------------------------------------------------
*/
if ($CFG::get('application.error_reporting') === true) {
ini_set('display_errors','On');
error_reporting(E_ALL);
} else {
ini_set('display_errors','Off');
error_reporting(0);
}
/*
*---------------------------------------------------------------
* SET DEBUGGER
*---------------------------------------------------------------
*/
if ($CFG::get('application.debug') === true) {
$debug_tool = & $CFG::get('application.debug_tool');
switch ($debug_tool) {
case 'dbug':
require PATH_CORE_DEBUG . 'dBug' . DS .'dBug' . EXT;
break;
case 'kint':
require PATH_CORE_DEBUG . 'kint' . DS .'Kint.class' . EXT;
break;
default:
break;
}
}
// 异常和错误都使用了 error_log() 来写错误
// 只有用户自己的log才使用日志类来写
/*
*---------------------------------------------------------------
* SET ERROR HANDLER
*---------------------------------------------------------------
*
* 全部预定义的Error常量对应的数值(即$errorNo)见:http://php.net/manual/en/errorfunc.constants.php
* error发生时自动触发,或手动触发:trigger_error("发生了一个错误")
*/
set_error_handler(function ($errorNo, $errMsg, $errFilePath, $errLine){
$logInfo = '['.date('Y-m-d H:i:s').'] Error: '.$errMsg.', on line: '.$errLine.', in file: '.$errFilePath.EOL;
echo $logInfo;
error_log($logInfo, 3, FILE_LOG);
});
/*
*---------------------------------------------------------------
* SET EXCEPTION HANDLER FOR EXCEPTION WITHOUT TRY...CATCH...
*---------------------------------------------------------------
*
* 设置一个异常处理器把异常写进log里
* 专门用于处理 try...catch... 之外的异常
* 可使用 throw new Exception('异常信息') 手动触发
*/
set_exception_handler(function ($e) {
$logInfo = '['.date("Y-m-d H:i:s").'] Exception on line: '.$e->getLine().', in file: '.$e->getFile()
.', with message: '.$e->getMessage().EOL;
echo $logInfo;
error_log($logInfo, 3, FILE_LOG);
});
/*
*---------------------------------------------------------------
* SET EXCEPTION HANDLER FOR EXCEPTION WITHIN TRY...CATCH...
*---------------------------------------------------------------
*
* 设置一个用于标识的异常处理器:Phxexception
* try {
* throw new Phxexception("异常信息");
* } catch(Phxexception $e) {
* echo $e->getMsg();
* }
*/
class Phxexception extends Exception
{
public function __construct($message) {
parent::__construct($message);
}
// 这里不打印,是因为会在catch里进行打印
public function getMsg()
{
$logInfo = '['.date("Y-m-d H:i:s").'] Phxexception on line: '.$this->getLine().', in file: '.$this->getFile()
.', with message: '.$this->getMessage().EOL;
error_log($logInfo, 3, FILE_LOG);
return $logInfo;
}
}
/*
*---------------------------------------------------------------
* INCLUDE IoC CONTAINER
*---------------------------------------------------------------
* Append your own IoC class in ioc.php file
*/
include 'ioc'.EXT;
/*
*---------------------------------------------------------------
* RESOLVE ALL CLASSES
*---------------------------------------------------------------
* $test = IoC::resolve('classname');
* 附带一个simpleexcel做演示?
*/
//$test = IoC::resolve('classname');
/*
*---------------------------------------------------------------
* SET CONTROLLER AND MODEL CLASSES TO BE AUTOLOAD
*---------------------------------------------------------------
* 参考:http://php.net/manual/en/function.spl-autoload-register.php
* $classname eg. Default_Controller
*/
spl_autoload_register(function ($classname){
try {
$fileFound = false;
$filename = PATH_APP_C.strtolower($classname).EXT;
if( is_file($filename) ){
$fileFound = true;
include $filename;
}
$filename = PATH_APP_M.strtolower($classname).EXT;
if( is_file($filename) ){
$fileFound = true;
include $filename;
}
if($fileFound = false) {
throw new Phxexception("无法自动加载该类:".$filename);
}
} catch(Phxexception $e) {
echo $e->getMsg();
}
});
/*
*---------------------------------------------------------------
* INCLUDE php-o
*---------------------------------------------------------------
*/
if ($CFG::get('application.php-o') === true) {
include PATH_CORE_PLUGINS . 'php-o' . DS . 'O.php';
}
/*
*---------------------------------------------------------------
* ROUTE URI TO CONTROLLER/METHOD
*---------------------------------------------------------------
* eg. index.php?c=default&a=index [decrypted]
* eg. index.php/default/hello/param1/value1/param2/value2
*/
require 'frontcontroller.php';
$frontController = FrontController::getInstance();
$frontController->route();