1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart\debug; |
12
|
|
|
|
13
|
|
|
use hiqdev\hiart\Command; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\InvalidConfigException; |
16
|
|
|
use yii\base\ViewContextInterface; |
17
|
|
|
use yii\log\Logger; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Debugger panel that collects and displays HiArt queries performed. |
21
|
|
|
*/ |
22
|
|
|
class DebugPanel extends \yii\debug\Panel implements ViewContextInterface |
23
|
|
|
{ |
24
|
|
|
public function init() |
25
|
|
|
{ |
26
|
|
|
$this->actions['hiart-query'] = [ |
27
|
|
|
'class' => DebugAction::class, |
28
|
|
|
'panel' => $this, |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function getName() |
36
|
|
|
{ |
37
|
|
|
return 'HiArt'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getSummary() |
44
|
|
|
{ |
45
|
|
|
$timings = $this->getTimings(); |
46
|
|
|
$total = 0; |
47
|
|
|
foreach ($timings as $timing) { |
48
|
|
|
$total += $timing[3]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->render('summary', [ |
52
|
|
|
'url' => $this->getUrl(), |
53
|
|
|
'count' => count($timings), |
54
|
|
|
'total' => number_format($total * 1000) . ' ms', |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getDetail() |
62
|
|
|
{ |
63
|
|
|
return $this->render('detail', [ |
64
|
|
|
'timings' => Timing::buildAll($this), |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private $_timings; |
69
|
|
|
|
70
|
|
|
public function getTimings() |
71
|
|
|
{ |
72
|
|
|
if ($this->_timings === null) { |
73
|
|
|
$this->_timings = $this->calculateTimings(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->_timings; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function calculateTimings() |
80
|
|
|
{ |
81
|
|
|
$messages = $this->data['messages']; |
82
|
|
|
$timings = []; |
83
|
|
|
$stack = []; |
84
|
|
|
foreach ($messages as $i => $log) { |
85
|
|
|
list($token, $level, $category, $timestamp) = $log; |
|
|
|
|
86
|
|
|
$log[5] = $i; |
87
|
|
|
if ($level === Logger::LEVEL_PROFILE_BEGIN) { |
88
|
|
|
$stack[] = $log; |
89
|
|
|
} elseif ($level === Logger::LEVEL_PROFILE_END) { |
90
|
|
|
$last = array_pop($stack); |
91
|
|
|
if ($last !== null && $last[0] === $token) { |
92
|
|
|
$timings[$last[5]] = [count($stack), $token, $last[3], $timestamp - $last[3], $last[4]]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$now = microtime(true); |
98
|
|
|
while (($last = array_pop($stack)) !== null) { |
99
|
|
|
$delta = $now - $last[3]; |
100
|
|
|
$timings[$last[5]] = [count($stack), $last[0], $last[2], $delta, $last[4]]; |
101
|
|
|
} |
102
|
|
|
ksort($timings); |
103
|
|
|
|
104
|
|
|
return $timings; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function save() |
111
|
|
|
{ |
112
|
|
|
$target = $this->module->logTarget; |
113
|
|
|
$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, [Command::getProfileCategory()]); |
114
|
|
|
|
115
|
|
|
return ['messages' => $messages]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected $_viewPath; |
119
|
|
|
|
120
|
|
|
public function setViewPath($value) |
121
|
|
|
{ |
122
|
|
|
$this->_viewPath = $value; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getViewPath() |
126
|
|
|
{ |
127
|
|
|
if ($this->_viewPath === null) { |
128
|
|
|
$this->_viewPath = dirname(__DIR__) . '/views/debug'; |
129
|
|
|
} |
130
|
|
|
return $this->_viewPath; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function render($file, $data) |
134
|
|
|
{ |
135
|
|
|
return Yii::$app->view->render($file, $data, $this); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.