You are here

public function HttpDataCollector::collect in Devel 8.3

Same name and namespace in other branches
  1. 8 webprofiler/src/DataCollector/HttpDataCollector.php \Drupal\webprofiler\DataCollector\HttpDataCollector::collect()
  2. 8.2 webprofiler/src/DataCollector/HttpDataCollector.php \Drupal\webprofiler\DataCollector\HttpDataCollector::collect()
  3. 4.x webprofiler/src/DataCollector/HttpDataCollector.php \Drupal\webprofiler\DataCollector\HttpDataCollector::collect()

File

webprofiler/src/DataCollector/HttpDataCollector.php, line 37

Class

HttpDataCollector
Collects data about http calls during request.

Namespace

Drupal\webprofiler\DataCollector

Code

public function collect(Request $request, Response $response, \Exception $exception = NULL) {
  $completed = $this->middleware
    ->getCompletedRequests();
  $failed = $this->middleware
    ->getFailedRequests();
  foreach ($completed as $data) {

    /** @var \GuzzleHttp\Psr7\Request $request */
    $request = $data['request'];

    /** @var \GuzzleHttp\Psr7\Response $response */
    $response = $data['response'];

    /** @var \GuzzleHttp\TransferStats $stats */
    $stats = $request->stats;
    $uri = $request
      ->getUri();
    $this->data['completed'][] = [
      'request' => [
        'method' => $request
          ->getMethod(),
        'uri' => [
          'schema' => $uri
            ->getScheme(),
          'host' => $uri
            ->getHost(),
          'port' => $uri
            ->getPort(),
          'path' => $uri
            ->getPath(),
          'query' => $uri
            ->getQuery(),
          'fragment' => $uri
            ->getFragment(),
        ],
        'headers' => $request
          ->getHeaders(),
        'protocol' => $request
          ->getProtocolVersion(),
        'request_target' => $request
          ->getRequestTarget(),
        'stats' => [
          'transferTime' => $stats
            ->getTransferTime(),
          'handlerStats' => $stats
            ->getHandlerStats(),
        ],
      ],
      'response' => [
        'phrase' => $response
          ->getReasonPhrase(),
        'status' => $response
          ->getStatusCode(),
        'headers' => $response
          ->getHeaders(),
        'protocol' => $response
          ->getProtocolVersion(),
      ],
    ];
  }
  foreach ($failed as $data) {

    /** @var \GuzzleHttp\Psr7\Request $request */
    $request = $data['request'];

    /** @var \GuzzleHttp\Psr7\Response $response */
    $response = $data['response'];
    $uri = $request
      ->getUri();
    $failureData = [
      'request' => [
        'method' => $request
          ->getMethod(),
        'uri' => [
          'schema' => $uri
            ->getScheme(),
          'host' => $uri
            ->getHost(),
          'port' => $uri
            ->getPort(),
          'path' => $uri
            ->getPath(),
          'query' => $uri
            ->getQuery(),
          'fragment' => $uri
            ->getFragment(),
        ],
        'headers' => $request
          ->getHeaders(),
        'protocol' => $request
          ->getProtocolVersion(),
        'request_target' => $request
          ->getRequestTarget(),
      ],
    ];
    if ($response) {
      $failureData['response'] = [
        'phrase' => $response
          ->getReasonPhrase(),
        'status' => $response
          ->getStatusCode(),
        'headers' => $response
          ->getHeaders(),
        'protocol' => $response
          ->getProtocolVersion(),
      ];
    }
    $this->data['failed'][] = $failureData;
  }
}