ErrorHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\helper;
  10. use fecshop\services\Service;
  11. use Yii;
  12. /**
  13. * Helper Errors services.
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class ErrorHandler extends Service
  18. {
  19. protected $_errorHandlerModelName = '\fecshop\models\mongodb\ErrorHandlerLog';
  20. protected $_errorHandlerModel;
  21. public function init()
  22. {
  23. parent::init();
  24. list($this->_errorHandlerModelName, $this->_errorHandlerModel) = \Yii::mapGet($this->_errorHandlerModelName);
  25. }
  26. public function getPrimaryKey()
  27. {
  28. return '_id';
  29. }
  30. /**
  31. * @param $code | Int, http 错误码
  32. * @param $message | String, 错误的具体信息
  33. * @param $file | string, 发生错误的文件
  34. * @param $line | Int, 发生错误所在文件的代码行
  35. * @param $created_at | Int, 发生错误的执行时间戳
  36. * @param $ip | string, 访问人的ip
  37. * @param $name | string, 错误的名字
  38. * @param $trace_string | string, 错误的追踪信息
  39. * @return 返回错误存储到mongodb的id,作为前端显示的错误编码
  40. * 该函数从errorHandler得到错误信息,然后保存到mongodb中。
  41. */
  42. public function saveByErrorHandler(
  43. $code,
  44. $message,
  45. $file,
  46. $line,
  47. $created_at,
  48. $ip,
  49. $name,
  50. $trace_string,
  51. $url,
  52. $req_info=[]
  53. ) {
  54. $category = Yii::$service->helper->getAppName();
  55. $model = new $this->_errorHandlerModelName();
  56. $model->category = $category;
  57. $model->code = $code;
  58. $model->message = $message;
  59. $model->file = $file;
  60. $model->line = $line;
  61. $model->created_at = $created_at;
  62. $model->ip = $ip;
  63. $model->name = $name;
  64. $model->url = $url;
  65. $model->request_info = $req_info;
  66. $model->trace_string = $trace_string;
  67. $model->save();
  68. return (string)$model[$this->getPrimaryKey()];
  69. }
  70. /**
  71. * 通过主键,得到errorHandler对象。
  72. */
  73. public function getByPrimaryKey($primaryKey)
  74. {
  75. if ($primaryKey) {
  76. return $this->_errorHandlerModel->findOne($primaryKey);
  77. }
  78. }
  79. /*
  80. * example filter:
  81. * [
  82. * 'numPerPage' => 20,
  83. * 'pageNum' => 1,
  84. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  85. 'where' => [
  86. ['>','price',1],
  87. ['<=','price',10]
  88. * ['sku' => 'uk10001'],
  89. * ],
  90. * 'asArray' => true,
  91. * ]
  92. */
  93. public function coll($filter = '')
  94. {
  95. $query = $this->_errorHandlerModel->find();
  96. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  97. $coll = $query->all();
  98. if (!empty($coll)) {
  99. foreach ($coll as $k => $one) {
  100. $coll[$k] = $one;
  101. }
  102. }
  103. return [
  104. 'coll' => $coll,
  105. 'count'=> $query->limit(null)->offset(null)->count(),
  106. ];
  107. }
  108. }