function 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;
    }
}