Skip to content

Commit 7fc0471

Browse files
author
Karel Wintersky
committed
1.0.1
- PHP8 compatible code - unify loggers: info for slow requests, error for errors, emergency for fatal errors
1 parent 97ce39c commit 7fc0471

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

sources/Database/DBWrapper.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
namespace Arris\Database;
44

5+
use ArrayAccess;
56
use PDO;
67
use Psr\Log\LoggerInterface;
78
use Psr\Log\NullLogger;
89

910
/**
1011
* @method int|false exec(string $statement = '')
11-
*
12-
* PDOStatement|false _prepare($query = '', array $options = [])
13-
* PDOStatement|false _query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, ...$fetch_mode_args)
14-
*
1512
* @method bool beginTransaction()
1613
* @method bool commit()
1714
* @method bool rollback()
@@ -24,6 +21,9 @@
2421
*
2522
* @method string errorCode()
2623
* @method array errorInfo()
24+
*
25+
* PDOStatement|false _prepare($query = '', array $options = [])
26+
* PDOStatement|false _query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, ...$fetch_mode_args)
2727
*/
2828
class DBWrapper
2929
{
@@ -144,7 +144,16 @@ public function __call($function, $args)
144144
$this->config->total_queries++;
145145

146146
if ($this->last_state['time'] >= $this->config->slow_query_threshold && $this->config->slow_query_threshold > 0) {
147-
$this->logger->debug($function);
147+
$debug = \debug_backtrace();
148+
$debug = $debug[1] ?? $debug[0];
149+
$caller = \sprintf("%s%s%s", ($debug['class'] ?? ''), ($debug['type'] ?? ''), ($debug['function'] ?? ''));
150+
151+
$this->logger->info("PDO::{$function}() slow: ", [
152+
$this->last_state['time'],
153+
$caller,
154+
((PHP_SAPI === "cli") ? __FILE__ : ($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])),
155+
$args
156+
]);
148157
}
149158

150159
return $result;
@@ -221,24 +230,28 @@ public function getLastState():array
221230

222231
/**
223232
* @param int $precision
224-
* @return array{total_queries:int,total_time:string}
233+
* @return array{total_queries: int, total_time: string}
225234
*/
226235
public function getStats(int $precision = 6)
227236
{
228-
return [
229-
'total_queries' => $this->config->total_queries,
230-
'total_time' => $this->config->formatTime($this->config->total_time, $precision)
231-
];
232-
233-
/*$object = (new class() extends stdClass {
237+
/*$object = (new class() implements ArrayAccess {
234238
public int $total_queries;
235239
public string $total_time;
240+
241+
#[ReturnTypeWillChange] public function offsetExists($offset): bool { return isset($this->$offset); }
242+
#[ReturnTypeWillChange] public function offsetGet($offset) { return $this->$offset ?? null; }
243+
#[ReturnTypeWillChange] public function offsetSet($offset, $value):void { $this->$offset = $value; }
244+
#[ReturnTypeWillChange] public function offsetUnset($offset):void { unset($this->$offset);}
236245
});
237246
$object->total_queries = $this->config->total_queries;
238247
$object->total_time = $this->config->formatTime($this->config->total_time, $precision);
239248
240-
return $object;
241-
*/
249+
return $object;*/
250+
251+
return [
252+
'total_queries' => $this->config->total_queries,
253+
'total_time' => $this->config->formatTime($this->config->total_time, $precision)
254+
];
242255
}
243256

244257
private function updateLastState($args)

0 commit comments

Comments
 (0)