You are here

class SubRequestBuffer in GraphQL 8.3

Hierarchy

Expanded class hierarchy of SubRequestBuffer

6 files declare their use of SubRequestBuffer
BlocksByRegion.php in modules/graphql_core/src/Plugin/GraphQL/Fields/Blocks/BlocksByRegion.php
Breadcrumbs.php in modules/graphql_core/src/Plugin/GraphQL/Fields/Breadcrumbs/Breadcrumbs.php
Context.php in modules/graphql_core/src/Plugin/GraphQL/Fields/Context/Context.php
InternalRequest.php in modules/graphql_core/src/Plugin/GraphQL/Fields/Routing/InternalUrl/InternalRequest.php
LanguageSwitchLinks.php in modules/graphql_core/src/Plugin/GraphQL/Fields/LanguageSwitch/LanguageSwitchLinks.php

... See full list

1 string reference to 'SubRequestBuffer'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses SubRequestBuffer
graphql.buffer.subrequest in ./graphql.services.yml
Drupal\graphql\GraphQL\Buffers\SubRequestBuffer

File

src/GraphQL/Buffers/SubRequestBuffer.php, line 14

Namespace

Drupal\graphql\GraphQL\Buffers
View source
class SubRequestBuffer extends BufferBase {

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

  /**
   * The request stack service.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * SubrequestBuffer constructor.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
   *   The http kernel service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack service.
   */
  public function __construct(HttpKernelInterface $httpKernel, RequestStack $requestStack) {
    $this->httpKernel = $httpKernel;
    $this->requestStack = $requestStack;
  }

  /**
   * Add an item to the buffer.
   *
   * @param \Drupal\Core\Url $url
   *   The url object to run the subrequest on.
   * @param callable $extract
   *   The callback to run within the sub-request.
   *
   * @return \Closure
   *   The callback to invoke to load the result for this buffer item.
   */
  public function add(Url $url, callable $extract) {
    $item = new \ArrayObject([
      'url' => $url,
      'extract' => $extract,
    ]);
    return $this
      ->createBufferResolver($item);
  }

  /**
   * {@inheritdoc}
   */
  protected function getBufferId($item) {

    /** @var \Drupal\Core\GeneratedUrl $url */
    $url = $item['url']
      ->toString(TRUE);
    return hash('sha256', json_encode([
      'url' => $url
        ->getGeneratedUrl(),
      'tags' => $url
        ->getCacheTags(),
      'contexts' => $url
        ->getCacheContexts(),
      'age' => $url
        ->getCacheMaxAge(),
    ]));
  }

  /**
   * Create a sub-request for the given url.
   *
   * @param \Symfony\Component\HttpFoundation\Request $current
   *   The current main request.
   * @param string $url
   *   The url to run the subrequest on.
   * @param array $buffer
   *   The buffer.
   *
   * @return \Symfony\Component\HttpFoundation\Request
   *   The request object.
   */
  protected function createRequest(Request $current, array $buffer, $url) {
    $request = Request::create($url, 'GET', $current->query
      ->all(), $current->cookies
      ->all(), $current->files
      ->all(), $current->server
      ->all());
    $request->attributes
      ->set('_graphql_subrequest', function () use ($buffer) {
      return array_map(function ($item) {
        return $item['extract']($item['url']);
      }, $buffer);
    });
    if ($session = $current
      ->getSession()) {
      $request
        ->setSession($session);
    }
    return $request;
  }

  /**
   * {@inheritdoc}
   */
  public function resolveBufferArray(array $buffer) {

    /** @var \Drupal\Core\GeneratedUrl $url */
    $url = reset($buffer)['url']
      ->toString(TRUE);
    $current = $this->requestStack
      ->getCurrentRequest();
    $target = $url
      ->getGeneratedUrl();
    $request = $this
      ->createRequest($current, $buffer, $target);

    /** @var \Drupal\graphql\GraphQL\Buffers\SubRequestResponse $response */
    $response = $this->httpKernel
      ->handle($request, HttpKernelInterface::SUB_REQUEST);
    while ($response instanceof LocalRedirectResponse) {
      $target = $response
        ->getTargetUrl();
      $request = $this
        ->createRequest($current, $buffer, $target);
      $response = $this->httpKernel
        ->handle($request, HttpKernelInterface::SUB_REQUEST);
    }
    if (!$response instanceof SubRequestResponse) {
      return array_fill_keys(array_keys($buffer), NULL);
    }

    // TODO:
    // Remove the request stack manipulation once the core issue described at
    // https://www.drupal.org/node/2613044 is resolved.
    while ($this->requestStack
      ->getCurrentRequest() !== $current) {
      $this->requestStack
        ->pop();
    }
    if ($url instanceof CacheableDependencyInterface) {
      $response
        ->addCacheableDependency($url);
    }
    return array_map(function ($value) use ($response) {
      return new CacheableValue($value, [
        $response,
      ]);
    }, $response
      ->getResult());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BufferBase::$buffers protected property The the array of buffers.
BufferBase::$results protected property The array of result sets.
BufferBase::createBufferResolver public function Helper function to create a resolver for a singular buffer.
BufferBase::createResolver protected function Creates a callback to invoke to load the result for this buffer item.
BufferBase::resolveBuffer protected function Resolves the given buffer wholly.
BufferBase::resolveItem protected function Returns the result of the given item after processing the buffer if needed.
SubRequestBuffer::$httpKernel protected property The http kernel service.
SubRequestBuffer::$requestStack protected property The request stack service.
SubRequestBuffer::add public function Add an item to the buffer.
SubRequestBuffer::createRequest protected function Create a sub-request for the given url.
SubRequestBuffer::getBufferId protected function Overrides BufferBase::getBufferId
SubRequestBuffer::resolveBufferArray public function Resolve the buffer as an array. Overrides BufferBase::resolveBufferArray
SubRequestBuffer::__construct public function SubrequestBuffer constructor.