Exemple #1
0
 /**
  * 处理请求
  *
  *     $request->execute();
  *
  * @param   Request  $request
  * @param   Response $response
  * @return \tourze\Http\Response
  * @throws \tourze\Base\Exception\BaseException
  */
 public function executeRequest(Request $request, Response $response)
 {
     $className = 'Controller';
     // 控制器
     $controller = $request->controller;
     $className = Inflector::classify($controller) . $className;
     // 目录
     $directory = $request->directory;
     if ($directory) {
         $directory = str_replace('/', '\\', $directory);
         $className = $directory . $className;
     }
     // 保存请求状态
     $previous = Request::$current;
     Request::$current = $request;
     Base::getLog()->info(__METHOD__ . ' controller class', ['class' => $className]);
     try {
         if (!class_exists($className)) {
             Base::getLog()->debug(__METHOD__ . ' class not found', ['class' => $className]);
             throw HttpException::factory(Http::NOT_FOUND, 'The requested URL :uri was not found on this server.', [':uri' => $request->uri])->request($request);
         }
         $class = new ReflectionClass($className);
         if ($class->isAbstract()) {
             Base::getLog()->error(__METHOD__ . ' calling abstract controller class', ['class' => $className]);
             throw new BaseException('Cannot create instances of abstract :controller', [':controller' => $className]);
         }
         $controller = $class->newInstance(['request' => $request, 'response' => $response]);
         $response = $class->getMethod('execute')->invoke($controller);
         if (!$response instanceof Response) {
             Base::getLog()->error(__METHOD__ . ' unknown response type');
             throw new BaseException('Controller failed to return a Response');
         }
     } catch (HttpException $e) {
         if (null === $e->request()) {
             $e->request($request);
         }
         $response = $e->getResponse();
     }
     Request::$current = $previous;
     return $response;
 }
Exemple #2
0
 /**
  * 执行请求
  *
  * @return Response
  * @throws HttpException
  * @throws ClientRecursionException
  * @throws RequestException
  */
 public function execute()
 {
     if (!$this->external) {
         Base::getLog()->debug(__METHOD__ . ' execute internal request');
         $processed = Request::process($this, $this->routes);
         if ($processed) {
             // 保存匹配到的路由
             $this->route = Arr::get($processed, 'route');
             $params = Arr::get($processed, 'params');
             // 是否为外部链接
             $this->external = $this->route->isExternal();
             // 控制器放在子目录中的情况
             if (isset($params['directory'])) {
                 $this->directory = $params['directory'];
             }
             // 附加上命名空间
             if (isset($params['namespace'])) {
                 $this->directory = $params['namespace'];
             }
             // 命名空间处理
             if (!$this->directory) {
                 $this->directory = Route::$defaultNamespace;
             }
             // 修正命名空间
             if (false === strpos($this->directory, '\\')) {
                 $this->directory = Route::$defaultNamespace . $this->directory . '\\';
             }
             // 保存控制器
             $this->controller = Arr::get($params, 'controller');
             // 保存动作
             $this->action = Arr::get($params, 'action', Route::$defaultAction);
             // 清理保留字段
             foreach ($this->persistRouteParams as $name) {
                 unset($params[$name]);
             }
             $this->_params = $params;
             Base::getLog()->debug(__METHOD__ . ' execute info', ['directory' => $this->directory, 'controller' => $this->controller, 'action' => $this->action, 'params' => $this->_params]);
         } else {
             Base::getLog()->debug(__METHOD__ . ' not route matched');
         }
     }
     if (!$this->route instanceof Entry) {
         $e = HttpException::factory(Http::NOT_FOUND, 'Unable to find a route to match the URI: :uri', [':uri' => $this->uri]);
         $e->request($this);
         throw $e;
     }
     if (!$this->client instanceof RequestClient) {
         throw new RequestException('Unable to execute :uri without a RequestClient', [':uri' => $this->uri]);
     }
     return $this->client->execute($this);
 }