|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Debug; |
|
4
|
|
|
|
|
5
|
|
|
use DebugBar\DataCollector\ConfigCollector; |
|
6
|
|
|
use DebugBar\StandardDebugBar; |
|
7
|
|
|
use Ffcms\Core\App; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Debug. Provide methods of display information about debug and collected data in debug bar |
|
12
|
|
|
* @package Ffcms\Core |
|
13
|
|
|
*/ |
|
14
|
|
|
class Manager |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
public $bar; |
|
18
|
|
|
public $render; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Manager constructor. Construct debug manager - build debug bar, javascripts and initialize configs |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->bar = new StandardDebugBar(); |
|
26
|
|
|
$this->render = $this->bar->getJavascriptRenderer(); |
|
27
|
|
|
|
|
28
|
|
|
$this->bar->addCollector(new ConfigCollector()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Render debug bar header |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function renderHead() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->render->renderHead(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Render debug bar code |
|
42
|
|
|
* @return string |
|
43
|
|
|
* @throws \DebugBar\DebugBarException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function renderOut() |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$this->bar->hasCollector('queries')) { |
|
48
|
|
|
$timeCollector = null; |
|
49
|
|
|
$log = App::$Database->connection()->getQueryLog(); |
|
50
|
|
|
if ($this->bar->hasCollector('time')) { |
|
51
|
|
|
$timeCollector = $this->bar->getCollector('time'); |
|
52
|
|
|
} |
|
53
|
|
|
$queryCollector = new LaravelDatabaseCollector($timeCollector, $log); |
|
|
|
|
|
|
54
|
|
|
$this->bar->addCollector($queryCollector); |
|
55
|
|
|
} |
|
56
|
|
|
return $this->render->render(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add exception into debug bar and stop execute |
|
61
|
|
|
* @param $e |
|
62
|
|
|
* @throws \DebugBar\DebugBarException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function addException($e) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($e instanceof \Exception) { |
|
67
|
|
|
$this->bar->getCollector('exceptions')->addException($e); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add message into debug bar |
|
73
|
|
|
* @param string $m |
|
74
|
|
|
* @param string $type |
|
75
|
|
|
* @throws \DebugBar\DebugBarException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function addMessage($m, $type = 'info') |
|
78
|
|
|
{ |
|
79
|
|
|
if (!Obj::isString($m) || !Obj::isString($type)) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
$m = App::$Security->secureHtml($m); |
|
83
|
|
|
$mCollector = $this->bar->getCollector('messages'); |
|
84
|
|
|
|
|
85
|
|
|
if (method_exists($mCollector, $type)) { |
|
86
|
|
|
$this->bar->getCollector('messages')->{$type}($m); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Add message debug data to bar |
|
92
|
|
|
* @param $data |
|
93
|
|
|
* @throws \DebugBar\DebugBarException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function vardump($data) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->bar->getCollector('messages')->info($data); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Start timeline measure |
|
102
|
|
|
* @param string $key |
|
103
|
|
|
*/ |
|
104
|
|
|
public function startMeasure(string $key): void |
|
105
|
|
|
{ |
|
106
|
|
|
$this->bar['time']->startMeasure($key); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Stop timeline measure |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
*/ |
|
113
|
|
|
public function stopMeasure(string $key): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->bar['time']->stopMeasure($key); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Check if debug bar is enabled. Method called before __construct() is initiated!! |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function isEnabled() |
|
123
|
|
|
{ |
|
124
|
|
|
$property = App::$Properties->get('debug'); |
|
125
|
|
|
// $_COOKIE used insted of symfony request, cuz debug initialize early |
|
126
|
|
|
return ($property['all'] === true || $_COOKIE[$property['cookie']['key']] === $property['cookie']['value']); |
|
127
|
|
|
} |
|
128
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.