1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\LaravelTracy; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Tracy\Bar; |
8
|
|
|
use Tracy\Helpers; |
9
|
|
|
use ErrorException; |
10
|
|
|
use Tracy\Debugger; |
11
|
|
|
use Tracy\BlueScreen; |
12
|
|
|
use Illuminate\Support\Arr; |
13
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
14
|
|
|
|
15
|
|
|
class DebuggerManager |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* $config. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* $bar. |
26
|
|
|
* |
27
|
|
|
* @var \Tracy\Bar |
28
|
|
|
*/ |
29
|
|
|
protected $bar; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* $blueScreen. |
33
|
|
|
* |
34
|
|
|
* @var \Tracy\BlueScreen |
35
|
|
|
*/ |
36
|
|
|
protected $blueScreen; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* $urlGenerator. |
40
|
|
|
* |
41
|
|
|
* @var \Illuminate\Contracts\Routing\UrlGenerator |
42
|
|
|
*/ |
43
|
|
|
protected $urlGenerator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* __construct. |
47
|
|
|
* |
48
|
|
|
* @param array $config |
49
|
|
|
* @param \Tracy\Bar $bar |
50
|
|
|
* @param \Tracy\BlueScreen $blueScreen |
51
|
|
|
*/ |
52
|
13 |
|
public function __construct($config = [], Bar $bar = null, BlueScreen $blueScreen = null) |
53
|
|
|
{ |
54
|
13 |
|
$this->config = $config; |
55
|
13 |
|
$this->bar = $bar ?: Debugger::getBar(); |
56
|
13 |
|
$this->blueScreen = $blueScreen ?: Debugger::getBlueScreen(); |
57
|
13 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* init. |
61
|
|
|
* |
62
|
|
|
* @param array $config |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
3 |
|
public static function init($config = []) |
66
|
|
|
{ |
67
|
3 |
|
$config = array_merge([ |
68
|
3 |
|
'accepts' => [], |
69
|
3 |
|
'appendTo' => 'body', |
70
|
3 |
|
'showBar' => false, |
71
|
3 |
|
'editor' => Debugger::$editor, |
72
|
3 |
|
'maxDepth' => Debugger::$maxDepth, |
73
|
3 |
|
'maxLength' => Debugger::$maxLength, |
74
|
3 |
|
'scream' => true, |
75
|
3 |
|
'showLocation' => true, |
76
|
3 |
|
'strictMode' => true, |
77
|
3 |
|
'currentTime' => $_SERVER['REQUEST_TIME_FLOAT'] ?: microtime(true), |
78
|
3 |
|
'editorMapping' => isset(Debugger::$editorMapping) === true ? Debugger::$editorMapping : [], |
79
|
3 |
|
], $config); |
80
|
|
|
|
81
|
3 |
|
Debugger::$editor = $config['editor']; |
82
|
3 |
|
Debugger::$maxDepth = $config['maxDepth']; |
83
|
3 |
|
Debugger::$maxLength = $config['maxLength']; |
84
|
3 |
|
Debugger::$scream = $config['scream']; |
85
|
3 |
|
Debugger::$showLocation = $config['showLocation']; |
86
|
3 |
|
Debugger::$strictMode = $config['strictMode']; |
87
|
3 |
|
Debugger::$time = $config['currentTime']; |
88
|
|
|
|
89
|
3 |
|
if (isset(Debugger::$editorMapping) === true) { |
90
|
3 |
|
Debugger::$editorMapping = $config['editorMapping']; |
91
|
3 |
|
} |
92
|
|
|
|
93
|
3 |
|
return $config; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* enabled. |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
1 |
|
public function enabled() |
102
|
|
|
{ |
103
|
1 |
|
return Arr::get($this->config, 'enabled', true) === true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* showBar. |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
1 |
|
public function showBar() |
112
|
|
|
{ |
113
|
1 |
|
return Arr::get($this->config, 'showBar', true) === true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* accepts. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
1 |
|
public function accepts() |
122
|
|
|
{ |
123
|
1 |
|
return Arr::get($this->config, 'accepts', []); |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
public function setUrlGenerator(UrlGenerator $urlGenerator) |
127
|
|
|
{ |
128
|
2 |
|
$this->urlGenerator = $urlGenerator; |
129
|
|
|
|
130
|
2 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* dispatchAssets. |
135
|
|
|
* |
136
|
|
|
* @param string $type |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
4 |
|
public function dispatchAssets($type) |
140
|
|
|
{ |
141
|
|
|
switch ($type) { |
142
|
4 |
|
case 'css': |
143
|
4 |
|
case 'js': |
144
|
4 |
|
case 'assets': |
145
|
|
|
$headers = [ |
146
|
3 |
|
'Content-Type' => $type === 'css' ? 'text/css; charset=utf-8' : 'text/javascript; charset=utf-8', |
147
|
3 |
|
'Cache-Control' => 'max-age=86400', |
148
|
3 |
|
]; |
149
|
3 |
|
$content = $this->replacePath( |
150
|
|
|
$this->renderBuffer(function () { |
151
|
3 |
|
return $this->bar->dispatchAssets(); |
152
|
3 |
|
}) |
153
|
3 |
|
); |
154
|
3 |
|
break; |
155
|
1 |
|
default: |
156
|
|
|
$headers = [ |
157
|
1 |
|
'Content-Type' => 'text/javascript; charset=utf-8', |
158
|
1 |
|
]; |
159
|
1 |
|
$content = $this->dispatch(); |
160
|
1 |
|
break; |
161
|
1 |
|
} |
162
|
|
|
|
163
|
|
|
return [ |
164
|
4 |
|
array_merge($headers, [ |
165
|
4 |
|
'Content-Length' => strlen($content), |
166
|
4 |
|
]), |
167
|
4 |
|
$content, |
168
|
4 |
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* dispatch. |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
1 |
|
public function dispatch() |
177
|
|
|
{ |
178
|
1 |
|
if (session_status() !== PHP_SESSION_ACTIVE) { |
179
|
1 |
|
ini_set('session.use_cookies', '1'); |
180
|
1 |
|
ini_set('session.use_only_cookies', '1'); |
181
|
1 |
|
ini_set('session.use_trans_sid', '0'); |
182
|
1 |
|
ini_set('session.cookie_path', '/'); |
183
|
1 |
|
ini_set('session.cookie_httponly', '1'); |
184
|
1 |
|
session_start(); |
185
|
1 |
|
} |
186
|
|
|
|
187
|
1 |
|
return $this->replacePath( |
188
|
|
|
$this->renderBuffer(function () { |
189
|
1 |
|
return method_exists($this->bar, 'dispatchContent') === true ? |
190
|
1 |
|
$this->bar->dispatchContent() : $this->bar->dispatchAssets(); |
191
|
1 |
|
}) |
192
|
1 |
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* shutdownHandler. |
197
|
|
|
* |
198
|
|
|
* @param string $content |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
4 |
|
public function shutdownHandler($content, $error = null) |
202
|
|
|
{ |
203
|
4 |
|
$error = $error ?: error_get_last(); |
204
|
4 |
|
if (in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE, E_RECOVERABLE_ERROR, E_USER_ERROR], true)) { |
205
|
1 |
|
return $this->exceptionHandler( |
206
|
1 |
|
Helpers::fixStack( |
207
|
1 |
|
new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']) |
208
|
1 |
|
) |
209
|
1 |
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
$bar = $this->replacePath( |
213
|
|
|
$this->renderBuffer(function () { |
214
|
3 |
|
$this->bar->render(); |
215
|
3 |
|
}) |
216
|
3 |
|
); |
217
|
|
|
|
218
|
3 |
|
$appendTo = Arr::get($this->config, 'appendTo', 'body'); |
219
|
3 |
|
$pos = strripos($content, '</'.$appendTo.'>'); |
220
|
|
|
|
221
|
|
|
return $pos !== false |
222
|
3 |
|
? substr($content, 0, $pos).$bar.substr($content, $pos) |
223
|
3 |
|
: $content.$bar; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* exceptionHandler. |
228
|
|
|
* |
229
|
|
|
* @param \Exception $exception |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function exceptionHandler(Exception $exception) |
233
|
|
|
{ |
234
|
2 |
|
return $this->renderBuffer(function () use ($exception) { |
235
|
2 |
|
Helpers::improveException($exception); |
236
|
2 |
|
$this->blueScreen->render($exception); |
237
|
2 |
|
}); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* renderBuffer. |
242
|
|
|
* |
243
|
|
|
* @param \Closure $callback |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
9 |
|
protected function renderBuffer(Closure $callback) |
247
|
|
|
{ |
248
|
9 |
|
ob_start(); |
249
|
9 |
|
$callback(); |
250
|
|
|
|
251
|
9 |
|
return ob_get_clean(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* replacePath. |
256
|
|
|
* |
257
|
|
|
* @param string $content |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
7 |
|
protected function replacePath($content) |
261
|
|
|
{ |
262
|
7 |
|
$path = is_null($this->urlGenerator) === false |
263
|
7 |
|
? $this->urlGenerator->route(Arr::get($this->config, 'route.as').'bar') |
264
|
7 |
|
: null; |
265
|
|
|
|
266
|
7 |
|
return empty($path) === false |
267
|
7 |
|
? str_replace('?_tracy_bar', $path.'?_tracy_bar', $content) |
268
|
7 |
|
: $content; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|