Init.php 2.3 KB

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