|
1 | 1 | <?php |
2 | 2 |
|
3 | | -/* |
4 | | - * The regular expression used in the `prepareQuery()` method and |
5 | | - * the quote emulation used in the `quote()` method where extracted |
6 | | - * from the "barryvdh/laravel-debugbar" package available at: |
7 | | - * |
8 | | - * https://github.com/barryvdh/laravel-debugbar/tree/6420113d90bb746423fa70b9940e9e7c26ebc121 |
9 | | - * |
10 | | - * "barryvdh/laravel-debugbar" is licensed under MIT. License is available at: |
11 | | - * |
12 | | - * https://github.com/barryvdh/laravel-debugbar/blob/6420113d90bb746423fa70b9940e9e7c26ebc121/LICENSE |
13 | | - */ |
14 | | - |
15 | 3 | namespace RodrigoPedra\QueryLogger; |
16 | 4 |
|
17 | | -use Illuminate\Contracts\Config\Repository; |
| 5 | +use Illuminate\Database\ConnectionResolverInterface; |
18 | 6 | use Illuminate\Database\Events\QueryExecuted; |
19 | 7 | use Illuminate\Support\Arr; |
20 | 8 | use Psr\Log\LoggerInterface; |
|
23 | 11 | { |
24 | 12 | public function __construct( |
25 | 13 | private LoggerInterface $logger, |
26 | | - private Repository $config, |
| 14 | + private ConnectionResolverInterface $db, |
27 | 15 | ) {} |
28 | 16 |
|
29 | 17 | public function handle(QueryExecuted $event): void |
30 | 18 | { |
31 | | - $pdo = \method_exists($event->connection, 'getPdo') |
32 | | - ? $event->connection->getPdo() |
33 | | - : null; |
34 | | - |
35 | | - $dateFormat = $event->connection->getQueryGrammar()->getDateFormat(); |
36 | | - |
37 | | - $bindings = $event->connection->prepareBindings($event->bindings); |
38 | | - $bindings = \array_map(fn ($value) => $this->prepareValue($pdo, $dateFormat, $value), $bindings); |
39 | | - |
40 | | - $query = $this->prepareQuery($event->sql, $bindings); |
41 | | - |
42 | | - $this->logger->info($query, [ |
| 19 | + $this->logger->debug($event->toRawSql(), [ |
43 | 20 | 'time' => $event->time, |
44 | 21 | 'connection' => $event->connectionName, |
45 | | - 'database' => $this->config->get("database.connections.{$event->connectionName}.database"), |
| 22 | + 'database' => $this->db->connection($event->connectionName)->getDatabaseName(), |
46 | 23 | 'bindings' => $event->bindings, |
47 | 24 | 'callSpot' => $this->guessCallSpot(), |
48 | 25 | ]); |
49 | 26 | } |
50 | 27 |
|
51 | | - protected function prepareQuery(string $query, array $bindings): string |
52 | | - { |
53 | | - foreach ($bindings as $key => $value) { |
54 | | - $regex = \is_numeric($key) |
55 | | - ? "/(?<!\?)\?(?=(?:[^'\\\']*'[^'\\']*')*[^'\\\']*$)(?!\?)/" |
56 | | - : "/:$key(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/"; |
57 | | - |
58 | | - $query = \preg_replace($regex, $value, $query, 1); |
59 | | - } |
60 | | - |
61 | | - return $query; |
62 | | - } |
63 | | - |
64 | | - protected function prepareValue(?\PDO $pdo, string $dateFormat, $value): string |
65 | | - { |
66 | | - if (\is_null($value)) { |
67 | | - return 'NULL'; |
68 | | - } |
69 | | - |
70 | | - if (\is_bool($value)) { |
71 | | - return $value ? '1' : '0'; |
72 | | - } |
73 | | - |
74 | | - if (\is_int($value) || \is_float($value)) { |
75 | | - return \strval($value); |
76 | | - } |
77 | | - |
78 | | - if (\is_string($value) && ! \mb_check_encoding($value, 'UTF-8')) { |
79 | | - return $this->quote($pdo, '[BINARY DATA]'); |
80 | | - } |
81 | | - |
82 | | - if ($value instanceof \DateTimeInterface) { |
83 | | - $value = $value->format($dateFormat); |
84 | | - } |
85 | | - |
86 | | - if ($value instanceof \Stringable) { |
87 | | - $value = \strval($value); |
88 | | - } |
89 | | - |
90 | | - if (\is_object($value) && \method_exists($value, 'toString')) { |
91 | | - $value = $value->toString(); |
92 | | - } |
93 | | - |
94 | | - // objects not implementing __toString() or toString() will fail here |
95 | | - return $this->quote($pdo, \strval($value)); |
96 | | - } |
97 | | - |
98 | | - protected function quote(?\PDO $pdo, string $value): string |
99 | | - { |
100 | | - if ($pdo) { |
101 | | - return $pdo->quote($value); |
102 | | - } |
103 | | - |
104 | | - $search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"]; |
105 | | - $replace = ["\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z"]; |
106 | | - |
107 | | - return "'" . \str_replace($search, $replace, $value) . "'"; |
108 | | - } |
109 | | - |
110 | | - protected function guessCallSpot(): array |
| 28 | + private function guessCallSpot(): array |
111 | 29 | { |
112 | 30 | $stack = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS); |
113 | 31 | $vendor = \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR; |
|
0 commit comments