ErrorHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /**
  20. * $storagePrex , $storage , $storagePath 为找到当前的storage而设置的配置参数
  21. * 可以在配置中更改,更改后,就会通过容器注入的方式修改相应的配置值
  22. */
  23. public $storage; // = 'ErrorHandlerMysqldb'; // ErrorHandlerMysqldb | ErrorHandlerMongodb 当前的storage,如果在config中配置,那么在初始化的时候会被注入修改
  24. /**
  25. * 设置storage的path路径,
  26. * 如果不设置,则系统使用默认路径
  27. * 如果设置了路径,则使用自定义的路径
  28. */
  29. public $storagePath = '';
  30. protected $_errorHandler;
  31. public function init()
  32. {
  33. parent::init();
  34. // 从数据库配置中得到值, 设置成当前service存储,是Mysqldb 还是 Mongodb
  35. $config = Yii::$app->store->get('service_db', 'error_handle_log');
  36. $this->storage = 'ErrorHandlerMysqldb';
  37. if ($config == Yii::$app->store->serviceMongodbName) {
  38. $this->storage = 'ErrorHandlerMongodb';
  39. }
  40. $currentService = $this->getStorageService($this);
  41. $this->_errorHandler = new $currentService();
  42. }
  43. // 动态更改为mongodb model
  44. public function changeToMongoStorage()
  45. {
  46. $this->storage = 'ErrorHandlerMongodb';
  47. $currentService = $this->getStorageService($this);
  48. $this->_errorHandler = new $currentService();
  49. }
  50. // 动态更改为mongodb model
  51. public function changeToMysqlStorage()
  52. {
  53. $this->storage = 'ErrorHandlerMysqldb';
  54. $currentService = $this->getStorageService($this);
  55. $this->_errorHandler = new $currentService();
  56. }
  57. public function getPrimaryKey()
  58. {
  59. return $this->_errorHandler->getPrimaryKey();
  60. }
  61. /**
  62. * @param $code | Int, http 错误码
  63. * @param $message | String, 错误的具体信息
  64. * @param $file | string, 发生错误的文件
  65. * @param $line | Int, 发生错误所在文件的代码行
  66. * @param $created_at | Int, 发生错误的执行时间戳
  67. * @param $ip | string, 访问人的ip
  68. * @param $name | string, 错误的名字
  69. * @param $trace_string | string, 错误的追踪信息
  70. * @return 返回错误存储到mongodb的id,作为前端显示的错误编码
  71. * 该函数从errorHandler得到错误信息,然后保存到mongodb中。
  72. */
  73. public function saveByErrorHandler(
  74. $code,
  75. $message,
  76. $file,
  77. $line,
  78. $created_at,
  79. $ip,
  80. $name,
  81. $trace_string,
  82. $url,
  83. $req_info=[]
  84. )
  85. {
  86. return $this->_errorHandler->saveByErrorHandler($code,$message,$file,$line,$created_at,$ip,$name,$trace_string,$url,$req_info);
  87. }
  88. /**
  89. * 通过主键,得到errorHandler对象。
  90. */
  91. public function getByPrimaryKey($primaryKey)
  92. {
  93. return $this->_errorHandler->getByPrimaryKey($primaryKey);
  94. }
  95. /*
  96. * example filter:
  97. * [
  98. * 'numPerPage' => 20,
  99. * 'pageNum' => 1,
  100. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  101. 'where' => [
  102. ['>','price',1],
  103. ['<=','price',10]
  104. * ['sku' => 'uk10001'],
  105. * ],
  106. * 'asArray' => true,
  107. * ]
  108. */
  109. public function coll($filter)
  110. {
  111. return $this->_errorHandler->coll($filter);
  112. }
  113. }