1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.5.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Debugger; |
16
|
|
|
|
17
|
|
|
use DebugBar\DataCollector\RequestDataCollector; |
18
|
|
|
use DebugBar\DataCollector\TimeDataCollector; |
19
|
|
|
use DebugBar\DataCollector\MessagesCollector; |
20
|
|
|
use DebugBar\DataCollector\PhpInfoCollector; |
21
|
|
|
use DebugBar\DataCollector\MemoryCollector; |
22
|
|
|
use DebugBar\JavascriptRenderer; |
23
|
|
|
use DebugBar\DebugBar; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Debugger |
27
|
|
|
* @package Quantum\Debugger |
28
|
|
|
* @uses \DebugBar\DebugBar |
29
|
|
|
*/ |
30
|
|
|
class Debugger |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Messages tab |
35
|
|
|
*/ |
36
|
|
|
const MESSAGES = 'messages'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Quaeries tab |
40
|
|
|
*/ |
41
|
|
|
const QUERIES = 'queries'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Routes tab |
45
|
|
|
*/ |
46
|
|
|
const ROUTES = 'routes'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Mails tab |
50
|
|
|
*/ |
51
|
|
|
const MAILS = 'mails'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Debugbar instance |
55
|
|
|
* @var \DebugBar\DebugBar |
56
|
|
|
*/ |
57
|
|
|
private $debugbar; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Store |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private static $store; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Assets url |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $assetsUrl = '/assets/DebugBar/Resources'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Custom CSS |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
private $customCss = 'custom_debugbar.css'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Debugger constructor. |
79
|
|
|
* @throws \DebugBar\DebugBarException |
80
|
|
|
*/ |
81
|
|
|
public function __construct() |
82
|
|
|
{ |
83
|
|
|
$this->debugbar = new DebugBar(); |
84
|
|
|
|
85
|
|
|
$this->debugbar->addCollector(new PhpInfoCollector()); |
86
|
|
|
$this->debugbar->addCollector(new MessagesCollector()); |
87
|
|
|
$this->debugbar->addCollector(new RequestDataCollector()); |
88
|
|
|
$this->debugbar->addCollector(new TimeDataCollector()); |
89
|
|
|
$this->debugbar->addCollector(new MemoryCollector()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Initiates the store |
94
|
|
|
*/ |
95
|
|
|
public static function initStore() |
96
|
|
|
{ |
97
|
|
|
self::$store[self::MESSAGES] = []; |
98
|
|
|
self::$store[self::QUERIES] = []; |
99
|
|
|
self::$store[self::ROUTES] = []; |
100
|
|
|
self::$store[self::MAILS] = []; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Adds data to store |
105
|
|
|
* @param string $cell |
106
|
|
|
* @param string $level |
107
|
|
|
* @param mixed $data |
108
|
|
|
*/ |
109
|
|
|
public static function addToStore(string $cell, string $level, $data) |
110
|
|
|
{ |
111
|
|
|
if(!empty($data)) { |
112
|
|
|
array_push(self::$store[$cell], [$level => $data]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Renders the debug bar |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function render(): string |
121
|
|
|
{ |
122
|
|
|
$this->createTab(self::MESSAGES); |
123
|
|
|
$this->createTab(self::QUERIES); |
124
|
|
|
$this->createTab(self::ROUTES); |
125
|
|
|
$this->createTab(self::MAILS); |
126
|
|
|
|
127
|
|
|
$renderer = $this->getRenderer(); |
128
|
|
|
|
129
|
|
|
return $renderer->renderHead() . $renderer->render(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Creates a tab |
134
|
|
|
* @param string $type |
135
|
|
|
* @throws \DebugBar\DebugBarException |
136
|
|
|
*/ |
137
|
|
|
protected function createTab(string $type) |
138
|
|
|
{ |
139
|
|
|
if(!$this->debugbar->hasCollector($type)) { |
140
|
|
|
$this->debugbar->addCollector(new MessagesCollector($type)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (count(self::$store[$type])) { |
144
|
|
|
foreach (self::$store[$type] as $message) { |
145
|
|
|
$fn = key($message); |
146
|
|
|
$this->debugbar[$type]->$fn($message[$fn]); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Gets the renderer |
153
|
|
|
* @return \DebugBar\JavascriptRenderer |
154
|
|
|
*/ |
155
|
|
|
protected function getRenderer(): JavascriptRenderer |
156
|
|
|
{ |
157
|
|
|
return $this->debugbar |
158
|
|
|
->getJavascriptRenderer() |
159
|
|
|
->setBaseUrl(base_url() . $this->assetsUrl) |
160
|
|
|
->addAssets([$this->customCss], []); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|