Esempio n. 1
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. 2
0
 /**
  * @param \watoki\curir\delivery\WebRequest $request
  * @return \watoki\curir\delivery\WebResponse
  */
 public function createResponse(WebRequest $request)
 {
     $formats = $request->getFormats();
     foreach ($formats as $accepted) {
         if (array_key_exists($accepted, $this->renderers)) {
             return $this->respondWith($accepted);
         }
     }
     return $this->respondWithDefault($formats);
 }
Esempio n. 3
0
 private function findFile(WebRequest $request)
 {
     foreach ($request->getFormats() as $format) {
         $found = $this->findExistingFile($request->getTarget()->toString(), $format);
         if ($found) {
             return $found;
         }
     }
     return $this->findExistingFile($request->getTarget()->toString(), null);
 }
Esempio n. 4
0
 public function getBody()
 {
     $userMessage = '';
     if ($this->exception instanceof HttpError) {
         $userMessage = $this->exception->getUserMessage();
     }
     if ($this->request->getFormats()->contains('html')) {
         $model = array('status' => $this->getStatus(), 'rootUrl' => $this->request->getContext()->toString(), 'userMessage' => $userMessage);
         $details = date('Y-m-d H:i:s');
         $exception = $this->exception;
         while ($exception) {
             $details .= "\n" . get_class($exception) . ": " . $exception->getMessage() . "\n" . $exception->getTraceAsString() . "\n";
             $exception = $exception->getPrevious();
         }
         $model['details'] = htmlentities($details);
         return $this->renderTemplate($model);
     } else {
         return $userMessage ?: get_class($this->exception) . ': ' . $this->exception->getMessage();
     }
 }
Esempio n. 5
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. 6
0
 private function isMapping(WebRequest $request)
 {
     return $this->boxes->isMapping() && !$request->getHeaders()->has(self::HEADER_NO_BOXING) && $request->getFormats()->contains('html');
 }
Esempio n. 7
0
 public function givenTheRequestFormatIs($string)
 {
     $this->request->getFormats()->clear();
     $this->request->getFormats()->append($string);
 }
Esempio n. 8
0
 public function thenTheFormatsShouldBe($formats)
 {
     $this->spec->assertEquals((array) $formats, $this->request->getFormats()->toArray());
 }