Ejemplo n.º 1
0
 /**
  * 处理中间件
  *
  * @throws  \ErrorException
  */
 public static function handleMiddleware()
 {
     if (self::$handle_status != 0) {
         throw new \ErrorException("非法的QP框架使用方法 - 仅仅只能处理一次中间件,并且由框架底层自动处理!");
     }
     foreach (\Qp\Kernel\Http\Router\QpRouter::getMatchedData()['middleware'] as $middleware) {
         self::$response_message = "";
         self::$response_status = 200;
         (new $middleware())->handle();
         if (self::$handle_status == 2) {
             if (json_decode(self::$response_message, true) != []) {
                 \Qp\Kernel\Response::response()->setContentType('application/json');
             }
             \Qp\Kernel\Response::send(self::$response_message, self::$response_status);
             exit;
         }
     }
     self::$handle_status = 3;
 }
Ejemplo n.º 2
0
 /**
  * 记录起始请求日志
  * 记录成功返回true,失败或没记录日志返回false
  *
  * @return  bool
  */
 public static function request_start_log()
 {
     if (!BaseLog::isLog('debug')) {
         return false;
     }
     if (!Config::getEnv("app.request_start_log")) {
         return false;
     }
     $data = Request::nonPostParam();
     if (Config::getEnv("app.request_log_post")) {
         $data = Request::param();
     }
     $file_path = BaseLog::handle_log_file('framework', 'debug');
     $log_time = date("Y-m-d H:i:s", QP_RUN_START);
     $ip = Request::getIp();
     $router_url = \Qp\Kernel\Http\Router\QpRouter::getRouterStr();
     $prefix = "[{$log_time}] [{$ip}] [router : {$router_url}] ";
     $msg = "【请求日志】" . json_encode(['data' => $data]);
     $logger = new FileAdapter($file_path);
     $logger->setFormatter(new LineFormatter("%message%"));
     return (bool) $logger->log($prefix . $msg);
 }
Ejemplo n.º 3
0
 /**
  * 设置路由
  *
  * @param array $router
  */
 public static function set(array $router)
 {
     if (self::$modules_status == 0) {
         self::$modules_status = 1;
     }
     if (Base::hasMatched()) {
         return;
     }
     // 匹配前缀
     $prefix = strval(array_get($router, 'prefix', ''));
     if ($prefix === "") {
         $prefix = self::$prefix;
     }
     if ($prefix !== Base::getPrefix()) {
         return;
     }
     // 匹配控制器
     $controllers = array_get($router, 'controllers', []);
     if (!is_array($controllers) || empty($controllers)) {
         throw new \InvalidArgumentException("路由配置有误,'controllers'的值必须是非空数组");
     }
     $controller = strval(array_get($controllers, Base::getController(), ''));
     if (empty($controller)) {
         return;
     }
     // 处理命名空间
     $namespace = strval(array_get($router, 'namespace', ''));
     self::handleConfigNamespace($namespace, $prefix);
     // 获取中间件,延迟处理 - 需要先加载其他模块才能处理中间件
     $middleware = array_get($router, 'middlewares', []);
     Http\Middleware\QpMiddleware::handleConfigMiddleware($middleware, $namespace);
     Base::saveRouterData(['namespace' => $namespace, 'controller' => $controller, 'middleware' => $middleware]);
     Base::match();
 }
Ejemplo n.º 4
0
 /**
  * 处理用户请求的路由
  * 匹配失败直接抛出异常,成功则返回设置过的Phalcon路由对象
  *
  * @return  \Phalcon\Mvc\Router     Phalcon的路由对象
  * @throws  \ErrorException
  */
 private function handleRouter()
 {
     require_once QP_APP_PATH . "routers.php";
     if (QR::hasMatched() == false) {
         echo (new \App\Controllers\IndexController())->notFoundAction();
         exit;
     }
     $matched_router_data = QR::getMatchedData();
     $ns = $matched_router_data['namespace'];
     $ctrl = $matched_router_data['controller'];
     $m = QR::getMethod();
     if (!method_exists($ns . "\\" . $ctrl . "Controller", $m . "Action")) {
         echo (new \App\Controllers\IndexController())->notFoundAction();
         exit;
     }
     $router = new \Phalcon\Mvc\Router();
     $router->setDefaults(["namespace" => $ns, "controller" => $ctrl, "action" => $m]);
     return $router;
 }
Ejemplo n.º 5
0
 /**
  * 底层日志记录方法
  * 记录成功返回true,失败或没记录日志返回false
  *
  * @param   string  $log_level  日志等级
  * @param   string  $msg        日志消息(占位符使用{键名})
  * @param   array   $data       数据(支持key-value形式的数组,用来替换$msg中的占位字符)
  * @param   bool    $is_replace 是否替换占位字符
  * @param   string  $modular    应用模块名
  * @return  bool
  */
 protected static function log($log_level, $msg = '', $data = [], $is_replace = false, $modular = 'unknown_module')
 {
     if (!in_array($log_level, self::$log_level_allow)) {
         throw new \InvalidArgumentException("不支持的日志等级:" . $log_level);
     }
     if (!self::isLog($log_level)) {
         return false;
     }
     $file_path = self::handle_log_file($modular, $log_level);
     $log_time = date('Y-m-d H:i:s');
     $ip = \Qp\Kernel\Request::getIp();
     $router_url = \Qp\Kernel\Http\Router\QpRouter::getRouterStr();
     $prefix = "[{$log_time}] [{$ip}] [router : {$router_url}] ";
     if ($is_replace) {
         $msg = self::interpolate($msg, $data);
     } else {
         $msg = $msg . json_encode(['data' => $data]);
     }
     $logger = new FileAdapter($file_path);
     $logger->setFormatter(new LineFormatter("%message%"));
     return (bool) $logger->log($prefix . $msg);
 }