Basic.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年11月4日
  7. * 系统基础类
  8. */
  9. namespace core\basic;
  10. class Basic
  11. {
  12. protected static $models = array();
  13. // 实现类文件自动加载
  14. public static function autoLoad($className)
  15. {
  16. if (substr($className, 0, 4) == 'core') { // 框架类文件命名空间转换
  17. $class_file = CORE_PATH . '/' . str_replace('\\', '/', substr($className, 5)) . '.php';
  18. } elseif (substr($className, 0, 3) == 'app') { // 应用类文件命名空间转换
  19. $class_file = APP_PATH . '/' . str_replace('\\', '/', substr($className, 4)) . '.php';
  20. } elseif (strpos($className, '\\')) { // 如果带有命名空间,使用全路径载入
  21. $class_file = ROOT_PATH . '/' . str_replace('\\', '/', $className) . '.php';
  22. } else { // 默认载入内核基础目录下文件
  23. $class_file = CORE_PATH . '/basic/' . $className . '.php';
  24. }
  25. if (! file_exists($class_file)) {
  26. error('自动加载类文件时发生错误,类名【' . $className . '】,文件:【' . $class_file . '】');
  27. }
  28. require $class_file;
  29. }
  30. // 自定义错误函数
  31. public static function errorHandler($errno, $errstr, $errfile, $errline)
  32. {
  33. if (! (error_reporting() & $errno)) {
  34. // 如果这个错误类型没有包含在error_reporting里,如加了@的错误则不报告
  35. return;
  36. }
  37. switch ($errno) {
  38. case E_ERROR:
  39. $err_level = 'ERROR';
  40. break;
  41. case E_WARNING:
  42. $err_level = 'WARNING';
  43. break;
  44. case E_PARSE:
  45. $err_level = 'PARSE';
  46. break;
  47. case E_NOTICE:
  48. $err_level = 'NOTICE';
  49. break;
  50. case E_RECOVERABLE_ERROR:
  51. case E_CORE_ERROR:
  52. case E_COMPILE_ERROR:
  53. case E_USER_ERROR:
  54. $err_level = 'FATAL ERROR';
  55. break;
  56. default:
  57. $err_level = 'UNKNOW';
  58. break;
  59. }
  60. $info = "<h3>$err_level:</h3>\n";
  61. $info .= "<p><b>Code:</b> $errno;</p>\n";
  62. $info .= "<p><b>Desc:</b> $errstr;</p>\n";
  63. $info .= "<p><b>File:</b> $errfile;</p>\n";
  64. $info .= "<p><b>Line:</b> $errline;</p>\n";
  65. if ($err_level == 'WARNING' || $err_level == 'NOTICE') {
  66. echo $info;
  67. } else {
  68. error($info);
  69. }
  70. }
  71. // 异常捕获
  72. public static function exceptionHandler($exception)
  73. {
  74. error("程序运行异常: " . $exception->getMessage() . ",位置:" . $exception->getFile() . ',第' . $exception->getLine() . '行。');
  75. }
  76. // 致命错误捕获
  77. public static function shutdownFunction()
  78. {
  79. $error = error_get_last();
  80. define('E_FATAL', E_ERROR | E_RECOVERABLE_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR);
  81. if ($error && ($error["type"] === ($error["type"] & E_FATAL))) {
  82. $errno = $error["type"];
  83. $errstr = $error["message"];
  84. $errfile = $error["file"];
  85. $errline = $error["line"];
  86. self::errorHandler($errno, $errstr, $errfile, $errline);
  87. }
  88. }
  89. // 会话处理程序设置
  90. public static function setSessionHandler()
  91. {
  92. if (ini_get('session.auto_start')) {
  93. return;
  94. }
  95. // 配置会话安全参数
  96. session_name('PbootSystem');
  97. ini_set("session.use_trans_sid", 0);
  98. ini_set("session.use_cookies", 1);
  99. ini_set("session.use_only_cookies", 1);
  100. session_set_cookie_params(0, SITE_DIR . '/', null, null, true);
  101. switch (Config::get('session.handler')) {
  102. case 'memcache':
  103. if (! extension_loaded('memcache'))
  104. error('PHP运行环境未安装memcache.dll扩展!');
  105. ini_set("session.save_handler", "memcache");
  106. ini_set("session.save_path", Config::get('seesion.path'));
  107. break;
  108. default:
  109. if (Config::get('session_in_sitepath')) {
  110. $save_path = RUN_PATH . '/session/';
  111. if (! check_dir($save_path, true))
  112. error('设置的会话目录创建失败!' . $save_path);
  113. ini_set("session.save_handler", "files");
  114. $depth = 1;
  115. ini_set("session.save_path", $depth . ';' . $save_path);
  116. if (! is_dir($save_path . '/0/0') || ! is_dir($save_path . '/v/v')) {
  117. create_session_dir($save_path, $depth);
  118. }
  119. }
  120. break;
  121. }
  122. }
  123. // 实例化模型
  124. public static function createModel($name = null, $new = false)
  125. {
  126. // 自动同名模型控制器
  127. if (! $name)
  128. $name = C;
  129. // 获取类名
  130. if (strpos($name, '.') !== false) {
  131. $path = explode('.', $name);
  132. $class_name = '\\app\\' . $path[0] . '\\model';
  133. $len = count($path);
  134. for ($i = 1; $i < $len - 1; $i ++) {
  135. $class_name .= '\\' . $path[$i];
  136. }
  137. $class_name .= '\\' . ucfirst($path[$i]) . 'Model';
  138. } else {
  139. $class_name = '\\app\\' . M . '\\model\\' . ucfirst($name) . 'Model';
  140. }
  141. // 根据需要实例化
  142. $key = md5($class_name);
  143. if (! isset(self::$models[$key]) || $new) {
  144. self::$models[$key] = new $class_name();
  145. }
  146. return self::$models[$key];
  147. }
  148. // 创建数据接口
  149. public static function createApi($args = null)
  150. {
  151. // 直接调用方式
  152. if (! is_array($args)) {
  153. $args = func_get_args();
  154. }
  155. // 分离参数
  156. $name = $args[0];
  157. unset($args[0]);
  158. $param = $args;
  159. // 如果只是传递了方法,则自动完善模块及模型控制器
  160. if (strpos($name, '.') === false) {
  161. $name = M . '.' . C . '.' . $name;
  162. }
  163. $path = explode('.', $name); // 第一个为模块 $path[0],倒数第二个为模型$path[$i],倒数第一个为方法$path[$i+1]
  164. $class_name = '\\app\\' . $path[0] . '\\model';
  165. $len = count($path);
  166. for ($i = 1; $i < $len - 2; $i ++) {
  167. $class_name .= '\\' . $path[$i];
  168. }
  169. $class_name .= '\\' . ucfirst($path[$i]) . 'Model';
  170. $key = md5($class_name);
  171. if (isset(self::$models[$key])) {
  172. $model = self::$models[$key];
  173. } else {
  174. $model = new $class_name();
  175. self::$models[$key] = $model;
  176. }
  177. // 调取接口方法
  178. if (is_array($param)) {
  179. $rs = call_user_func_array(array(
  180. $model,
  181. $path[$i + 1]
  182. ), $param);
  183. }
  184. // 返回结果,如果不是json数据,则转换
  185. if (! ! $return = json_decode($rs)) {
  186. return $rs;
  187. } else {
  188. return json_encode($rs);
  189. }
  190. }
  191. }