Exemple #1
0
 /**
  * Returns a response with the given message and status code
  *
  * @param string|array $data Data to send
  * @param int $status Status code of the response
  * @param bool $forceError If TRUE the response will be treated as an error, otherwise any status below 400 will be a normal response
  * @return Response
  * @internal
  */
 public function createResponse($data, $status, $forceError = FALSE)
 {
     $body = NULL;
     $response = new Response(NULL, $status);
     $format = $this->requestFactory->getRequest()->format();
     if (!$format) {
         $format = 'json';
     }
     $messageKey = 'message';
     if ($forceError || $status >= 400) {
         $messageKey = 'error';
     }
     switch ($format) {
         case 'json':
             switch (gettype($data)) {
                 case 'string':
                     $body = array($messageKey => $data);
                     break;
                 case 'array':
                     $body = $data;
                     break;
                 case 'NULL':
                     $body = array($messageKey => $response->statusText($status));
                     break;
             }
             $response->contentType('application/json');
             $response->content(json_encode($body));
             break;
         case 'xml':
             // TODO: support more response formats
         // TODO: support more response formats
         default:
             $body = sprintf('Unsupported format: %s. Please set the Accept header to application/json', $format);
             $response->content($body);
     }
     return $response;
 }
Exemple #2
0
 public function __construct($items, $status = 200)
 {
     parent::__construct('', $status);
     $this->_items = $items;
 }
Exemple #3
0
 /**
  * Sets the cache value for the given request
  *
  * @param \Cundd\Rest\Request $request
  * @param \Bullet\Response $response
  */
 public function setCachedValueForRequest(\Cundd\Rest\Request $request, Response $response)
 {
     $this->currentRequest = $request;
     /** @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $cacheInstance */
     $cacheInstance = NULL;
     // Don't cache exceptions
     if ($response->content() instanceof \Exception) {
         return;
     }
     // Don't cache write requests
     if ($request->isWrite()) {
         return;
     }
     $cacheLifeTime = $this->getCacheLifeTime();
     /*
      * Use caching if the cache life time configuration is not -1, an API
      * path is given and the request is a read request
      */
     $useCaching = $cacheLifeTime !== -1 && $request->path();
     if (!$useCaching) {
         return;
     }
     $cacheInstance = $this->_getCacheInstance();
     $cacheInstance->set($this->_getCacheKey(), array('content' => (string) $response, 'status' => $response->status(), 'encoding' => $response->encoding(), 'content-type' => $response->contentType(), 'last-modified' => $this->getHttpDate(time())), $this->_getTags(), $cacheLifeTime);
 }