You are here

protected function SubrequestsManager::processBatchesSequence in Subrequests 3.x

Same name and namespace in other branches
  1. 8.2 src/SubrequestsManager.php \Drupal\subrequests\SubrequestsManager::processBatchesSequence()

Processes all the Subrequests until produce a collection of responses.

Parameters

\Drupal\subrequests\SubrequestsTree $tree: The request tree that contains the requesting structure.

int $_sequence: (internal) The current index in the sequential chain.

\Symfony\Component\HttpFoundation\Response[] $_responses: (internal) The list of responses accumulated so far.

Return value

\Symfony\Component\HttpFoundation\Response[] An array of responses when everything has been resolved.

1 call to SubrequestsManager::processBatchesSequence()
SubrequestsManager::request in src/SubrequestsManager.php

File

src/SubrequestsManager.php, line 57

Class

SubrequestsManager

Namespace

Drupal\subrequests

Code

protected function processBatchesSequence($tree, $_sequence = 0, array $_responses = []) {
  $batch = $tree[$_sequence];

  // Perform all the necessary replacements for the elements in the batch.
  $batch = $this->replacer
    ->replaceBatch($batch, $_responses);
  $results = array_map(function (Subrequest $subrequest) use ($tree) {
    $master_request = $tree
      ->getMasterRequest();

    // Create a Symfony Request object based on the Subrequest.

    /** @var \Symfony\Component\HttpFoundation\Request $request */
    $request = $this->serializer
      ->denormalize($subrequest, Request::class, NULL, [
      'master_request' => $master_request,
    ]);
    $response = $this->httpKernel
      ->handle($request, HttpKernelInterface::MASTER_REQUEST);

    // Set the Content-ID header in the response.
    $content_id = sprintf('<%s>', $subrequest->requestId);
    $response->headers
      ->set('Content-ID', $content_id);
    return $response;
  }, $batch);

  // Accumulate the responses for the current batch.
  $_responses = array_merge($_responses, $results);

  // If we're not done, then call recursively with the updated arguments.
  $_sequence++;
  return $_sequence === $tree
    ->count() ? $_responses : $this
    ->processBatchesSequence($tree, $_sequence, $_responses);
}