Esempio n. 1
0
 /**
  * @param \watoki\collections\Liste $formats
  * @throws HttpError If no renderer for accepted format and no default renderer is set
  * @return WebResponse
  */
 private function respondWithDefault(Liste $formats)
 {
     if (!isset($this->renderers[''])) {
         throw new HttpError(WebResponse::STATUS_NOT_ACCEPTABLE, "Could not render the resource in an accepted format.", "Invalid accepted types: " . "[" . $formats->join(', ') . "] not supported by " . "[" . implode(', ', array_keys($this->renderers)) . "]");
     }
     $response = new WebResponse(call_user_func($this->renderers['']));
     if (!$formats->isEmpty()) {
         $response->getHeaders()->set(WebResponse::HEADER_CONTENT_TYPE, MimeTypes::getType($formats->first()));
     }
     return $response;
 }
Esempio n. 2
0
 /**
  * @return mixed
  */
 function respond()
 {
     $response = new WebResponse($this->content);
     if (strpos($this->key, '.') !== false) {
         $parts = explode('.', $this->key);
         $response->getHeaders()->set(WebResponse::HEADER_CONTENT_TYPE, MimeTypes::getType(end($parts)));
     } else {
         if (!$this->webRequest->getFormats()->isEmpty()) {
             $response->getHeaders()->set(WebResponse::HEADER_CONTENT_TYPE, MimeTypes::getType($this->webRequest->getFormats()->first()));
         }
     }
     return $response;
 }
Esempio n. 3
0
 /**
  * @param WebRequest $request
  * @throws \watoki\curir\error\HttpError
  * @return WebResponse
  */
 public function createResponse(WebRequest $request)
 {
     $formats = $request->getFormats();
     foreach ($formats as $format) {
         try {
             $response = new WebResponse($this->render($format));
             $response->getHeaders()->set(WebResponse::HEADER_CONTENT_TYPE, MimeTypes::getType($format));
             return $response;
         } catch (\ReflectionException $e) {
         }
     }
     throw new HttpError(WebResponse::STATUS_NOT_ACCEPTABLE, "Could not render the resource in an accepted format.", "Invalid accepted types: [" . $formats->join(', ') . "]");
 }
Esempio n. 4
0
 private function getFormatsFromHeaders()
 {
     $formats = array();
     $headers = $this->environment->getHeaders();
     if ($headers->has(WebRequest::HEADER_ACCEPT)) {
         foreach (explode(',', $headers->get(WebRequest::HEADER_ACCEPT)) as $accepted) {
             $accepted = trim($accepted);
             if (strpos($accepted, ';') !== false) {
                 list($accepted, ) = explode(';', $accepted);
             }
             $formats = array_merge($formats, MimeTypes::getExtensions($accepted));
         }
     }
     return $formats;
 }