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