Init.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Heanup\Frame;
  3. // 引入全局常量
  4. require_once dirname(__DIR__) . '/constants.php';
  5. // 引入全局函数
  6. require_once dirname(__DIR__) . '/functions.php';
  7. // 引入自动加载
  8. require_once dirname(__DIR__) . '/Autoload.php';
  9. require_once dirname(__DIR__) . '/vendor/autoload.php';
  10. class Init
  11. {
  12. /**
  13. * 环境变量设置
  14. * @param $configFile
  15. * @return bool
  16. */
  17. public static function setEnvironment($configFile = '')
  18. {
  19. // 设置错误显示
  20. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  21. ini_set('display_errors', false);
  22. // 无需浏览器缓存
  23. header("Cache-Control: no-cache, must-revalidate");
  24. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  25. // 设置时区和编码
  26. date_default_timezone_set('Asia/Shanghai');
  27. setlocale(LC_ALL, 'en_US.utf-8');
  28. header('Content-type:text/html;charset=utf-8');
  29. // 设置错误捕获机制
  30. // set_error_handler('\Heanup\Frame\Init::errorHandler');
  31. // set_exception_handler('\Heanup\Frame\Init::exceptionHandler');
  32. // Sentry::instance()->setException();
  33. // 如果没有附加配置文件的话则可以结束配置了,反之,则要重新设置一些变量
  34. if (!$configFile || !file_exists($configFile)) {
  35. return true;
  36. }
  37. // 解析配置文件
  38. $config = parse_ini_file($configFile);
  39. if (!$config) {
  40. return true;
  41. }
  42. // 调整错误显示
  43. if (isset($config['display_errors'])) {
  44. ini_set('display_errors', intval($config['display_errors']));
  45. }
  46. // 设置日志路径
  47. if (isset($config['log_path'])) {
  48. Logger::setLogPath($config['log_path']);
  49. }
  50. // 设置日志等级
  51. if (isset($config['log_level'])) {
  52. Logger::setLogLevel(intval($config['log_level']));
  53. }
  54. // 设置环境变量
  55. if (isset($config['mode'])) {
  56. Request::setMode($config['mode']);
  57. }
  58. return true;
  59. }
  60. /**
  61. * 异常捕获
  62. * @param $ex
  63. */
  64. public static function exceptionHandler($ex)
  65. {
  66. // Sentry::instance()->exceptionHandler($ex);
  67. // Logger::error($ex, 'uncaught_exception');
  68. }
  69. }