3 namespace Drupal\webprofiler\DataCollector;
5 use Drupal\webprofiler\DrupalDataCollectorInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector as BaseTimeDataCollector;
10 use Symfony\Component\HttpKernel\KernelInterface;
11 use Symfony\Component\Stopwatch\StopwatchEvent;
14 * Class TimeDataCollector.
16 class TimeDataCollector extends BaseTimeDataCollector implements DrupalDataCollectorInterface {
18 use StringTranslationTrait, DrupalDataCollectorTrait;
21 * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
24 public function __construct(KernelInterface $kernel = NULL, $stopwatch = NULL) {
25 parent::__construct($kernel, $stopwatch);
31 public function collect(Request $request, Response $response, \Exception $exception = NULL) {
32 parent::collect($request, $response, $exception);
34 $this->data['memory_limit'] = $this->convertToBytes(ini_get('memory_limit'));
35 $this->updateMemoryUsage();
41 public function lateCollect() {
42 parent::lateCollect();
44 $this->updateMemoryUsage();
53 public function getMemory() {
54 return $this->data['memory'];
58 * Gets the PHP memory limit.
63 public function getMemoryLimit() {
64 return $this->data['memory_limit'];
68 * Updates the memory usage data.
70 public function updateMemoryUsage() {
71 $this->data['memory'] = memory_get_peak_usage(TRUE);
77 public function getTitle() {
78 return $this->t('Timeline');
84 public function getPanelSummary() {
85 return $this->t('Duration: @duration', ['@duration' => sprintf('%.0f ms', $this->getDuration())]);
91 public function getIcon() {
92 return 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAcCAYAAABoMT8aAAABqUlEQVR42t2Vv0sCYRyHX9OmEhsMx/YKGlwLQ69DTEUSBJEQEy5J3FRc/BsuiFqEIIcQIRo6ysUhoaBBWhoaGoJwiMJLglRKrs8bXgienmkQdPDAwX2f57j3fhFJkkbiPwTK5bIiFoul3kmPud8MqKMewDXpwuGww+12n9hsNhFnlijYf/Z4PDmO45Yxo+10ZFGTyWRMEItU6AdCx7lczkgd6n7J2Wx2xm63P6jJMk6n80YQBBN1aUDv9XqvlAbbm2LE7/cLODRB0un0VveAeoDC8/waCQQC18MGQqHQOcEKvw8bcLlcL6TfYnVtCrGRAlartUUYhmn1jKg/E3USjUYfhw3E4/F7ks/nz4YNFIvFQ/ogbUYikdefyqlU6gnuOg2YK5XKvs/n+xhUDgaDTVEUt+HO04ABOBA5isViDTU5kUi81Wq1AzhWMEkDGmAEq2C3UCjcYXGauDvfEsuyUjKZbJRKpVvM8IABU9SVX+cxYABmwIE9cFqtVi9xtgvsC2AHbIAFoKey0gdlHEyDObAEWLACFsEsMALdIJ80+dK0bTS95v7+v/AJnis0eO906QwAAAAASUVORK5CYII=';
98 public function getLibraries() {
100 'webprofiler/timeline',
107 public function getDrupalSettings() {
108 /** @var StopwatchEvent[] $collectedEvents */
109 $collectedEvents = $this->getEvents();
111 if (!empty($collectedEvents)) {
112 $sectionPeriods = $collectedEvents['__section__']->getPeriods();
113 $endTime = end($sectionPeriods)->getEndTime();
116 foreach ($collectedEvents as $key => $collectedEvent) {
117 if ('__section__' != $key) {
119 foreach ($collectedEvent->getPeriods() as $period) {
121 'start' => sprintf("%F", $period->getStartTime()),
122 'end' => sprintf("%F", $period->getEndTime()),
128 "category" => $collectedEvent->getCategory(),
129 "origin" => sprintf("%F", $collectedEvent->getOrigin()),
130 "starttime" => sprintf("%F", $collectedEvent->getStartTime()),
131 "endtime" => sprintf("%F", $collectedEvent->getEndTime()),
132 "duration" => sprintf("%F", $collectedEvent->getDuration()),
133 "memory" => sprintf("%.1F", $collectedEvent->getMemory() / 1024 / 1024),
134 "periods" => $periods,
139 return ['time' => ['events' => $events, 'endtime' => $endTime]];
142 return ['time' => ['events' => [], 'endtime' => 0]];
149 public function getData() {
152 $data['duration'] = $this->getDuration();
153 $data['initTime'] = $this->getInitTime();