You are here

class TimeDataCollector in Devel 4.x

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

Class TimeDataCollector.

Hierarchy

Expanded class hierarchy of TimeDataCollector

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

File

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

Namespace

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

  /**
   * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
   * @param $stopwatch
   */
  public function __construct(KernelInterface $kernel = NULL, $stopwatch = NULL) {
    parent::__construct($kernel, $stopwatch);
  }

  /**
   * {@inheritdoc}
   */
  public function collect(Request $request, Response $response, \Exception $exception = NULL) {
    parent::collect($request, $response, $exception);
    $this->data['memory_limit'] = $this
      ->convertToBytes(ini_get('memory_limit'));
    $this
      ->updateMemoryUsage();
  }

  /**
   * {@inheritdoc}
   */
  public function lateCollect() {
    parent::lateCollect();
    $this
      ->updateMemoryUsage();
  }

  /**
   * Gets the memory.
   *
   * @return int
   *   The memory
   */
  public function getMemory() {
    return $this->data['memory'];
  }

  /**
   * Gets the PHP memory limit.
   *
   * @return int
   *   The memory limit
   */
  public function getMemoryLimit() {
    return $this->data['memory_limit'];
  }

  /**
   * Updates the memory usage data.
   */
  public function updateMemoryUsage() {
    $this->data['memory'] = memory_get_peak_usage(TRUE);
  }

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

  /**
   * {@inheritdoc}
   */
  public function getPanelSummary() {
    return $this
      ->t('Duration: @duration', [
      '@duration' => sprintf('%.0f ms', $this
        ->getDuration()),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getIcon() {
    return 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAcCAYAAABoMT8aAAABqUlEQVR42t2Vv0sCYRyHX9OmEhsMx/YKGlwLQ69DTEUSBJEQEy5J3FRc/BsuiFqEIIcQIRo6ysUhoaBBWhoaGoJwiMJLglRKrs8bXgienmkQdPDAwX2f57j3fhFJkkbiPwTK5bIiFoul3kmPud8MqKMewDXpwuGww+12n9hsNhFnlijYf/Z4PDmO45Yxo+10ZFGTyWRMEItU6AdCx7lczkgd6n7J2Wx2xm63P6jJMk6n80YQBBN1aUDv9XqvlAbbm2LE7/cLODRB0un0VveAeoDC8/waCQQC18MGQqHQOcEKvw8bcLlcL6TfYnVtCrGRAlartUUYhmn1jKg/E3USjUYfhw3E4/F7ks/nz4YNFIvFQ/ogbUYikdefyqlU6gnuOg2YK5XKvs/n+xhUDgaDTVEUt+HO04ABOBA5isViDTU5kUi81Wq1AzhWMEkDGmAEq2C3UCjcYXGauDvfEsuyUjKZbJRKpVvM8IABU9SVX+cxYABmwIE9cFqtVi9xtgvsC2AHbIAFoKey0gdlHEyDObAEWLACFsEsMALdIJ80+dK0bTS95v7+v/AJnis0eO906QwAAAAASUVORK5CYII=';
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries() {
    return [
      'webprofiler/timeline',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getDrupalSettings() {

    /** @var \Symfony\Component\Stopwatch\StopwatchEvent[] $collectedEvents */
    $collectedEvents = $this
      ->getEvents();
    if (!empty($collectedEvents)) {
      $sectionPeriods = $collectedEvents['__section__']
        ->getPeriods();
      $endTime = end($sectionPeriods)
        ->getEndTime();
      $events = [];
      foreach ($collectedEvents as $key => $collectedEvent) {
        if ('__section__' != $key) {
          $periods = [];
          foreach ($collectedEvent
            ->getPeriods() as $period) {
            $periods[] = [
              'start' => sprintf("%F", $period
                ->getStartTime()),
              'end' => sprintf("%F", $period
                ->getEndTime()),
            ];
          }
          $events[] = [
            "name" => $key,
            "category" => $collectedEvent
              ->getCategory(),
            "origin" => sprintf("%F", $collectedEvent
              ->getOrigin()),
            "starttime" => sprintf("%F", $collectedEvent
              ->getStartTime()),
            "endtime" => sprintf("%F", $collectedEvent
              ->getEndTime()),
            "duration" => sprintf("%F", $collectedEvent
              ->getDuration()),
            "memory" => sprintf("%.1F", $collectedEvent
              ->getMemory() / 1024 / 1024),
            "periods" => $periods,
          ];
        }
      }
      return [
        'time' => [
          'events' => $events,
          'endtime' => $endTime,
        ],
      ];
    }
    else {
      return [
        'time' => [
          'events' => [],
          'endtime' => 0,
        ],
      ];
    }
  }

  /**
   * @return array
   */
  public function getData() {
    $data = $this->data;
    $data['duration'] = $this
      ->getDuration();
    $data['initTime'] = $this
      ->getInitTime();
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDataCollectorInterface::getName public function Returns the name of the collector. 21
DrupalDataCollectorInterface::hasPanel public function Returns true if this datacollector has a detail panel. 2
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.
TimeDataCollector::collect public function
TimeDataCollector::getData public function Overrides DrupalDataCollectorInterface::getData
TimeDataCollector::getDrupalSettings public function Overrides DrupalDataCollectorInterface::getDrupalSettings
TimeDataCollector::getIcon public function Returns the collector icon in base64 format. Overrides DrupalDataCollectorInterface::getIcon
TimeDataCollector::getLibraries public function Returns the libraries needed in detail panel. Overrides DrupalDataCollectorInterface::getLibraries
TimeDataCollector::getMemory public function Gets the memory.
TimeDataCollector::getMemoryLimit public function Gets the PHP memory limit.
TimeDataCollector::getPanelSummary public function Returns the string used in vertical tab summary. Overrides DrupalDataCollectorInterface::getPanelSummary
TimeDataCollector::getTitle public function Returns the datacollector title. Overrides DrupalDataCollectorInterface::getTitle
TimeDataCollector::lateCollect public function
TimeDataCollector::updateMemoryUsage public function Updates the memory usage data.
TimeDataCollector::__construct public function