forked from zhuravljov/yii2-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYii2Debug.php
392 lines (364 loc) · 9.73 KB
/
Yii2Debug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* Application component of debug panel.
*
* @property string $tag
*
* @author Roman Zhuravlev <[email protected]>
* @package Yii2Debug
* @since 1.1.13
*/
class Yii2Debug extends CApplicationComponent
{
/**
* @var array the list of IPs that are allowed to access this module.
* Each array element represents a single IP filter which can be either an IP address
* or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
* The default value is `['127.0.0.1', '::1']`, which means the module can only be accessed
* by localhost.
*/
public $allowedIPs = array('127.0.0.1', '::1');
/**
* @var null|string|callback Additional php expression for access evaluation.
*/
public $accessExpression;
/**
* @var array|Yii2DebugPanel[] list debug panels. The array keys are the panel IDs, and values are the corresponding
* panel class names or configuration arrays. This will be merged with ::corePanels().
* You may reconfigure a core panel via this property by using the same panel ID.
* You may also disable a core panel by setting it to be false in this property.
*/
public $panels = array();
/**
* @var string the directory storing the debugger data files. This can be specified using a path alias.
*/
public $logPath;
/**
* @var integer the maximum number of debug data files to keep. If there are more files generated,
* the oldest ones will be removed.
*/
public $historySize = 50;
/**
* @var bool enable/disable component in application.
*/
public $enabled = true;
/**
* @var string module ID for viewing stored debug logs.
*/
public $moduleId = 'debug';
/**
* @var bool use nice route rules in debug module.
*/
public $internalUrls = true;
/**
* @var bool highlight code in debug logs.
*/
public $highlightCode = true;
/**
* @var bool show brief application configuration.
*/
public $showConfig = false;
/**
* @var array list of unsecure component options (like login, passwords, secret keys) that
* will be hidden in application configuration page.
*/
public $hiddenConfigOptions = array(
'components/db/username',
'components/db/password',
);
private $_tag;
/**
* Panel initialization.
* Generate unique tag for page. Attach panels, log watcher. Register scripts for printing debug panel.
*/
public function init()
{
parent::init();
if (!$this->enabled) return;
Yii::setPathOfAlias('yii2-debug', dirname(__FILE__));
Yii::app()->setImport(array(
'yii2-debug.*',
'yii2-debug.panels.*',
));
if ($this->logPath === null) {
$this->logPath = Yii::app()->getRuntimePath() . '/debug';
}
$panels = array();
foreach (CMap::mergeArray($this->corePanels(), $this->panels) as $id => $config) {
if (!isset($config['highlightCode'])) $config['highlightCode'] = $this->highlightCode;
$panels[$id] = Yii::createComponent($config, $this, $id);
}
$this->panels = $panels;
Yii::app()->setModules(array(
$this->moduleId => array(
'class' => 'Yii2DebugModule',
'owner' => $this,
),
));
if ($this->internalUrls && (Yii::app()->getUrlManager()->urlFormat == 'path')) {
$rules = array();
foreach ($this->coreUrlRules() as $key => $value) {
$rules[$this->moduleId . '/' . $key] = $this->moduleId . '/' . $value;
}
Yii::app()->getUrlManager()->addRules($rules, false);
}
Yii::app()->attachEventHandler('onEndRequest', array($this, 'onEndRequest'));
$this->initToolbar();
}
/**
* @return string current page tag
*/
public function getTag()
{
if ($this->_tag === null) $this->_tag = uniqid();
return $this->_tag;
}
/**
* @return array default panels
*/
protected function corePanels()
{
return array(
'config' => array(
'class' => 'Yii2ConfigPanel',
),
'request' => array(
'class' => 'Yii2RequestPanel',
),
'log' => array(
'class' => 'Yii2LogPanel',
),
'profiling' => array(
'class' => 'Yii2ProfilingPanel',
),
'db' => array(
'class' => 'Yii2DbPanel',
),
);
}
protected function coreUrlRules()
{
return array(
'' => 'default/index',
'<tag:[0-9a-f]+>/<action:toolbar|explain>' => 'default/<action>',
'<tag:[0-9a-f]+>/<panel:\w+>' => 'default/view',
'latest/<panel:\w+>' => 'default/view',
'<action:\w+>' => 'default/<action>',
);
}
/**
* Register debug panel scripts.
*/
protected function initToolbar()
{
if (!$this->checkAccess()) return;
$assetsUrl = CHtml::asset(dirname(__FILE__) . '/assets');
/* @var CClientScript $cs */
$cs = Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$url = Yii::app()->createUrl($this->moduleId . '/default/toolbar', array('tag' => $this->getTag()));
$cs->registerScript(__CLASS__ . '#toolbar', <<<JS
(function($){
$('<div>').appendTo('body').load('$url', function(){
if (window.localStorage && localStorage.getItem('yii2-debug-toolbar') == 'minimized') {
$('#yii2-debug-toolbar').hide();
$('#yii2-debug-toolbar-min').show();
} else {
$('#yii2-debug-toolbar-min').hide();
$('#yii2-debug-toolbar').show();
}
$('#yii2-debug-toolbar .yii2-debug-toolbar-toggler').click(function(){
$('#yii2-debug-toolbar').hide();
$('#yii2-debug-toolbar-min').show();
if (window.localStorage) {
localStorage.setItem('yii2-debug-toolbar', 'minimized');
}
});
$('#yii2-debug-toolbar-min .yii2-debug-toolbar-toggler').click(function(){
$('#yii2-debug-toolbar-min').hide();
$('#yii2-debug-toolbar').show();
if (window.localStorage) {
localStorage.setItem('yii2-debug-toolbar', 'maximized');
}
});
});
})(jQuery);
JS
);
}
/**
* @param CEvent $event
*/
protected function onEndRequest($event)
{
$this->processDebug();
}
/**
* Log processing routine.
*/
protected function processDebug()
{
$path = $this->logPath;
if (!is_dir($path)) mkdir($path);
$indexFile = "$path/index.data";
$manifest = array();
if (is_file($indexFile)) {
$manifest = unserialize(file_get_contents($indexFile));
}
$data = array();
foreach ($this->panels as $panel) {
$data[$panel->getId()] = $panel->save();
if (isset($panel->filterData)) {
$data[$panel->getId()] = $panel->evaluateExpression(
$panel->filterData,
array('data' => $data[$panel->getId()])
);
}
$panel->load($data[$panel->getId()]);
}
$statusCode = null;
if (isset($this->panels['request']) && isset($this->panels['request']->data['statusCode'])) {
$statusCode = $this->panels['request']->data['statusCode'];
}
$request = Yii::app()->getRequest();
$manifest[$this->getTag()] = $data['summary'] = array(
'tag' => $this->getTag(),
'url' => $request->getHostInfo() . $request->getUrl(),
'ajax' => $request->getIsAjaxRequest(),
'method' => $request->getRequestType(),
'code' => $statusCode,
'ip' => $request->getUserHostAddress(),
'time' => time(),
);
$this->resizeHistory($manifest);
file_put_contents("$path/{$this->getTag()}.data", serialize($data));
file_put_contents($indexFile, serialize($manifest));
}
/**
* Debug files rotation according to {@link ::$historySize}.
* @param $manifest
*/
protected function resizeHistory(&$manifest)
{
$tags = array_keys($manifest);
$count = 0;
foreach ($tags as $tag) {
if (!$this->getLock($tag)) $count++;
}
if ($count > $this->historySize + 10) {
$path = $this->logPath;
$n = $count - $this->historySize;
foreach ($tags as $tag) {
if (!$this->getLock($tag)) {
@unlink("$path/$tag.data");
unset($manifest[$tag]);
if (--$n <= 0) break;
}
}
}
}
/**
* Check access rights.
* @return bool
*/
public function checkAccess()
{
if (
$this->accessExpression !== null &&
!$this->evaluateExpression($this->accessExpression)
) {
return false;
}
$ip = Yii::app()->getRequest()->getUserHostAddress();
foreach ($this->allowedIPs as $filter) {
if (
$filter === '*' || $filter === $ip || (
($pos = strpos($filter, '*')) !== false &&
!strncmp($ip, $filter, $pos)
)
) {
return true;
}
}
return false;
}
/**
* Dump variable to debug log.
* @param mixed $data
*/
public static function dump($data)
{
Yii::log(serialize($data), CLogger::LEVEL_INFO, Yii2LogPanel::CATEGORY_DUMP);
}
/**
* @var
*/
private $_locks;
/**
* @param string $tag
* @return bool
*/
public function getLock($tag)
{
if ($this->_locks === null) {
$locksFile = $this->logPath . '/locks.data';
if (is_file($locksFile)) {
$this->_locks = array_flip(unserialize(file_get_contents($locksFile)));
} else {
$this->_locks = array();
}
}
return isset($this->_locks[$tag]);
}
/**
* @param string $tag
* @param bool $value
*/
public function setLock($tag, $value)
{
$value = !!$value;
if ($this->getLock($tag) !== $value) {
if ($value) {
$this->_locks[$tag] = true;
} else {
unset($this->_locks[$tag]);
}
$locksFile = $this->logPath . '/locks.data';
file_put_contents($locksFile, serialize(array_keys($this->_locks)));
}
}
/**
* Convert data to plain array in recursive manner.
* @param mixed $data
* @return array
*/
public static function prepareData($data)
{
static $parents = array();
$result = array();
if (is_array($data) || $data instanceof CMap) {
foreach ($data as $key => $value) {
$result[$key] = self::prepareData($value);
}
} elseif (is_object($data)) {
if (!in_array($data, $parents, true)) {
array_push($parents, $data);
$result['class'] = get_class($data);
if ($data instanceof CActiveRecord) {
foreach ($data->attributes as $field => $value) {
$result[$field] = $value;
}
}
foreach (get_object_vars($data) as $key => $value) {
$result[$key] = self::prepareData($value);
}
array_pop($parents);
} else {
$result = get_class($data) . '()';
}
} else {
$result = $data;
}
return $result;
}
}