Exemplo n.º 1
0
 /**
  * session初始化
  *
  * @param array $config
  *
  * @throws \Think\Exception\Exception
  */
 public static function init(array $config = array())
 {
     if (empty($config)) {
         $config = Config::get('session');
     }
     // 记录初始化信息
     APP_DEBUG && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info');
     $isDoStart = false;
     if (isset($config['use_trans_sid'])) {
         ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
     }
     // 启动session
     if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
         ini_set('session.auto_start', 0);
         $isDoStart = true;
     }
     if (isset($config['prefix'])) {
         self::$prefix = $config['prefix'];
     }
     if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) {
         session_id($_REQUEST[$config['var_session_id']]);
     } elseif (isset($config['id']) && !empty($config['id'])) {
         session_id($config['id']);
     }
     if (isset($config['name'])) {
         session_name($config['name']);
     }
     if (isset($config['path'])) {
         session_save_path($config['path']);
     }
     if (isset($config['domain'])) {
         ini_set('session.cookie_domain', $config['domain']);
     }
     if (isset($config['expire'])) {
         ini_set('session.gc_maxlifetime', $config['expire']);
         ini_set('session.cookie_lifetime', $config['expire']);
     }
     if (isset($config['use_cookies'])) {
         ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
     }
     if (isset($config['cache_limiter'])) {
         session_cache_limiter($config['cache_limiter']);
     }
     if (isset($config['cache_expire'])) {
         session_cache_expire($config['cache_expire']);
     }
     if (!empty($config['type'])) {
         // 读取session驱动
         $class = (!empty($config['namespace']) ? $config['namespace'] : '\\Supe\\Core\\Session\\Driver\\') . ucwords($config['type']);
         // 检查驱动类
         if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
             throw new \Supe\Exception\Exception('error session handler', 11700);
         }
     }
     if ($isDoStart) {
         session_start();
         self::$active = true;
     }
 }
Exemplo n.º 2
0
 public static function alarm($config = array())
 {
     $type = isset($config['type']) ? $config['type'] : 'Email';
     $class = (!empty($config['namespace']) ? $config['namespace'] : '\\Supe\\Core\\Log\\Alarm\\') . ucwords($type);
     unset($config['type']);
     self::$alarm = new $class($config['alarm']);
     // 记录初始化信息
     APP_DEBUG && Log::record('[ CACHE ] ALARM ' . $type . ': ' . var_export($config, true), 'info');
 }
Exemplo n.º 3
0
 /**
  * 数据库初始化 并取得数据库类实例
  *
  * @static
  * @access public
  *
  * @param mixed $config 连接配置
  *
  * @return Object 返回数据库驱动类
  * @throws \Exception
  */
 public static function connect($config = '')
 {
     $md5 = md5(serialize($config));
     if (!isset(self::$instances[$md5])) {
         // 解析连接参数 支持数组和字符串
         $options = self::parseConfig($config);
         if (empty($options['type'])) {
             throw new \Exception('Db type error');
         }
         $class = (!empty($options['namespace']) ? $options['namespace'] : '\\Supe\\Database\\Driver\\') . ucwords($options['type']);
         self::$instances[$md5] = new $class($options);
         // 记录初始化信息
         APP_DEBUG && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info');
     }
     self::$instance = self::$instances[$md5];
     return self::$instance;
 }
Exemplo n.º 4
0
 /**
  * SQL性能分析
  *
  * @access protected
  *
  * @param string $sql
  *
  * @return array
  */
 protected function getExplain($sql)
 {
     $pdo = $this->linkID->query("EXPLAIN " . $sql);
     $result = $pdo->fetch(\PDO::FETCH_ASSOC);
     $result = array_change_key_case($result);
     if (isset($result['extra'])) {
         if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
             Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
         }
     }
     return $result;
 }
Exemplo n.º 5
0
 /**
  * 数据库调试 记录当前SQL
  *
  * @access protected
  *
  * @param boolean $start 调试开始标记 true 开始 false 结束
  */
 protected function debug($start)
 {
     if (!empty($this->config['debug'])) {
         // 开启数据库调试模式
         if ($start) {
             Debug::remark('queryStartTime', 'time');
         } else {
             $this->modelSql[$this->model] = $this->queryStr;
             // 记录操作结束时间
             Debug::remark('queryEndTime', 'time');
             $log = $this->queryStr . ' [ RunTime:' . Debug::getRangeTime('queryStartTime', 'queryEndTime') . 's ]';
             // SQL性能分析
             if (0 === stripos(trim($this->queryStr), 'select')) {
                 $result = $this->getExplain($this->queryStr);
                 Log::record('[ EXPLAIN : ' . var_export($result, true) . ' ]', 'sql');
             }
             Log::record('[ SQL ] ' . $log, 'sql');
         }
     }
 }
Exemplo n.º 6
0
 /**
  * Shutdown Handler
  *
  * @return bool true-禁止往下传播已处理过的异常; false-未处理的异常继续传播
  */
 public static function appShutdown()
 {
     // 写入日志
     Log::save();
     if ($error = error_get_last()) {
         // 将错误信息托管至 Supe\Exception\ErrorException
         $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);
         /**
          * Shutdown handler 中的异常将不被往下传播
          * 所以,这里我们必须手动传播而不能像 Error handler 中那样 throw
          */
         self::appException($exception);
         // 禁止往下传播已处理过的异常
         return true;
     }
     return false;
 }