error.init.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 - 工具库-错误处理
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class errorInit {
  12. private $error_data = array(); //error容器
  13. private $error_type = array('html', 'text', 'json', 'xml', 'array'); //error类型数组
  14. /**
  15. * Error机制 添加一个error
  16. * 添加错误信息,不会直接输出,直到调用send_error的时候才会输出所有的错误信息
  17. * 使用方法:$this->getUtil('error')->add_error('aaaa');
  18. * @param string $error_message 错误信息
  19. * @return object
  20. */
  21. public function add_error($error_message) {
  22. $this->error_data[] = $error_message;
  23. }
  24. /**
  25. * Error机制 输出一个error
  26. * 输出错误信息,可以选择各种输出方式,json,xml,json,html
  27. * add_error的错误信息也会被一起输出
  28. * 使用方法:$this->getUtil('error')->send_error();
  29. * @param string $error_message 错误信息
  30. * @param string $error_type 错误类型
  31. * @return object
  32. */
  33. public function send_error($error_message, $error_type = 'json') {
  34. $this->error_data[] = $error_message;
  35. $error_type = strtolower($error_type);
  36. if (!in_array($error_type, $this->error_type)) $error_type = 'json';
  37. $this->display($error_type);
  38. }
  39. /**
  40. * Error机制 私有函数,error输出
  41. * @param string $error_type 错误类型
  42. * @return object
  43. */
  44. private function display($error_type) {
  45. $InitPHP_conf = InitPHP::getConfig();
  46. if ($error_type == 'text') {
  47. $error = implode("\r\t", $this->error_data);
  48. exit($error);
  49. } elseif ($error_type == 'json') {
  50. exit(json_encode($this->error_data));
  51. } elseif ($error_type == 'xml') {
  52. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  53. $xml .= '<return>';
  54. foreach ($this->error_data as $v) {
  55. $xml .= '<error>' .$v. '</error>';
  56. }
  57. $xml .= '</return>';
  58. exit($xml);
  59. } elseif ($error_type == 'array') {
  60. exit(var_export($this->error_data));
  61. } elseif ($error_type == 'html') {
  62. $error = $this->error_data;
  63. $template = InitPHP::getAppPath($InitPHP_conf['error']['template']);
  64. if ($template) {
  65. if (!file_exists($template)) InitPHP::initError('error template is not exist');
  66. @include $template;
  67. } else {
  68. InitPHP::initError('please set error template in initphp.conf.php');
  69. }
  70. //扩展HTML错误输出
  71. exit;
  72. }
  73. }
  74. }