Esempio n. 1
0
 /**
  * Make an API response from an existing response object.
  *
  * @author Morten Rugaard <*****@*****.**>
  *
  * @param  \Illuminate\Http\Response $old
  * @return \Nodes\Api\Http\Response
  */
 public static function makeFromExisting(IlluminateResponse $old)
 {
     // Support for custom status code and message
     $statusCode = $old instanceof self ? $old->getStatusCodeAndMessage() : $old->getStatusCode();
     // Generate API response from response object
     $new = static::create($old->getOriginalContent(), $statusCode);
     $new->headers = $old->headers;
     return $new;
 }
Esempio n. 2
0
 /**
  * Set cache headers and 304 not modify if needed.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Illuminate\Http\Response $response
  */
 protected function setCacheHeaders($request, $response)
 {
     if (starts_with($request->getPathInfo(), ['/images'])) {
         $stat = stat(session()->pull('requestImagePath'));
     } else {
         if (($view = $response->getOriginalContent()) instanceof View) {
             $stat = stat($view->getPath());
         }
     }
     if (isset($stat)) {
         $response->setCache(['etag' => md5("{$stat['ino']}|{$stat['mtime']}|{$stat['size']}"), 'public' => true]);
         $response->setExpires(Carbon::now()->addDays(30));
         if (null !== ($etag = $request->headers->get('If-None-Match')) || null !== $request->headers->get('If-Modified-Since')) {
             $etags = explode('-', $etag, -1);
             $request->headers->set('If-None-Match', count($etags) ? $etags[0] . '"' : $etag);
             $response->isNotModified($request);
         }
     }
 }
 /**
  * @param Response $response
  * @return mixed
  * @throws \Exception
  */
 protected function getResultFromResponse(Response $response)
 {
     $result = $response->getOriginalContent();
     switch (true) {
         case is_string($result):
             /** @var string $result */
             return ['type' => 'string', 'data' => $result];
         case $result instanceof View:
             /** @var View $result */
             return ['type' => 'view', 'data' => $result->render()];
         case $result instanceof Model:
             /** @var Model $result */
             return ['type' => strtolower((new \ReflectionClass($result))->getShortName()), 'data' => $result->toArray()];
         case $result instanceof Collection:
         case is_array($result):
             return ['type' => 'collection', 'data' => $result];
         case is_null($result):
             return ['type' => 'null', 'data' => null];
         default:
             throw new \Exception('Object of class ' . get_class($response) . ' could not be converted to JSON');
     }
 }
Esempio n. 4
0
 /**
  * Save a cached view if caching is enabled.
  *
  * @param  \Illuminate\Http\Response  $response
  * @return void
  */
 protected function saveCachedView(Response $response)
 {
     if (config('sitemap.cache_enabled')) {
         $key = $this->getCacheKey();
         $content = $response->getOriginalContent()->render();
         if (!$this->cache->get($key)) {
             $this->cache->put($key, $content, config('sitemap.cache_length'));
         }
     }
 }
Esempio n. 5
0
 /**
  * Make an API response from an existing Illuminate response.
  *
  * @param \Illuminate\Http\Response $old
  *
  * @return \Dingo\Api\Http\Response
  */
 public static function makeFromExisting(IlluminateResponse $old)
 {
     $new = static::create($old->getOriginalContent(), $old->getStatusCode());
     $new->headers = $old->headers;
     return $new;
 }
 /**
  * Replace the content of the response, with the transformed content.
  *
  * @param  \Illuminate\Http\Response $response
  * @return void
  */
 protected function transformContent(Response $response)
 {
     $response->setContent($this->transformer->transform($response->getOriginalContent(), $response->getStatusCode()));
 }
Esempio n. 7
0
 /**
  * Make an API response from an existing Illuminate response.
  *
  * @param  \Illuminate\Http\Response  $response
  * @return \Dingo\Api\Http\Response
  */
 public static function makeFromExisting(IlluminateResponse $response)
 {
     return new static($response->getOriginalContent(), $response->getStatusCode(), $response->headers->all());
 }
Esempio n. 8
0
 /**
  * @return $this
  */
 public function printResponseOriginContent()
 {
     print_r((string) $this->response->getOriginalContent());
     return $this;
 }
 protected function makeMessageFromResponse(Response $response)
 {
     $message['original_content'] = $response->getOriginalContent();
     $message['content'] = $response->getContent();
     $message['status_code'] = $response->getStatusCode();
     return json_encode($message, JSON_PRETTY_PRINT);
 }