示例#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;
 }
示例#2
0
 /**
  * @param WebRequest $request
  * @return \watoki\curir\delivery\WebResponse
  */
 public function createResponse(WebRequest $request)
 {
     $response = new WebResponse();
     $response->setStatus($this->status);
     $response->getHeaders()->set(WebResponse::HEADER_LOCATION, $this->getAbsoluteTarget($request)->toString());
     return $response;
 }
示例#3
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;
 }
示例#4
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(', ') . "]");
 }
示例#5
0
文件: Box.php 项目: watoki/boxes
 public function dispatch(WebRequest $request, Router $router)
 {
     $arguments = $request->getArguments();
     $request->setTarget($this->target);
     if ($arguments->isEmpty()) {
         $arguments->merge($this->arguments);
         $request->setMethod(WebRequest::METHOD_GET);
     } else {
         if ($arguments->has(self::$TARGET_KEY)) {
             $request->setTarget(Path::fromString($arguments->get(self::$TARGET_KEY)));
         }
         if ($arguments->has(WebRequest::$METHOD_KEY)) {
             $request->setMethod($arguments->get(WebRequest::$METHOD_KEY));
         } else {
             $request->setMethod(WebRequest::METHOD_GET);
         }
     }
     $this->response = $router->route($request)->respond();
     if ($this->response->getHeaders()->has(WebResponse::HEADER_LOCATION)) {
         throw new WrappedRedirection($this->response->getHeaders()->get(WebResponse::HEADER_LOCATION));
     }
     return $request;
 }
示例#6
0
 /**
  * @param WebResponse|mixed $response
  * @throws \Exception if $response is not a WebResponse
  * @return null
  */
 public function deliver($response)
 {
     if ($response instanceof WebResponse) {
         if ($response->getStatus()) {
             header('HTTP/1.1 ' . $response->getStatus());
         }
         foreach ($response->getHeaders() as $header => $value) {
             if (!is_null($value)) {
                 header($header . ': ' . $value);
             }
         }
     }
     $this->cookies->applyCookies('setcookie');
     echo $response;
 }
示例#7
0
 public function thenTheResponseShouldBeARedirectionTo($url)
 {
     $this->spec->assertArrayHasKey(WebResponse::HEADER_LOCATION, $this->response->getHeaders()->toArray());
     $this->spec->assertEquals($url, $this->response->getHeaders()->get(WebResponse::HEADER_LOCATION));
 }
示例#8
0
 private function thenTheContentTypeShouldBe($string)
 {
     $this->assertEquals($string, $this->response->getHeaders()->get(WebResponse::HEADER_CONTENT_TYPE));
 }