Ejemplo n.º 1
0
 /**
  * 响应请求,依次执行添加的中间件逻辑
  *
  * @param \Owl\Http\Request $request
  * @param \Owl\Http\Response $response
  * @return void
  */
 public function execute(\Owl\Http\Request $request, \Owl\Http\Response $response)
 {
     try {
         $this->middleware->execute([$request, $response]);
     } catch (\Exception $exception) {
         $handler = $this->exception_handler ?: function ($exception, $request, $response) {
             $response->setStatus(500);
             $response->setBody('');
         };
         call_user_func($handler, $exception, $request, $response);
     }
     if (!TEST) {
         $response->end();
     }
     if (function_exists('fastcgi_finish_request')) {
         fastcgi_finish_request();
     }
 }
Ejemplo n.º 2
0
Archivo: Router.php Proyecto: stcer/owl
 /**
  * @param \Owl\Http\Request $request
  * @param \Owl\Http\Response $response
  * @return \Owl\Http\Response $response
  *
  * @throws \Owl\Http\Exception 404
  * @throws \Owl\Http\Exception 501
  */
 protected function respond(\Owl\Http\Request $request, \Owl\Http\Response $response)
 {
     $path = $this->getRequestPath($request);
     list($class, $parameters) = $this->byRewrite($path) ?: $this->byPath($path);
     if (!class_exists($class)) {
         throw \Owl\Http\Exception::factory(404);
     }
     $controller = new $class($request, $response);
     $controller->request = $request;
     $controller->response = $response;
     // 如果__beforeExecute()返回了内容就直接返回内容
     if (method_exists($controller, '__beforeExecute') && ($data = call_user_func_array([$controller, '__beforeExecute'], $parameters))) {
         if (!$data instanceof \Owl\Http\Response) {
             $response->setBody($data);
         }
         return $response;
     }
     $method = $request->getMethod();
     if ($method === 'HEAD') {
         $method = 'GET';
     }
     if (!in_array($method, ['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'])) {
         throw \Owl\Http\Exception::factory(501);
     }
     if (!is_callable([$controller, $method])) {
         throw \Owl\Http\Exception::factory(405);
     }
     $data = call_user_func_array([$controller, $method], $parameters);
     if (!$data instanceof \Owl\Http\Response) {
         $response->setBody($data);
     }
     if (method_exists($controller, '__afterExecute')) {
         $controller->__afterExecute($request, $response);
     }
     return $response;
 }