| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace Heanup\Frame;
- class Request
- {
- const MODE_ONLINE = 0; // 正式环境
- const MODE_BETA = 1; // Beta 环境
- const MODE_PRE = 2; // Pre 预览环境
- const MODE_DEVELOP = 3; // 开发环境
-
- const MODE_ONLINE_PRE=4;
- const MODE_BETA_PRE=5;
- const MODE_DEVLOP_PRE=6;
- protected static $privateDataPath = ''; // 私有文件路径
- protected static $mode = null;
- protected static $exception;
- protected static $currentUserId = null; // 当前登录UserID
- protected static $sessionId = null;
- protected static $isCacheEnabled = true; // 缓存是否有效
- protected static $data = array(); // 运行期缓存
- /**
- * 获取一个随机的唯一的ID
- * @return null|string
- */
- public static function getRequestId()
- {
- static $id = null;
- if (is_null($id)) {
- $id = md5(uniqid('', true) . microtime() . rand(0, 999999999));
- }
- return $id;
- }
- /**
- * 设置运行期缓存,尽量不要设置大数据
- * @param $key
- * @param $value
- */
- public static function set($key, $value)
- {
- self::$data[$key] = $value;
- }
- /**
- * 读取运行期缓存
- * @param $key
- * @return mixed
- */
- public static function get($key)
- {
- return self::$data[$key];
- }
- /**
- * 抛出异常,并记录
- * @param string $message
- * @param int $code
- * @throws \Exception
- */
- public static function throwException($message = '', $code = 0)
- {
- self::$exception = new \Exception($message, $code);
- Logger::error(self::$exception, 'request.exception');
- throw self::$exception;
- }
- /**
- * 设置异常
- * @param $exception
- * @return bool
- */
- public static function setException($exception)
- {
- if ($exception instanceof \Exception) {
- self::$exception = $exception;
- Logger::error($exception, 'request.exception');
- }
- return true;
- }
- /**
- * 获取异常
- * @return mixed
- */
- public static function getException()
- {
- return self::$exception;
- }
- /**
- * 设置环境变量
- * @param int $value
- */
- public static function setMode($value = 0)
- {
- self::$mode = $value;
- }
- /**
- * 获取环境变量,优先使用程序中设置的,其次使用环境变量
- * @return int
- */
- public static function getMode()
- {
- return intval(self::$mode);
- }
- /**
- * 检查环境变量
- * @param $value
- * @return bool
- */
- public static function checkMode($value)
- {
- if (self::getMode() == $value) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 设置当前登录用户ID
- * @param $userId
- */
- public static function setCurrentUserId($userId)
- {
- if ($userId) {
- self::$currentUserId = $userId;
- }
- }
- /**
- * 返回当前登录用户ID
- * @return int|null
- */
- public static function getCurrentUserId()
- {
- return intval(self::$currentUserId);
- }
- /**
- * 设置会话ID
- * @param $sessionId
- */
- public static function setSessionId($sessionId)
- {
- if ($sessionId) {
- self::$sessionId = $sessionId;
- }
- }
- /**
- * 获取会话ID
- * @return int|null
- */
- public static function getSessionId()
- {
- return self::$sessionId;
- }
- /**
- * 设置私有文件路径
- * @param $path
- */
- public static function setPrivateDataPath($path)
- {
- self::$privateDataPath = rtrim($path, '/');
- }
- /**
- * 获取私有文件路径
- * @param string $filename
- * @return string
- */
- public static function getPrivateDataPath($filename = '')
- {
- return self::$privateDataPath . '/' . rtrim($filename) . '/';
- }
- /**
- * 判断是否开启缓存
- * @return bool
- */
- public static function isCacheEnabled()
- {
- return self::$isCacheEnabled;
- }
- /**
- * 是否启用缓存
- * @param bool $value
- */
- public static function setCacheEnabled($value = true)
- {
- self::$isCacheEnabled = intval($value);
- }
- }
|