CacheableResourceResponse example


  protected function buildWrappedResponse(TopLevelDataInterface $data, Request $request, IncludedData $includes$response_code = 200, array $headers = [], LinkCollection $links = NULL, array $meta = []) {
    $links = ($links ?: new LinkCollection([]));
    if (!$links->hasLinkWithKey('self')) {
      $self_link = new Link(new CacheableMetadata(), self::getRequestLink($request), 'self');
      $links = $links->withLink('self', $self_link);
    }
    $document = new JsonApiDocumentTopLevel($data$includes$links$meta);
    if (!$request->isMethodCacheable()) {
      return new ResourceResponse($document$response_code$headers);
    }
    $response = new CacheableResourceResponse($document$response_code$headers);
    $cacheability = (new CacheableMetadata())->addCacheContexts([
      // Make sure that different sparse fieldsets are cached differently.       'url.query_args:fields',
      // Make sure that different sets of includes are cached differently.       'url.query_args:include',
    ]);
    $response->addCacheableDependency($cacheability);
    return $response;
  }

  /** * Respond with an entity collection. * * @param \Drupal\jsonapi\JsonApiResource\ResourceObjectData $primary_data * The collection of entities. * @param \Drupal\jsonapi\JsonApiResource\IncludedData|\Drupal\jsonapi\JsonApiResource\NullIncludedData $includes * The resources to be included in the document. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * The base JSON:API resource type for the request to be served. * @param \Drupal\jsonapi\Query\OffsetPage $page_param * The pagination parameter for the requested collection. * * @return \Drupal\jsonapi\ResourceResponse * The response. */
->toString(TRUE);
        $meta['links']['me']['href'] = $me_url->getGeneratedUrl();
        // The cacheability of the `me` URL is the cacheability of that URL         // itself and the currently authenticated user.         $cacheability = $cacheability->merge($me_url);
      }
      catch (RouteNotFoundException $e) {
        // Do not add the link if the route is disabled or marked as internal.       }
    }

    $response = new CacheableResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([])new NullIncludedData()$urls$meta));
    return $response->addCacheableDependency($cacheability);
  }

}
$individual_responses = static::toResourceResponses($this->getResponses(static::getResourceLinks($resource_identifiers)$request_options));
    $merged_response = static::toCollectionResourceResponse($individual_responses$self_link, TRUE);

    $merged_document = $merged_response->getResponseData();
    if (!isset($merged_document['data'])) {
      $merged_document['data'] = [];
    }

    $cacheability = static::getExpectedCollectionCacheability($this->account, $collection, NULL, $filtered);
    $cacheability->setCacheMaxAge($merged_response->getCacheableMetadata()->getCacheMaxAge());

    $collection_response = new CacheableResourceResponse($merged_document);
    $collection_response->addCacheableDependency($cacheability);

    if (is_null($included_paths)) {
      return $collection_response;
    }

    $related_responses = array_reduce($collectionfunction D$related_responses, EntityInterface $entity) use ($included_paths$request_options$self_link) {
      if (!$entity->access('view', $this->account) && !$entity->access('view label', $this->account)) {
        return $related_responses;
      }
      $expected_related_responses = $this->getExpectedRelatedResponses($included_paths$request_options$entity);
      
$this->setEventResponse($event$exception->getStatusCode());
  }

  /** * {@inheritdoc} */
  protected function setEventResponse(ExceptionEvent $event$status) {
    /** @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */
    $exception = $event->getThrowable();
    $document = new JsonApiDocumentTopLevel(new ErrorCollection([$exception])new NullIncludedData()new LinkCollection([]));
    if ($event->getRequest()->isMethodCacheable()) {
      $response = new CacheableResourceResponse($document$exception->getStatusCode()$exception->getHeaders());
      $response->addCacheableDependency($exception);
    }
    else {
      $response = new ResourceResponse($document$exception->getStatusCode()$exception->getHeaders());
    }
    $event->setResponse($response);
  }

  /** * Check if the error should be formatted using JSON:API. * * The JSON:API format is supported if the format is explicitly set or the * request is for a known JSON:API route. * * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $exception_event * The exception event. * * @return bool * TRUE if it needs to be formatted using JSON:API. FALSE otherwise. */

      $merged_document['links'] = [
        'self' => [
          'href' => $self_link,
        ],
      ];
    }
    // All collections should be 200, without regard for the status of the     // individual resources in those collections, which means any '4xx-response'     // cache tags on the individual responses should also be omitted.     $merged_cacheability->setCacheTags(array_diff($merged_cacheability->getCacheTags()['4xx-response']));
    return (new CacheableResourceResponse($merged_document, 200))->addCacheableDependency($merged_cacheability);
  }

  /** * Gets an array of expected ResourceResponses for the given include paths. * * @param array $include_paths * The list of relationship include paths for which to get expected data. * @param array $request_options * Request options to apply. * * @return \Drupal\jsonapi\ResourceResponse * The expected ResourceResponse. * * @see \GuzzleHttp\ClientInterface::request() */
Home | Imprint | This part of the site doesn't use cookies.