|
2 | 2 |
|
3 | 3 | namespace Rakutentech\LaravelRequestDocs; |
4 | 4 |
|
5 | | -use Illuminate\Auth\Middleware\Authenticate as Middleware; |
6 | | -use Illuminate\Http\Response; |
7 | | -use Illuminate\Http\JsonResponse; |
8 | | -use Illuminate\Http\Resources\Json\JsonResource; |
9 | 5 | use Closure; |
10 | | - |
11 | 6 | use Illuminate\Database\Events\QueryExecuted; |
12 | 7 | use Illuminate\Support\Facades\DB; |
| 8 | +use KitLoong\AppLogger\QueryLog\LogWriter as QueryLogger; |
13 | 9 |
|
14 | | -class LaravelRequestDocsMiddleware extends Middleware |
| 10 | +class LaravelRequestDocsMiddleware extends QueryLogger |
15 | 11 | { |
16 | 12 | private array $queries = []; |
17 | 13 |
|
18 | | - public function handle($request, Closure $next, ...$guards) |
| 14 | + public function handle($request, Closure $next) |
19 | 15 | { |
20 | 16 | if (!$request->headers->has('X-Request-LRD') || !config('app.debug')) { |
21 | 17 | return $next($request); |
@@ -50,45 +46,4 @@ public function listenDB() |
50 | 46 | $this->queries[] = $this->getMessages($query); |
51 | 47 | }); |
52 | 48 | } |
53 | | - |
54 | | - protected function getMessages(QueryExecuted $query): array |
55 | | - { |
56 | | - $sql = $query->sql; |
57 | | - |
58 | | - foreach ($query->bindings as $key => $binding) { |
59 | | - // https://github.com/barryvdh/laravel-debugbar/blob/master/src/DataCollector/QueryCollector.php#L138 |
60 | | - // This regex matches placeholders only, not the question marks, |
61 | | - // nested in quotes, while we iterate through the bindings |
62 | | - // and substitute placeholders by suitable values. |
63 | | - $regex = is_numeric($key) |
64 | | - ? "/(?<!\?)\?(?=(?:[^'\\\']*'[^'\\']*')*[^'\\\']*$)(?!\?)/" |
65 | | - : "/:{$key}(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/"; |
66 | | - |
67 | | - // Mimic bindValue and only string data types |
68 | | - if (is_string($binding)) { |
69 | | - $binding = $this->quote($binding); |
70 | | - } |
71 | | - |
72 | | - $sql = preg_replace($regex, $binding, $sql, 1); |
73 | | - } |
74 | | - |
75 | | - return [ |
76 | | - 'time' => $query->time, |
77 | | - 'sql' => $sql, |
78 | | - ]; |
79 | | - } |
80 | | - |
81 | | - /** |
82 | | - * Mimic mysql_real_escape_string |
83 | | - * |
84 | | - * @param string $value |
85 | | - * @return string |
86 | | - */ |
87 | | - protected function quote(string $value): string |
88 | | - { |
89 | | - $search = ['\\', "\x00", "\n", "\r", "'", '"', "\x1a"]; |
90 | | - $replace = ['\\\\', '\\0', '\\n', '\\r', "\'", '\"', '\\Z']; |
91 | | - |
92 | | - return "'".str_replace($search, $replace, $value)."'"; |
93 | | - } |
94 | 49 | } |
0 commit comments