-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.php
400 lines (371 loc) · 10.2 KB
/
Application.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
<?php
define('APP_START_TIME', microtime(true));
/**
*
* -+-----------------------------------
* |PHP5 Framework - 2011
* |Web Site: www.iblue.cc
* |E-mail: [email protected]
* |Date: 2011-12-15
* -+-----------------------------------
*
* @desc 应用程序入口类
* @author jingke
*/
class XF_Application
{
/**
* 当前实例
* @var XF_Application
*/
private static $_instance = null;
/**
* Bootstrap
* @var XF_Application_Bootstrap
*/
protected $_bootstrap;
/**
* 当前是否为命令行模式运行
* @var bool
*/
private $_is_command_line = FALSE;
/**
* 加载所有文件总时间
* @var int
*/
private $_load_file_time = 0;
private function __construct(){}
private function __clone(){}
/**
* 获取实例
* @access public
* @param bool $isCommandLine 是否为命令行模式? 默认为 FALSE
* @param bool $is_compile_model 是否编译框架代码再运行?默认为TRUE
* @return XF_Application
*/
public static function getInstance($isCommandLine = FALSE, $is_compile_model = TRUE)
{
if (self::$_instance === null)
{
self::$_instance = new self();
self::$_instance->_init($isCommandLine, $is_compile_model);
}
return self::$_instance;
}
/**
* 初始化
* @access private
* @return void
*/
private function _init($isCommandLine, $is_compile_model)
{
$this->_is_command_line = $isCommandLine;
if ($isCommandLine === FALSE)
{
$this->_runTime($is_compile_model);
//设定错误和异常处理
register_shutdown_function(array('XF_Application','fatalError'));
set_error_handler(array('XF_Application','appError'));
set_exception_handler(array('XF_Application','appException'));
XF_Loader_Autoloader::getInstance();
$this->_setPHPSettings(XF_Config::getInstance()->getPHPSettings());
}
else
{
$this->_runCommandLineTime($is_compile_model);
XF_Loader_Autoloader::getInstance();
}
}
/**
* 脚本解析错误的处理方法
* @param public
* @return void
*/
public static function fatalError()
{
if ($e = error_get_last())
{
$env = XF_Config::getInstance()->getEnvironmental();
$req = XF_Controller_Request_Http::getInstance();
$url = $req->getMethod().' "'.$req->getRequestUrl().'"';
switch($e['type'])
{
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
ob_end_clean();
$message = $url."\n".'ERROR: '.$e['message']. ' in '.$e['file'].' on line '.$e['line'];
if ($env == 'development')
{
exit($message);
}
else
{
XF_Functions::writeErrLog($message);
XF_Controller_Plugin_Manage::getInstance()->exception(XF_Controller_Request_Http::getInstance(), new XF_Exception($e['message']));
}
break;
}
}
}
/**
* 普通错误,警告
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @throws XF_Exception
*/
public static function appError($errno, $errstr, $errfile, $errline)
{
$env = XF_Config::getInstance()->getEnvironmental();
$req = XF_Controller_Request_Http::getInstance();
$url = $req->getMethod().' "'.$req->getRequestUrl().'"';
switch ($errno)
{
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
$message = $url."\n".'ERROR: '.$errstr. ' in '.$errfile.' on line '.$errline;
if ($env == 'development')
{
exit($message);
}
else
{
XF_Functions::writeErrLog($message);
XF_Controller_Plugin_Manage::getInstance()->exception(XF_Controller_Request_Http::getInstance(), new XF_Exception($e['message']));
}
break;
/*case E_STRICT:
case E_USER_WARNING:
case E_USER_NOTICE:
default:
$message = 'ERROR: ['.$errno.']'.$errstr. ' in '.$errfile.' on line '.$errline;
XF_Functions::writeErrLog($message);
if ($env == 'development')
{
echo '<br/>'.$message;
}
break;*/
}
}
/**
* 全局异常处理
* @param Exception $e
* @throws Exception
* @throws XF_Exception
*/
public static function appException(Exception $e)
{
$req = XF_Controller_Request_Http::getInstance();
$message = $req->getMethod().' "'.$req->getRequestUrl().'" '.$e->getMessage()."\n";
XF_Functions::writeErrLog($message.$e->getTraceAsString());
echo $e;
}
/**
* 是否命令行状态
* @access public
* @return bool
*/
public function isCommandLine()
{
return $this->_is_command_line;
}
/**
* 获取Bootstrap
* @access public
* @return XF_Application_Bootstrap
*/
public function getBootstrap()
{
if (NULL === $this->_bootstrap)
{
$this->_bootstrap = new XF_Application_Bootstrap();
}
return $this->_bootstrap;
}
/**
* 设置Bootstrap
* @access public
* @param string $bootstrapClassName bootstrap Class name.
* @throws XF_Application_Exception
* @return XF_Application
*/
public function setBootstrap($bootstrapClassName = null)
{
if (NULL === $bootstrapClassName)
return $this;
$file = APPLICATION_PATH.'/bootstraps/'.ucfirst($bootstrapClassName).'.php';
if(is_file($file))
{
require $file;
$bootstrapClassName = $bootstrapClassName.'Bootstrap';
if(!class_exists($bootstrapClassName, false))
throw new XF_Application_Exception($bootstrapClassName.' 不存在');
$this->_bootstrap = new $bootstrapClassName($this);
if (!$this->_bootstrap instanceof XF_Application_Bootstrap)
throw new XF_Application_Exception($bootstrapClassName.' 不是一个有效的对象');
}
else
throw new XF_Application_Exception('Bootstrap 不存在 '.$file);
return $this;
}
/**
* 加载框架运行环境
* @param bool $is_compile_model 是否编译框架代码再运行?默认为TRUE
* @return void
*/
private function _runTime($is_compile_model = TRUE)
{
if ($is_compile_model !== TRUE)
{
require XF_PATH.'/Loader/Autoloader.php';
return;
}
$file = TEMP_PATH.'/RunTime.php';
if (!is_file($file))
{
$cores = array(
'Loader/Autoloader.php',
'DataPool.php',
'Functions.php',
'Config.php',
'Application/Bootstrap.php',
'Controller/Front.php',
'Controller/Router/Interface.php',
'Controller/Router/Abstract.php',
'Controller/Router.php',
'Controller/Request/Abstract.php',
'Controller/Plugin/Manage.php',
'Controller/Request/Http.php',
'Controller/Plugin/Abstract.php',
'View.php',
'File.php',
'String.php',
'Controller/Abstract.php',
'Controller/Interface.php',
'Cache/Interface.php',
'Cache/Abstract.php',
'Cache/SECache.php',
'Db/Table/Abstract.php',
'Db/Tool.php',
'Db/Table/ValidateRule.php',
'Db/Table/Select/Interface.php',
'Db/Table/Select/Abstract.php',
'Db/Table/Select/Mysql.php',
'Db/Config/Interface.php',
'Db/Drive/Abstract.php',
'Db/Drive/Mysql.php',
'View/Helper.php',
'View/Helper/Header/Title.php',
'View/Helper/Header/Link.php',
'View/Helper/Header/Meta.php',
'View/Helper/Header/Stylesheet.php',
'View/Helper/Header/Script.php',
);
$phpcode = '<?php';
foreach ($cores as $c)
{
$code = php_strip_whitespace(XF_PATH.'/'.$c);
$phpcode .= str_replace('<?php', '', $code);
}
file_put_contents($file, $phpcode);
}
require $file;
}
/**
* 加载命令行下的框架运行环境
* @param bool $is_compile_model 是否编译框架代码再运行?默认为TRUE
* @return void
*/
private function _runCommandLineTime($is_compile_model = TRUE)
{
if ($is_compile_model === FALSE)
{
require XF_PATH.'/Loader/Autoloader.php';
return;
}
$file = TEMP_PATH.'/~RunTime.php';
if (!is_file($file))
{
$cores = array(
'Loader/Autoloader.php',
'DataPool.php',
'Functions.php',
'Config.php',
'File.php',
'String.php',
'Cache/Interface.php',
'Cache/Abstract.php',
'Cache/SECache.php',
'Db/Table/Abstract.php',
'Db/Tool.php',
'Db/Table/ValidateRule.php',
'Db/Table/Select/Interface.php',
'Db/Table/Select/Abstract.php',
'Db/Table/Select/Mysql.php',
'Db/Config/Interface.php',
'Db/Config.php',
'Db/Drive/Abstract.php',
'Db/Drive/Mysql.php'
);
$phpcode = '<?php';
foreach ($cores as $c)
{
$code = php_strip_whitespace(XF_PATH.'/'.$c);
$phpcode .= str_replace('<?php', '', $code);
}
file_put_contents($file, $phpcode);
}
require $file;
}
/**
* 添加文件加载时间
* @param int $number
* @return void
*/
public function addLoadFileTime($number)
{
$this->_load_file_time += floatval($number);
}
/**
* 获取文件加载的总时间
* @return float
*/
public function loadFileTime()
{
return $this->_load_file_time;
}
/**
* 启动框架
* @return void
*/
public function run()
{
XF_DataPool::getInstance()->add('RunApplication', sprintf("%.6f", microtime(true) - APP_START_TIME));
//如果是命令行模式则直接返回
if ($this->_is_command_line === TRUE) return;
$this->getBootstrap()->run();
}
/**
* 设置php环境
* @access private
* @param array $var
* @return void
*/
private function _setPHPSettings($var)
{
if(is_array($var))
{
foreach($var as $key => $val)
{
ini_set($key, $val);
}
}
}
}