/** * 框架autoloader * @param string $classname 要加载的类名 */ public static function loader($classname) { if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $classname)) { exit; } if (isset(self::$coreClass[$classname])) { $path = ''; $filename = $classname; //默认文件名称和类名称相同 if (!is_array(self::$coreClass[$classname])) { $path = self::$coreClass[$classname]; } else { if (isset(self::$coreClass[$classname]['path'])) { $path = implode('', self::$coreClass[$classname]['path']); } if (isset(self::$coreClass[$classname]['classname'])) { $filename = self::$coreClass[$classname]['filename']; //用手工配置的文件名称覆盖默认的 } if (isset(self::$coreClass[$classname]['postfix'])) { $filename .= self::$coreClass[$classname]['postfix']; } } self::includeFile($path, $filename); } else { /** * 对于外部使用Dagger,不要去找文件结尾为Controller、 * DB或者其它任何没有被加载的文件 */ if (preg_grep('/.*?Controller$/', array($classname))) { self::includeFile(DAGGER_PATH_APP_CTL, $classname); } else { if (preg_grep('/.*?ModelDB$/', array($classname))) { self::includeFile(DAGGER_PATH_MODEL . 'db/', $classname); } else { if (preg_grep('/.*?Config$/', array($classname))) { self::includeFile(DAGGER_PATH_CONFIG, $classname); } else { if (preg_grep('/.*?Pagelet$/', array($classname))) { self::includeFile(DAGGER_PATH_APP_PLT, $classname); } else { if (defined('DAGGER_PATH_APP_MODEL') && file_exists(DAGGER_PATH_APP_MODEL . "{$classname}.php")) { if (file_exists(DAGGER_PATH_MODEL . "{$classname}.php")) { BaseModelLog::sendLog(90107, "{$classname}.php文件在/app/" . htmlspecialchars(Configure::$app, ENT_QUOTES, 'UTF-8') . "/model目录和/model目录重名", '', BaseModelLog::ERROR_MODEL_ID_PHP); BaseModelMessage::showError("{$classname}.php文件在/app/" . htmlspecialchars(Configure::$app, ENT_QUOTES, 'UTF-8') . "/model目录和/model目录重名"); } self::includeFile(DAGGER_PATH_APP_MODEL, $classname); } else { self::includeFile(DAGGER_PATH_MODEL, $classname); } } } } } } }
function daggerExceptionHandler($exception) { $errormsg = $exception->getMessage(); $errno = $exception->getCode(); $sendmsg = (empty($_SERVER['REQUEST_URI']) ? empty($_SERVER['SCRIPT_FILENAME']) ? '' : '(' . $_SERVER['SCRIPT_FILENAME'] . ')' : '(' . $_SERVER['REQUEST_URI'] . ')') . $errormsg; //记录到监控中心 switch (get_class($exception)) { case 'BaseModelDBException': BaseModelLog::sendLog($errno, $sendmsg, '', BaseModelLog::ERROR_MODEL_ID_DB); break; case 'BaseModelMCException': BaseModelLog::sendLog($errno, $sendmsg, '', BaseModelLog::ERROR_MODEL_ID_MC); break; case 'BaseModelHTTPException': BaseModelLog::sendLog($errno, $sendmsg, '', BaseModelLog::ERROR_MODEL_ID_HTTP); break; case 'BaseModelS3Exception': BaseModelLog::sendLog($errno, $sendmsg, '', BaseModelLog::ERROR_MODEL_ID_S3); break; default: BaseModelLog::sendLog($errno, $sendmsg, '', BaseModelLog::ERROR_MODEL_ID_DEFAULT); break; } defined('DAGGER_DEBUG') && BaseModelCommon::debug('[errro code] ' . $errno . ' [errro msg] ' . $errormsg . ' [详细说明]:https://github.com/wxkingstar/dagger/wiki/' . $errno, 'dagger_error'); if (!defined('DAGGER_DEBUG') || defined('DAGGER_ENV') && DAGGER_ENV === 'product') { BaseModelMessage::showError('抱歉让您看到这个页面'); } else { if (defined('QUEUE') || isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { BaseModelMessage::showError($errormsg, BaseModelException::$data, $errno); } else { $trace = $exception->getTrace(); $tracemsg = array(); $pos = 0; foreach ($trace as $k => $error) { if (strpos($error['function'], "smarty_function_") === 0) { $errormsg = 'smarty插件:' . substr($error['function'], 16) . $errormsg; $pos = false; defined('DAGGER_DEBUG') && BaseModelCommon::debug($errormsg, 'dagger_error'); } if (!isset($error['line']) && $pos === 0) { $pos = $k; } if (!empty($error['function'])) { $fun = ''; if (!empty($error['class'])) { $fun .= $error['class'] . $error['type']; } $fun .= $error['function'] . '('; if (!empty($error['args'])) { $mark = ''; foreach ($error['args'] as $arg) { //由于smarty和call_user方法会出现超大对象或数组传入,导致报错页面崩溃,所以直接跳过参数解析输出 if (stripos($error['function'], 'smarty') === 0 || stripos($error['function'], 'call_user') === 0) { continue; } $fun .= $mark; if (is_array($arg)) { $fun .= var_export($arg, true); } else { if (is_bool($arg)) { $fun .= $arg ? 'true' : 'false'; } else { if (is_int($arg) || is_float($arg)) { $fun .= $arg; } else { if (is_null($arg)) { $fun .= 'NULL'; } else { $fun .= '\'' . BaseModelException::daggerHtmlspecialchars(BaseModelException::daggerClear($arg)) . '\''; } } } } $mark = ', '; } } $fun .= ')'; $error['function'] = $fun; } $tracemsg[] = array('file' => isset($error['file']) ? str_replace(array(DAGGER_PATH_ROOT, '\\'), array('', '/'), $error['file']) : '', 'line' => isset($error['line']) ? $error['line'] : '', 'function' => $error['function']); } $throwPhpCode = file($exception->getFile()); $tracemsg = array_merge(array(array('file' => str_replace(array(DAGGER_PATH_ROOT, '\\'), array('', '/'), $exception->getFile()), 'line' => $exception->getLine(), 'function' => $throwPhpCode[$exception->getLine() - 1])), $tracemsg); defined('DAGGER_DEBUG') && BaseModelCommon::debug(array_merge(array(array('File', 'Line', 'Function')), $tracemsg), 'dagger_error_trace'); if ($pos > 0) { $errorFileCode = array_slice(file($tracemsg[$pos]['file']), $tracemsg[$pos]['line'] > 6 ? $tracemsg[$pos]['line'] - 7 : 0, 13); $tracemsg[$pos]['function'] = '<pre class="brush: php; toolbar : false; highlight: ' . $tracemsg[$pos]['line'] . '; first-line: ' . ($tracemsg[$pos]['line'] > 6 ? $tracemsg[$pos]['line'] - 6 : 1) . '">' . implode('', $errorFileCode) . '</pre>'; } $traceTable = ''; if (is_array($tracemsg) && !empty($tracemsg)) { $traceTable .= '<table cellpadding="5" cellspacing="1" width="100%" class="table">'; $traceTable .= '<tr class="bg2"><th width="249px">File</th><th width="44px">Line</th><th width="629px">Function</th></tr>'; foreach ($tracemsg as $k => $msg) { $traceTable .= '<tr class="' . ($pos > 0 && $k == $pos ? 'bg3' : 'bg1') . '">'; $traceTable .= '<td>' . $msg['file'] . '</td>'; $traceTable .= '<td>' . $msg['line'] . '</td>'; $traceTable .= '<td>' . $msg['function'] . '</td>'; $traceTable .= '</tr>'; } $traceTable .= '</table>'; } BaseModelDebug::showTrace($errno, $errormsg, $traceTable); } } }
/** * 检测memcache是否正常运行 */ private function checkConnection() { $timeout = defined('DAGGER_MCCONNECT_TIMEOUT') ? DAGGER_MCCONNECT_TIMEOUT : 0.5; $startTime = microtime(true); $rs = $this->mc->getVersion(); $checkTime = microtime(true) - $startTime; if ($checkTime > $timeout) { $errno = 90501; $error = "MC连接超{$timeout}秒" . "request_uri[{$_SERVER['REQUEST_URI']}]," . "c/s[{$_SERVER['REMOTE_ADDR']}/{$_SERVER['SERVER_ADDR']}]," . "mc[" . $this->servers . "]" . "runtime[{$runTime}s/{$timeout}s]"; defined('DAGGER_DEBUG') && BaseModelCommon::debug('[errro code] ' . $errno . ' [errro msg] ' . $errormsg . ' [详细说明]:https://github.com/wxkingstar/dagger/wiki/' . $errno, 'mc_error'); BaseModelLog::sendLog($errno, $error, '', BaseModelLog::ERROR_MODEL_ID_MC); } if ($rs !== false) { return true; } BaseModelLog::sendLog(90500, "memcache服务器: {$this->servers} 无法响应", '', BaseModelLog::ERROR_MODEL_ID_MC); return false; }
/** * 日志写入文件路径 * @param string $path 文件路径 * @return void */ public static function setLogFilePath($path) { self::$logFilePath = $path; }
/** * 错误处理 * @param int $errno 错误号 * @param string $error 错误信息 * @param data $data 相关提示数据 * @return void * @author wangxin3 * error code * 90301 数据库返回非资源 * 90302 insert或replace入库数据错误 * 90303 insertOnDuplicate中update_keys参数在insert_value中不存在 * 90304 insertOnDuplicate中update_keys参数无有效字段 * 90305 update中update_value传参错误 * 90306 update中where条件错误 * 90307 delete中where条件错误 * 90308 字段在配置文件中未定义 * 90309 字段在配置文件中禁止修改 * 90310 字段值不符合在配置文件定义的类型 * 90311 据库连接失败 * 90312 sql不能为空 * 90313 传参不符合拼接规范,无法正确翻译sql语句 * 90314 数据库连接超过指定时间 * 90320 数据库基类方法不存在 **/ protected function _error($errno = 0, $error = '', $data = array()) { // mysql错误忽略 if (!$this->link && in_array($this->link->errno, $this->ignoreErrorArr, true)) { defined('DAGGER_DEBUG') && BaseModelCommon::debug($this->link->errno, 'db_ignoreErrno_info'); return false; } $errno = empty($errno) ? $this->link->errno : $errno; $error = empty($error) ? $this->link->error : $error; if (in_array($errno, array(90314), true) || defined('QUEUE') || defined('EXTERN')) { defined('DAGGER_DEBUG') && BaseModelCommon::debug('[errro code] ' . $errno . ' [errro msg] ' . $error . ' [详细说明]:https://github.com/wxkingstar/dagger/wiki/' . $errno, 'db_error'); BaseModelLog::sendLog($errno, $error, '', BaseModelLog::ERROR_MODEL_ID_DB); return false; } throw new BaseModelDBException($error, $errno, $data); }
private static function _error($errno, $error) { if (!in_array($errno, array(90401, 90402), true) || defined('QUEUE')) { defined('DAGGER_DEBUG') && BaseModelCommon::debug("[errro code] {$errno} [errro msg] {$error} [详细说明]:https://github.com/wxkingstar/dagger/wiki/{$errno}", 'request_error'); BaseModelLog::sendLog($errno, $error, '', BaseModelLog::ERROR_MODEL_ID_HTTP); } else { throw new BaseModelHTTPException($error, $errno); } return false; }
private function _checkStats($function, $times = 0, $native = false) { $runTime = 0; if (!empty($times)) { $runTime = BaseModelCommon::addStatInfo('mc', $this->startRunTime, $times); } $native = $this->native || $native; $code = $native ? $this->mcd->getResultCode() : $this->lastResultCode; if (in_array($code, array(Memcached::RES_SUCCESS, Memcached::RES_NOTFOUND), true)) { return $runTime; } else { if (in_array($function, array('add', 'addByKey', '_getLock'), true) && in_array($code, array(Memcached::RES_DATA_EXISTS, Memcached::RES_NOTSTORED), true)) { return $runTime; } } $errno = 90502; $error = $native ? $this->mcd->getResultMessage() : $this->lastResultMessage; defined('DAGGER_DEBUG') && BaseModelCommon::debug("[errro code] {$errno} [errro msg] {$error} [详细说明]:http://wiki.intra.sina.com.cn/display/dagger/{$errno}", 'request_error'); BaseModelLog::sendLog($errno, "[code]{$code}[msg]{$error}[method]{$function}[server]{$this->servers}", BaseModelException::getCodeName($errno), BaseModelLog::ERROR_MODEL_ID_MC); return $runTime; }