コード例 #1
0
 /**
  * @param Request $request
  * @param Request\Response $response
  */
 public function init(Request $request, Request\Response $response)
 {
     // Save all request related information
     $this->request = $request;
     $this->controller = $request->getController();
     $this->input = $request->getInput();
     $this->method = $request->getMethod();
     $this->response = $response;
     // Get input params
     $params = $this->input->getData();
     try {
         if ($this->method === "GET") {
             // Single method for all get requests
             $callMethod = "getView";
         } else {
             // Check if we have necessary param to build method name
             if (!array_key_exists(self::REST_METHOD_PARAM, $params)) {
                 throw new \Exception(sprintf('Http requests must have required parameter "%s"', self::REST_METHOD_PARAM));
             }
             // Method name
             $callMethod = $this->method . "_" . $params[self::REST_METHOD_PARAM];
             $callMethod = \Comely::camelCase($callMethod);
         }
         // Check if method exists
         if (!method_exists($this, $callMethod)) {
             throw new \Exception('Request method not found');
         }
         // Call method
         call_user_func([$this, $callMethod]);
     } catch (\Throwable $t) {
         $this->response->set("error", $t->getMessage());
     }
     // Send response
     $this->response->send();
 }
コード例 #2
0
 /**
  * @param Request $request
  * @param Response $response
  * @throws \Comely\IO\Http\Exception\RequestException
  */
 public function init(Request $request, Response $response)
 {
     // Save all request related information
     $this->request = $request;
     $this->controller = $request->getController();
     $this->input = $request->getInput();
     $this->method = $request->getMethod();
     $this->response = $response;
     $this->uri = $request->getUri();
     $this->page->setProp("root", $request->getUriRoot());
     // Get input params
     $params = $this->input->getData();
     try {
         // Check if we have necessary param to build method name
         if (!empty($params[self::REST_METHOD_PARAM])) {
             $callMethod = $this->method . "_" . $params[self::REST_METHOD_PARAM];
             $callMethod = \Comely::camelCase($callMethod);
         } else {
             // Necessary param not found
             if ($this->method !== "GET") {
                 // Throw exception if request method is not GET
                 throw new HttpException(get_called_class(), sprintf('Http requests must have required parameter "%s"', self::REST_METHOD_PARAM));
             }
             // If method is GET, default method is getView
             $callMethod = "getView";
         }
         // Check if method exists
         if (!method_exists($this, $callMethod)) {
             throw new HttpException(get_called_class(), sprintf('Request method "%1$s" not found', $callMethod));
         }
         // Call "callBack" method prior to calling request method
         call_user_func([$this, "callBack"]);
         // Call method
         call_user_func([$this, $callMethod]);
     } catch (KernelException $e) {
         $this->response->set("message", $e->getMessage());
     }
     // Check number of props in Response object,
     if ($this->response->count() !== 1) {
         // populate "errors" property if there are multiple properties already
         $this->response->set("errors", array_map(function (array $error) {
             return $error["formatted"];
         }, $this->app->errorHandler()->fetchAll()));
     }
 }