Request.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace PhpLife\Frame;
  3. class Request
  4. {
  5. const MODE_ONLINE = 0; // 正式环境
  6. const MODE_BETA = 1; // Beta 环境
  7. const MODE_PRE = 2; // Pre 预览环境
  8. const MODE_DEVELOP = 3; // 开发环境
  9. const MODE_ONLINE_PRE=4;
  10. const MODE_BETA_PRE=5;
  11. const MODE_DEVLOP_PRE=6;
  12. protected static $privateDataPath = ''; // 私有文件路径
  13. protected static $mode = null;
  14. protected static $exception;
  15. protected static $currentUserId = null; // 当前登录UserID
  16. protected static $sessionId = null;
  17. protected static $isCacheEnabled = true; // 缓存是否有效
  18. protected static $data = array(); // 运行期缓存
  19. /**
  20. * 获取一个随机的唯一的ID
  21. * @return null|string
  22. */
  23. public static function getRequestId()
  24. {
  25. static $id = null;
  26. if (is_null($id)) {
  27. $id = md5(uniqid('', true) . microtime() . rand(0, 999999999));
  28. }
  29. return $id;
  30. }
  31. /**
  32. * 设置运行期缓存,尽量不要设置大数据
  33. * @param $key
  34. * @param $value
  35. */
  36. public static function set($key, $value)
  37. {
  38. self::$data[$key] = $value;
  39. }
  40. /**
  41. * 读取运行期缓存
  42. * @param $key
  43. * @return mixed
  44. */
  45. public static function get($key)
  46. {
  47. return self::$data[$key];
  48. }
  49. /**
  50. * 抛出异常,并记录
  51. * @param string $message
  52. * @param int $code
  53. * @throws \Exception
  54. */
  55. public static function throwException($message = '', $code = 0)
  56. {
  57. self::$exception = new \Exception($message, $code);
  58. Logger::error(self::$exception, 'request.exception');
  59. throw self::$exception;
  60. }
  61. /**
  62. * 设置异常
  63. * @param $exception
  64. * @return bool
  65. */
  66. public static function setException($exception)
  67. {
  68. if ($exception instanceof \Exception) {
  69. self::$exception = $exception;
  70. Logger::error($exception, 'request.exception');
  71. }
  72. return true;
  73. }
  74. /**
  75. * 获取异常
  76. * @return mixed
  77. */
  78. public static function getException()
  79. {
  80. return self::$exception;
  81. }
  82. /**
  83. * 设置环境变量
  84. * @param int $value
  85. */
  86. public static function setMode($value = 0)
  87. {
  88. self::$mode = $value;
  89. }
  90. /**
  91. * 获取环境变量,优先使用程序中设置的,其次使用环境变量
  92. * @return int
  93. */
  94. public static function getMode()
  95. {
  96. return intval(self::$mode);
  97. }
  98. /**
  99. * 检查环境变量
  100. * @param $value
  101. * @return bool
  102. */
  103. public static function checkMode($value)
  104. {
  105. if (self::getMode() == $value) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * 设置当前登录用户ID
  113. * @param $userId
  114. */
  115. public static function setCurrentUserId($userId)
  116. {
  117. if ($userId) {
  118. self::$currentUserId = $userId;
  119. }
  120. }
  121. /**
  122. * 返回当前登录用户ID
  123. * @return int|null
  124. */
  125. public static function getCurrentUserId()
  126. {
  127. return intval(self::$currentUserId);
  128. }
  129. /**
  130. * 设置会话ID
  131. * @param $sessionId
  132. */
  133. public static function setSessionId($sessionId)
  134. {
  135. if ($sessionId) {
  136. self::$sessionId = $sessionId;
  137. }
  138. }
  139. /**
  140. * 获取会话ID
  141. * @return int|null
  142. */
  143. public static function getSessionId()
  144. {
  145. return self::$sessionId;
  146. }
  147. /**
  148. * 设置私有文件路径
  149. * @param $path
  150. */
  151. public static function setPrivateDataPath($path)
  152. {
  153. self::$privateDataPath = rtrim($path, '/');
  154. }
  155. /**
  156. * 获取私有文件路径
  157. * @param string $filename
  158. * @return string
  159. */
  160. public static function getPrivateDataPath($filename = '')
  161. {
  162. return self::$privateDataPath . '/' . rtrim($filename) . '/';
  163. }
  164. /**
  165. * 判断是否开启缓存
  166. * @return bool
  167. */
  168. public static function isCacheEnabled()
  169. {
  170. return self::$isCacheEnabled;
  171. }
  172. /**
  173. * 是否启用缓存
  174. * @param bool $value
  175. */
  176. public static function setCacheEnabled($value = true)
  177. {
  178. self::$isCacheEnabled = intval($value);
  179. }
  180. }