You are here

class SubrequestsManager in Subrequests 8.2

Same name and namespace in other branches
  1. 3.x src/SubrequestsManager.php \Drupal\subrequests\SubrequestsManager

Hierarchy

Expanded class hierarchy of SubrequestsManager

2 files declare their use of SubrequestsManager
FrontController.php in src/Controller/FrontController.php
SubrequestsManagerTest.php in tests/src/Unit/SubrequestsManagerTest.php
1 string reference to 'SubrequestsManager'
subrequests.services.yml in ./subrequests.services.yml
subrequests.services.yml
1 service uses SubrequestsManager
subrequests.subrequests_manager in ./subrequests.services.yml
Drupal\subrequests\SubrequestsManager

File

src/SubrequestsManager.php, line 10

Namespace

Drupal\subrequests
View source
class SubrequestsManager {

  /**
   * The kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * The serializer.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $serializer;

  /**
   * The path replacer.
   *
   * @var \Drupal\subrequests\JsonPathReplacer
   */
  protected $replacer;
  public function __construct(HttpKernelInterface $http_kernel, DenormalizerInterface $serializer, JsonPathReplacer $replacer) {
    $this->httpKernel = $http_kernel;
    $this->serializer = $serializer;
    $this->replacer = $replacer;
  }
  public function request(SubrequestsTree $tree) {

    // Loop through all sequential requests and merge them.
    return $this
      ->processBatchesSequence($tree);
  }

  /**
   * Processes all the Subrequests until produce a collection of responses.
   *
   * @param \Drupal\subrequests\SubrequestsTree $tree
   *   The request tree that contains the requesting structure.
   * @param int $_sequence
   *   (internal) The current index in the sequential chain.
   * @param \Symfony\Component\HttpFoundation\Response[] $_responses
   *   (internal) The list of responses accumulated so far.
   *
   * @return \Symfony\Component\HttpFoundation\Response[]
   *   An array of responses when everything has been resolved.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SubrequestsManager::$httpKernel protected property The kernel.
SubrequestsManager::$replacer protected property The path replacer.
SubrequestsManager::$serializer protected property The serializer.
SubrequestsManager::processBatchesSequence protected function Processes all the Subrequests until produce a collection of responses.
SubrequestsManager::request public function
SubrequestsManager::__construct public function