1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Debugger\Traits; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use DB; |
7
|
|
|
use File; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
use Log; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class DebuggerTrait. |
14
|
|
|
* |
15
|
|
|
* @author Mahmoud Zalt <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
trait DebuggerTrait |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Write the DB queries in the Log and Display them in the |
22
|
|
|
* terminal (in case you want to see them while executing the tests). |
23
|
|
|
* |
24
|
|
|
* @param bool|false $terminal |
25
|
|
|
*/ |
26
|
|
|
public function runQueryDebugger($log = true, $terminal = false) |
27
|
|
|
{ |
28
|
|
|
if (Config::get('database.query_debugging')) { |
29
|
|
|
DB::listen(function ($event) use ($terminal, $log) { |
30
|
|
|
$fullQuery = vsprintf(str_replace(['%', '?'], ['%%', '%s'], $event->sql), $event->bindings); |
31
|
|
|
|
32
|
|
|
$text = $event->connectionName . ' (' . $event->time . '): ' . $fullQuery; |
33
|
|
|
|
34
|
|
|
if ($terminal) { |
35
|
|
|
dump($text); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($log) { |
39
|
|
|
Log::info($text); |
40
|
|
|
} |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $request |
47
|
|
|
* @param $response |
48
|
|
|
*/ |
49
|
|
|
public function runRequestDebugger($request, $response) |
50
|
|
|
{ |
51
|
|
|
if (App::environment() != 'testing' && Config::get('app.debug') === true) { |
52
|
|
|
|
53
|
|
|
Log::debug(''); |
54
|
|
|
Log::debug(''); |
55
|
|
|
Log::debug('REQUEST START------------------------------------------------------'); |
56
|
|
|
|
57
|
|
|
// Endpoint URL: |
58
|
|
|
Log::debug('URL: ' . $request->getMethod() . ' ' . $request->fullUrl()); |
59
|
|
|
|
60
|
|
|
// Request Device IP: |
61
|
|
|
Log::debug('IP: ' . $request->ip()); |
62
|
|
|
|
63
|
|
|
// Request Headers: |
64
|
|
|
Log::debug('App Headers: '); |
65
|
|
|
Log::debug(' Authorization = ' . substr($request->header('Authorization'), 0, 80) . '...'); |
66
|
|
|
|
67
|
|
|
// Request Data: |
68
|
|
|
if ($request->all()) { |
69
|
|
|
$data = http_build_query($request->all(), '', ' ; '); |
70
|
|
|
} else { |
71
|
|
|
$data = 'N/A'; |
72
|
|
|
} |
73
|
|
|
Log::debug('Request Data: ' . $data); |
74
|
|
|
|
75
|
|
|
// Authenticated User: |
76
|
|
|
if ($request->user()) { |
77
|
|
|
$user = 'ID: ' . $request->user()->id; |
78
|
|
|
} else { |
79
|
|
|
$user = 'N/A'; |
80
|
|
|
} |
81
|
|
|
Log::debug('Authenticated User: ' . $user); |
82
|
|
|
|
83
|
|
|
// Response Content: |
84
|
|
|
if ($response && method_exists($response, 'content')) { |
85
|
|
|
Log::debug('Response: ' . substr($response->content(), 0, 700) . '...'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|