d2aa0442de5526310f520c5d8c4aca3538400e06
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / DataCollector / TimeDataCollector.php
1 <?php
2
3 namespace Drupal\webprofiler\DataCollector;
4
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;
12
13 /**
14  * Class TimeDataCollector.
15  */
16 class TimeDataCollector extends BaseTimeDataCollector implements DrupalDataCollectorInterface {
17
18   use StringTranslationTrait, DrupalDataCollectorTrait;
19
20   /**
21    * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
22    * @param $stopwatch
23    */
24   public function __construct(KernelInterface $kernel = NULL, $stopwatch = NULL) {
25     parent::__construct($kernel, $stopwatch);
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public function collect(Request $request, Response $response, \Exception $exception = NULL) {
32     parent::collect($request, $response, $exception);
33
34     $this->data['memory_limit'] = $this->convertToBytes(ini_get('memory_limit'));
35     $this->updateMemoryUsage();
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function lateCollect() {
42     parent::lateCollect();
43
44     $this->updateMemoryUsage();
45   }
46
47   /**
48    * Gets the memory.
49    *
50    * @return int
51    *   The memory
52    */
53   public function getMemory() {
54     return $this->data['memory'];
55   }
56
57   /**
58    * Gets the PHP memory limit.
59    *
60    * @return int
61    *   The memory limit
62    */
63   public function getMemoryLimit() {
64     return $this->data['memory_limit'];
65   }
66
67   /**
68    * Updates the memory usage data.
69    */
70   public function updateMemoryUsage() {
71     $this->data['memory'] = memory_get_peak_usage(TRUE);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getTitle() {
78     return $this->t('Timeline');
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getPanelSummary() {
85     return $this->t('Duration: @duration', ['@duration' => sprintf('%.0f ms', $this->getDuration())]);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
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=';
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function getLibraries() {
99     return [
100       'webprofiler/timeline',
101     ];
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getDrupalSettings() {
108     /** @var StopwatchEvent[] $collectedEvents */
109     $collectedEvents = $this->getEvents();
110
111     if (!empty($collectedEvents)) {
112       $sectionPeriods = $collectedEvents['__section__']->getPeriods();
113       $endTime = end($sectionPeriods)->getEndTime();
114       $events = [];
115
116       foreach ($collectedEvents as $key => $collectedEvent) {
117         if ('__section__' != $key) {
118           $periods = [];
119           foreach ($collectedEvent->getPeriods() as $period) {
120             $periods[] = [
121               'start' => sprintf("%F", $period->getStartTime()),
122               'end' => sprintf("%F", $period->getEndTime()),
123             ];
124           }
125
126           $events[] = [
127             "name" => $key,
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,
135           ];
136         }
137       }
138
139       return ['time' => ['events' => $events, 'endtime' => $endTime]];
140     }
141     else {
142       return ['time' => ['events' => [], 'endtime' => 0]];
143     }
144   }
145
146   /**
147    * @return array
148    */
149   public function getData() {
150     $data = $this->data;
151
152     $data['duration'] = $this->getDuration();
153     $data['initTime'] = $this->getInitTime();
154
155     return $data;
156   }
157 }