Skip to content

Commit c4e9055

Browse files
authored
Merge pull request #9 from ppavlovic/master
Added __dbprofiler=2 to order profiler items by time
2 parents 49685b6 + f43f206 commit c4e9055

3 files changed

Lines changed: 45 additions & 9 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"g4/clean-core" : "*",
3131
"g4/http" : "*",
3232
"g4/di" : "*",
33-
"g4/profiler" : ">=1.10.0",
33+
"g4/profiler" : ">=1.11.0",
3434
"g4/factory" : "1.*",
3535
"twig/twig" : "1.*",
3636
"twig/extensions": "^1.2",

src/Presenter/Formatter/FormatterAbstract.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public function getDataTransfer()
4747
public function getProfilerData()
4848
{
4949
return $this->isProfilerEnabled()
50-
? ['profiler' => $this->getDataTransfer()->getProfiler()->getProfilerOutput($this->getDataTransfer()->getResponse()->getHttpResponseCode())]
50+
? [
51+
'profiler' => $this->getDataTransfer()->getProfiler()->getProfilerOutput(
52+
$this->getDataTransfer()->getResponse()->getHttpResponseCode(),
53+
$this->getDbProfilerRequestParam()
54+
)
55+
]
5156
: [];
5257
}
5358

@@ -58,8 +63,17 @@ public function setDataTransfer(DataTransfer $dataTransfer)
5863
}
5964

6065
private function isProfilerEnabled()
66+
{
67+
return $this->getDbProfilerRequestParam() !== null;
68+
}
69+
70+
/**
71+
* @return int|null
72+
*/
73+
protected function getDbProfilerRequestParam()
6174
{
6275
return $this->getDataTransfer()->getHttpRequest()->has(Override::DB_PROFILER)
63-
&& $this->getDataTransfer()->getHttpRequest()->get(Override::DB_PROFILER) == 1;
76+
? (int) $this->getDataTransfer()->getHttpRequest()->get(Override::DB_PROFILER)
77+
: null;
6478
}
65-
}
79+
}

src/Profiler.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class Profiler
1515
*/
1616
private $formatted;
1717

18+
/**
19+
* @var array
20+
*/
21+
private $timeline;
22+
1823
/**
1924
* @var array|TickerAbstract[]
2025
*/
@@ -57,10 +62,10 @@ public function setLogLevel($logLevel)
5762
/**
5863
* @return array
5964
*/
60-
public function getProfilerOutput($httpCode)
65+
public function getProfilerOutput($httpCode, $dbProfiler = 0)
6166
{
6267
return $this->hasProfilers() && $this->shouldLogProfiler($httpCode)
63-
? $this->getFormatted()
68+
? $this->getFormatted($dbProfiler)
6469
: [];
6570
}
6671

@@ -83,15 +88,32 @@ private function shouldLogProfiler($httpCode)
8388
/**
8489
* @return array
8590
*/
86-
private function getFormatted()
91+
private function getFormatted($dbProfiler)
8792
{
8893
if (!$this->hasFormatted()) {
8994
$this->formatted = [];
90-
foreach($this->profilers as $profiler) {
95+
$this->timeline = [];
96+
foreach ($this->profilers as $profiler) {
9197
$this->formatted[$profiler->getName()] = $profiler->getFormatted();
98+
$this->timeline[] = $profiler->getQueries();
99+
}
100+
}
101+
102+
if ((int) $dbProfiler === 1) {
103+
return $this->formatted;
104+
}
105+
106+
if ((int) $dbProfiler === 2) {
107+
$timelineFormatted = [];
108+
foreach ($this->timeline as $queries) {
109+
foreach ($queries as $key => $query) {
110+
$timelineFormatted[$key] = $query;
111+
}
92112
}
113+
ksort($timelineFormatted, SORT_NUMERIC);
114+
return $timelineFormatted;
93115
}
94-
return $this->formatted;
116+
return ['unsuported request parameter'];
95117
}
96118

97119
/**

0 commit comments

Comments
 (0)