You are here

public function Parser::combineResponses in Subrequests 8

Parameters

\Symfony\Component\HttpFoundation\Response[] $responses: The responses to combine.

Return value

\Symfony\Component\HttpFoundation\Response The combined response with a 207.

File

src/Blueprint/Parser.php, line 72

Class

Parser
TODO: Change this comment. We'll use the serializer instead. Base class for blueprint parsers. There may be slightly different blueprint formats depending on the encoding. For instance, JSON encoded blueprints will reference other properties in…

Namespace

Drupal\subrequests\Blueprint

Code

public function combineResponses(array $responses) {
  $delimiter = md5(microtime());

  // Prepare the root content type header.
  $content_type = sprintf('multipart/related; boundary="%s", type=%s', $delimiter, $this->type);
  $headers = [
    'Content-Type' => $content_type,
  ];
  $context = [
    'delimiter' => $delimiter,
  ];

  // Set the content.
  $content = $this->serializer
    ->normalize($responses, 'multipart-related', $context);
  $response = CacheableResponse::create($content, 207, $headers);

  // Set the cacheability metadata.
  $cacheable_responses = array_filter($responses, function ($response) {
    return $response instanceof CacheableResponseInterface;
  });
  array_walk($cacheable_responses, function (CacheableResponseInterface $partial_response) use ($response) {
    $response
      ->addCacheableDependency($partial_response
      ->getCacheableMetadata());
  });
  return $response;
}