Issues (645)

src/Routing/QueryRouteEnhancer.php (4 issues)

1
<?php
2
3
namespace Drupal\graphql\Routing;
4
5
use Drupal\Component\Utility\NestedArray;
0 ignored issues
show
The type Drupal\Component\Utility\NestedArray 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\Routing\EnhancerInterface;
0 ignored issues
show
The type Drupal\Core\Routing\EnhancerInterface 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\RouteObjectInterface;
0 ignored issues
show
The type Drupal\Core\Routing\RouteObjectInterface 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\Utility\JsonHelper;
9
use GraphQL\Server\Helper;
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
12
13
class QueryRouteEnhancer implements EnhancerInterface {
14
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function enhance(array $defaults, Request $request) {
19
    $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
20
    if (!$route->hasDefault('_graphql')) {
21
      return $defaults;
22
    }
23
24
    $helper = new Helper();
25
    $method = $request->getMethod();
26
    $body = $this->extractBody($request);
27
    $query = $this->extractQuery($request);
28
    $operations = $helper->parseRequestParams($method, $body, $query);
29
30
    // By default we assume a 'single' request. This is going to fail in the
31
    // graphql processor due to a missing query string but at least provides
32
    // the right format for the client to act upon.
33
    if (isset($defaults['_graphql']['single'])) {
34
      $defaults += [
35
        '_controller' => $defaults['_graphql']['single'],
36
      ];
37
    }
38
39
    return $defaults + [
40
      'operations' => $operations,
41
    ];
42
  }
43
44
  /**
45
   * Extracts the query parameters from a request.
46
   *
47
   * @param \Symfony\Component\HttpFoundation\Request $request
48
   *   The http request object.
49
   *
50
   * @return array
51
   *   The normalized query parameters.
52
   */
53
  protected function extractQuery(Request $request) {
54
    return JsonHelper::decodeParams($request->query->all());
55
  }
56
57
  /**
58
   * Extracts the body parameters from a request.
59
   *
60
   * @param \Symfony\Component\HttpFoundation\Request $request
61
   *   The http request object.
62
   *
63
   * @return array
64
   *   The normalized body parameters.
65
   */
66
  protected function extractBody(Request $request) {
67
    $values = [];
68
69
    // Extract the request content.
70
    if ($content = json_decode($request->getContent(), TRUE)) {
71
      $values = array_merge($values, JsonHelper::decodeParams($content));
72
    }
73
74
    if (stripos($request->headers->get('content-type') ?? '', 'multipart/form-data') !== FALSE) {
75
      return $this->extractMultipart($request, $values);
76
    }
77
78
    return $values;
79
  }
80
81
  /**
82
   * Handles file uploads from multipart/form-data requests.
83
   *
84
   * @return array
85
   *   The query parameters with added file uploads.
86
   */
87
  protected function extractMultipart(Request $request, $values) {
88
    // The request body parameters might contain file upload mutations. We treat
89
    // them according to the graphql multipart request specification.
90
    //
91
    // @see https://github.com/jaydenseric/graphql-multipart-request-spec#server
92
    if ($body = JsonHelper::decodeParams($request->request->all())) {
93
      // Flatten the operations array if it exists.
94
      $operations = isset($body['operations']) && is_array($body['operations']) ? $body['operations'] : [];
95
      $values = array_merge($values, $body, $operations);
96
    }
97
98
    // According to the graphql multipart request specification, uploaded files
99
    // are referenced to variable placeholders in a map. Here, we resolve this
100
    // map by assigning the uploaded files to the corresponding variables.
101
    if (!empty($values['map']) && is_array($values['map']) && $files = $request->files->all()) {
102
      foreach ($files as $key => $file) {
103
        if (!isset($values['map'][$key])) {
104
          continue;
105
        }
106
107
        $paths = (array) $values['map'][$key];
108
        foreach ($paths as $path) {
109
          $path = explode('.', $path);
110
111
          if (NestedArray::keyExists($values, $path)) {
112
            NestedArray::setValue($values, $path, $file);
113
          }
114
        }
115
      }
116
    }
117
118
    return $values;
119
  }
120
121
}
122