/**
  * @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();
 }
Example #2
0
 /**
  * @param Request $req
  * @param Response $res
  */
 public function init(Request $req, Response $res)
 {
     $controller = $req->getController();
     $input = $req->getInput();
     // Work with request and controller, populate Response
     $res->send();
 }
Example #3
0
 /**
  * @throws RequestException
  */
 public function send()
 {
     if ($this->format === "json") {
         // Json
         $this->sendHeader("Content-type", "application/json; charset=utf-8");
         print json_encode($this->data);
         return;
         // Return
     } elseif ($this->format === "jsonp") {
         // Jsonp
         $this->sendHeader("Content-type", "application/json; charset=utf-8");
         $params = $this->request->getInput()->getData();
         $jsonpFunction = $params["_callback"] ?? "callback";
         printf("%s(%s);", $jsonpFunction, json_encode($this->data));
         return;
         // Return
     } elseif ($this->format === "javascript") {
         $this->sendHeader("Content-type", "application/javascript; charset=utf-8");
     }
     // If data has only 1 key, get that for body, otherwise get print_r of entire data array
     reset($this->data);
     $body = count($this->data) === 1 ? $this->data[key($this->data)] : print_r($this->data, true);
     // Send body
     print $body;
     return;
     // Return
 }
Example #4
0
 /**
  * @param callable|null $callback
  * @return Request
  */
 public static function parseRequest(callable $callback = null) : Request
 {
     // HTTP Request Information
     $httpMethod = $_SERVER["REQUEST_METHOD"] ?? "";
     $httpUri = $_SERVER["REQUEST_URI"] ?? "";
     $httpUri = explode("?", $httpUri)[0];
     // Create request
     $request = new Request($httpMethod, $httpUri, new Input(self::getInputData($httpMethod), self::getHttpHeaders()));
     // Set response format based on HTTP_ACCEPT cookie
     if (isset($_SERVER["HTTP_ACCEPT"])) {
         $httpAccept = str_getcsv($_SERVER["HTTP_ACCEPT"])[0];
         $httpAccept = explode("/", $httpAccept)[1];
         $request->getResponse()->setFormat(trim($httpAccept));
     }
     if (isset($callback)) {
         call_user_func_array($callback, [$request, $request->getResponse()]);
     }
     return $request;
 }
 /**
  * @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()));
     }
 }