Issues (645)

src/GraphQL/Buffers/SubRequestBuffer.php (6 issues)

Labels
Severity
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Buffers;
4
5
use Drupal\Core\Cache\CacheableDependencyInterface;
0 ignored issues
show
The type Drupal\Core\Cache\CacheableDependencyInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Url;
0 ignored issues
show
The type Drupal\Core\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\Core\Routing\LocalRedirectResponse;
0 ignored issues
show
The type Drupal\Core\Routing\LocalRedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\graphql\GraphQL\Buffers\SubRequestResponse;
9
use Drupal\graphql\GraphQL\Cache\CacheableValue;
10
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
The type Symfony\Component\HttpFoundation\RequestStack was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\HttpKernel\HttpKernelInterface;
0 ignored issues
show
The type Symfony\Component\HttpKernel\HttpKernelInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class SubRequestBuffer extends BufferBase {
15
16
  /**
17
   * The http kernel service.
18
   *
19
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
20
   */
21
  protected $httpKernel;
22
23
  /**
24
   * The request stack service.
25
   *
26
   * @var \Symfony\Component\HttpFoundation\RequestStack
27
   */
28
  protected $requestStack;
29
30
  /**
31
   * SubrequestBuffer constructor.
32
   *
33
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
34
   *   The http kernel service.
35
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
36
   *   The request stack service.
37
   */
38
  public function __construct(HttpKernelInterface $httpKernel, RequestStack $requestStack) {
39
    $this->httpKernel = $httpKernel;
40
    $this->requestStack = $requestStack;
41
  }
42
43
  /**
44
   * Add an item to the buffer.
45
   *
46
   * @param \Drupal\Core\Url $url
47
   *   The url object to run the subrequest on.
48
   * @param callable $extract
49
   *   The callback to run within the sub-request.
50
   *
51
   * @return \Closure
52
   *   The callback to invoke to load the result for this buffer item.
53
   */
54
  public function add(Url $url, callable $extract) {
55
    $item = new \ArrayObject([
56
      'url' => $url,
57
      'extract' => $extract,
58
    ]);
59
60
    return $this->createBufferResolver($item);
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  protected function getBufferId($item) {
67
    /** @var \Drupal\Core\GeneratedUrl $url */
68
    $url = $item['url']->toString(TRUE);
69
70
    return hash('sha256', json_encode([
71
      'url' => $url->getGeneratedUrl(),
72
      'tags' => $url->getCacheTags(),
73
      'contexts' => $url->getCacheContexts(),
74
      'age' => $url->getCacheMaxAge(),
75
    ]));
76
  }
77
78
  /**
79
   * Create a sub-request for the given url.
80
   *
81
   * @param \Symfony\Component\HttpFoundation\Request $current
82
   *   The current main request.
83
   * @param string $url
84
   *   The url to run the subrequest on.
85
   * @param array $buffer
86
   *   The buffer.
87
   *
88
   * @return \Symfony\Component\HttpFoundation\Request
89
   *   The request object.
90
   */
91
  protected function createRequest(Request $current, array $buffer, $url) {
92
    $request = Request::create(
93
      $url,
94
      'GET',
95
      $current->query->all(),
96
      $current->cookies->all(),
97
      $current->files->all(),
98
      $current->server->all()
99
    );
100
101
    $request->attributes->set('_graphql_subrequest', function () use ($buffer) {
102
      return array_map(function ($item) {
103
        return $item['extract']($item['url']);
104
      }, $buffer);
105
    });
106
107
    if ($session = $current->getSession()) {
108
      $request->setSession($session);
109
    }
110
    
111
    return $request;
112
  }
113
114
  /**
115
   * {@inheritdoc}
116
   */
117
  public function resolveBufferArray(array $buffer) {
118
    /** @var \Drupal\Core\GeneratedUrl $url */
119
    $url = reset($buffer)['url']->toString(TRUE);
120
121
    $current = $this->requestStack->getCurrentRequest();
122
    $target = $url->getGeneratedUrl();
123
    $request = $this->createRequest($current, $buffer, $target);
124
    
125
    /** @var \Drupal\graphql\GraphQL\Buffers\SubRequestResponse $response */
126
    $response = $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST);
127
    while ($response instanceof LocalRedirectResponse) {
128
      $target = $response->getTargetUrl();
129
      $request = $this->createRequest($current, $buffer, $target);
130
      $response = $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST);
131
    }
132
    
133
    if (!($response instanceof SubRequestResponse)) {
134
      return array_fill_keys(array_keys($buffer), NULL);
135
    }
136
137
    // TODO:
138
    // Remove the request stack manipulation once the core issue described at
139
    // https://www.drupal.org/node/2613044 is resolved.
140
    while ($this->requestStack->getCurrentRequest() !== $current) {
141
      $this->requestStack->pop();
142
    }
143
144
    if ($url instanceof CacheableDependencyInterface) {
145
      $response->addCacheableDependency($url);
146
    }
147
    
148
    return array_map(function ($value) use ($response) {
149
      return new CacheableValue($value, [$response]);
150
    }, $response->getResult());
151
  }
152
153
}
154