You are here

class HttpDataCollector in Devel 4.x

Same name and namespace in other branches
  1. 8.3 webprofiler/src/DataCollector/HttpDataCollector.php \Drupal\webprofiler\DataCollector\HttpDataCollector
  2. 8 webprofiler/src/DataCollector/HttpDataCollector.php \Drupal\webprofiler\DataCollector\HttpDataCollector
  3. 8.2 webprofiler/src/DataCollector/HttpDataCollector.php \Drupal\webprofiler\DataCollector\HttpDataCollector

Collects data about http calls during request.

Hierarchy

Expanded class hierarchy of HttpDataCollector

1 string reference to 'HttpDataCollector'
webprofiler.services.yml in webprofiler/webprofiler.services.yml
webprofiler/webprofiler.services.yml
1 service uses HttpDataCollector
webprofiler.http in webprofiler/webprofiler.services.yml
Drupal\webprofiler\DataCollector\HttpDataCollector

File

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

Namespace

Drupal\webprofiler\DataCollector
View source
class HttpDataCollector extends DataCollector implements DrupalDataCollectorInterface {
  use StringTranslationTrait, DrupalDataCollectorTrait;

  /**
   * @var \GuzzleHttp\Client
   */
  private $middleware;

  /**
   * @param \Drupal\webprofiler\Http\HttpClientMiddleware $middleware
   */
  public function __construct(HttpClientMiddleware $middleware) {
    $this->middleware = $middleware;
    $this->data['completed'] = [];
    $this->data['failed'] = [];
  }

  /**
   * {@inheritdoc}
   */
  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;
    }
  }

  /**
   * @return int
   */
  public function getCompletedRequestsCount() {
    return count($this
      ->getCompletedRequests());
  }

  /**
   * @return array
   */
  public function getCompletedRequests() {
    return $this->data['completed'];
  }

  /**
   * @return int
   */
  public function getFailedRequestsCount() {
    return count($this
      ->getFailedRequests());
  }

  /**
   * @return array
   */
  public function getFailedRequests() {
    return $this->data['failed'];
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'http';
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this
      ->t('Http');
  }

  /**
   * {@inheritdoc}
   */
  public function getPanelSummary() {
    return $this
      ->t('Completed @completed, error @error', [
      '@completed' => $this
        ->getCompletedRequestsCount(),
      '@error' => $this
        ->getFailedRequestsCount(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getIcon() {
    return 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAcCAYAAACOGPReAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAATlJREFUeNrsleERgjAMha0TdISOwAZ2BEZghI7ABo7AOQFuwAjoBLgBblBbfb0LudByp/4jdw+PNnxtkqYq7/3h13Y8/MF26B9spfo6yAX1QSa6EY2Y0xLrzROgddAMOcgLmuFbhDqyG00W8Rm1JWgEWCG0oQBuJKjGigNUs/xWUJdJheZQJzhZFGkgKWkw1mM8bmTCvOPQcSVXrTA+Ydc0kujXJGg6p5VwrG5BJzb2CLriN9kT74afUylPloQ+kdDPELXpg1qGvwbtURwjFGkkC8RIZw6d2QeO7MII81wRbDm0Z068Zf0G1bxQl8z1UH1zoQwk9NRZdkPoHt+KbZoqa+HYZS4T3iimdEvRDrIF4KIRclBNjk+uSB2/eBJUvR9KSek26BZHOit2zl3oqkV91P6//3N7CTAAIIc/qj2gy4gAAAAASUVORK5CYII=';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDataCollectorInterface::getData public function 8
DrupalDataCollectorInterface::getDrupalSettings public function 1
DrupalDataCollectorInterface::getLibraries public function Returns the libraries needed in detail panel. 2
DrupalDataCollectorInterface::hasPanel public function Returns true if this datacollector has a detail panel. 2
HttpDataCollector::$middleware private property
HttpDataCollector::collect public function
HttpDataCollector::getCompletedRequests public function
HttpDataCollector::getCompletedRequestsCount public function
HttpDataCollector::getFailedRequests public function
HttpDataCollector::getFailedRequestsCount public function
HttpDataCollector::getIcon public function Returns the collector icon in base64 format. Overrides DrupalDataCollectorInterface::getIcon
HttpDataCollector::getName public function Returns the name of the collector. Overrides DrupalDataCollectorInterface::getName
HttpDataCollector::getPanelSummary public function Returns the string used in vertical tab summary. Overrides DrupalDataCollectorInterface::getPanelSummary
HttpDataCollector::getTitle public function Returns the datacollector title. Overrides DrupalDataCollectorInterface::getTitle
HttpDataCollector::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.