Skip to content

Commit

Permalink
PHP ^7.0 compatibility implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
donbidon committed Dec 10, 2022
1 parent 0f1bec5 commit 035a942
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 194 deletions.
16 changes: 9 additions & 7 deletions README.md
Expand Up @@ -2,22 +2,25 @@
[![GitHub license](https://img.shields.io/github/license/deepeloper/pdo-debugging.svg)](https://github.com/deepeloper/pdo-debugging/blob/master/LICENSE)

## Compatibility
[![PHP ^5.6](https://img.shields.io/badge/PHP%C2%A0%C2%A0-%5E5.6-%237A86B8)]() ‾‾❞
[![1.2.0](https://img.shields.io/badge/Release-1.2.0-%233fb950)](https://github.com/deepeloper/pdo-debugging/releases/tag/1.1.0)
[![PHP ^7.0](https://img.shields.io/badge/PHP-%5E7.0-%237A86B8)]() ‾‾
[![2.0.0](https://img.shields.io/badge/Release-2.0.0-%233fb950)](https://github.com/deepeloper/pdo-debugging/releases/tag/2.0.0)

[![PHP ^5.5](https://img.shields.io/badge/PHP%C2%A0%C2%A0-%5E5.5-%237A86B8)]() ‾‾❞
[![PHP ^5.6](https://img.shields.io/badge/PHP-%5E5.6-%237A86B8)]() ‾‾
[![1.2.0](https://img.shields.io/badge/Release-1.2.0-%233fb950)](https://github.com/deepeloper/pdo-debugging/releases/tag/1.2.0)

[![PHP ^5.5](https://img.shields.io/badge/PHP-%5E5.5-%237A86B8)]() ‾‾
[![1.1.0](https://img.shields.io/badge/Release-1.1.0-%233fb950)](https://github.com/deepeloper/pdo-debugging/releases/tag/1.1.0)

[![PHP ^5.4](https://img.shields.io/badge/PHP%C2%A0%C2%A0-%5E5.4-%237A86B8)]() ‾‾❞
[![PHP ^5.4](https://img.shields.io/badge/PHP-%5E5.4-%237A86B8)]() ‾‾
[![1.0.0](https://img.shields.io/badge/Release-1.0.0-%233fb950)](https://github.com/deepeloper/pdo-debugging/releases/tag/1.0.0)

## Installation
`composer require deepeloper/pdo-debugging "^1.2"`
`composer require deepeloper/pdo-debugging "^2.0"`

or add to "composer.json" section
```json
"require": {
"deepeloper/pdo-debugging": "^1.2"
"deepeloper/pdo-debugging": "^2.0"
}
```

Expand Down Expand Up @@ -349,5 +352,4 @@ deepeloper\PDO\PDOExcavatedExtended::scope
deepeloper\PDO\PDOExcavatedExtended::scope
deepeloper\PDO\PDOExcavatedExtended::scope
deepeloper\PDO\PDOStatementExcavatedExtended::scope
```
3 changes: 2 additions & 1 deletion build/bin/phpunit.bat
@@ -1,3 +1,4 @@
@cd "../.."
@echo ------------------------------------------------------
@call php "vendor/bin/phpunit" -c "build/config/phpunit.xml" --log-junit "build/logs/junit.xml" -v --debug
@rem call php "vendor/bin/phpunit" -c "build/config/phpunit.xml" --log-junit "build/logs/junit.xml" -v --debug
@call php "vendor/bin/phpunit" -c "build/config/phpunit.xml" --log-junit "build/logs/junit.xml"
2 changes: 1 addition & 1 deletion build/config/phpunit.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.2/phpunit.xsd"
bootstrap="../../vendor/autoload.php"
colors="true"
verbose="true"
Expand Down
57 changes: 33 additions & 24 deletions src/ExcavatingTrait.php
Expand Up @@ -12,6 +12,17 @@
use DateTime;
use PDOException;

use function array_diff_key;
use function array_pop;
use function array_replace_recursive;
use function debug_backtrace;
use function json_encode;
use function microtime;
use function preg_match;
use function sizeof;
use function sprintf;
use function str_replace;

/**
* PDO debugging trait.
*
Expand All @@ -38,10 +49,10 @@ trait ExcavatingTrait
'timeStamp' => "Y-m-d H:i:s.u",
'precision' => "%.05f",
'count' => "%03d",
// @codingStandardsIgnoreStart
// phpcs:disable
'call' => "[ %TIME_STAMP% ] [ %EXECUTION_TIME% ] [ CALL ] [ %DSN%;user=%USER_NAME% ] [ %SOURCE%(%ARGS%) ]",
'query' => "[ %TIME_STAMP% ] [ %EXECUTION_TIME% ] [ QUERY ] [ %DSN%;user=%USER_NAME% ] [ #%COUNT% ] [ %SOURCE% ] [ %QUERY% ]",
// @codingStandardsIgnoreEnd
// phpcs:enable
],
'sources' => [
],
Expand Down Expand Up @@ -75,12 +86,11 @@ abstract protected function scope(array &$scope);
/**
* Prepares query for logging.
*
* @param string &$query
* @param string $query
* @return void
* @link self::after()
*/
abstract protected function prepareQueryForLogging(&$query);

abstract protected function prepareQueryForLogging(string &$query);

/**
* Initializes debugging.
Expand All @@ -91,7 +101,7 @@ abstract protected function prepareQueryForLogging(&$query);
*/
public function initDebugging(array $options = [], BenchmarksContainer $benchmarkContainer = null)
{
$this->debuggingOptions = \array_replace_recursive($this->debuggingOptions, $options);
$this->debuggingOptions = array_replace_recursive($this->debuggingOptions, $options);
$this->benchmarks = null !== $benchmarkContainer ? $benchmarkContainer : new BenchmarksContainer();
}

Expand All @@ -101,7 +111,7 @@ public function initDebugging(array $options = [], BenchmarksContainer $benchmar
* @return array
* @see PDOStatementExcavated::__construct()
*/
public function getDebuggingEnvironment()
public function getDebuggingEnvironment(): array
{
return [$this->debuggingOptions, $this->benchmarks];
}
Expand All @@ -111,7 +121,7 @@ public function getDebuggingEnvironment()
*
* @return array
*/
public function getBenchmarks()
public function getBenchmarks(): array
{
return $this->benchmarks->container;
}
Expand All @@ -129,22 +139,22 @@ protected function before(array $args = null, array $excludeArgIndexes = null)
return;
}
if (null === $args) {
$trace = \debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$source = \sprintf("%s::%s", $trace[1]['class'], $trace[1]['function']);
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$source = sprintf("%s::%s", $trace[1]['class'], $trace[1]['function']);
$args = $trace[1]['args'];
} else {
$source = $args['source'];
unset($args['source']);
}
if (null !== $excludeArgIndexes) {
$args = \array_diff_key($args, array_combine($excludeArgIndexes, $excludeArgIndexes));
$args = array_diff_key($args, array_combine($excludeArgIndexes, $excludeArgIndexes));
}
\preg_match("/\w+::.+$/", $source, $matches);
preg_match("/\w+::.+$/", $source, $matches);
$source = $matches[0];
$dateTime = new DateTime();
$call = [
'timeStamp' => $dateTime->format($this->debuggingOptions['format']['timeStamp']),
'microTime' => \microtime(true),
'microTime' => microtime(true),
'source' => $source,
];
if (empty($args['query'])) {
Expand All @@ -160,38 +170,37 @@ protected function before(array $args = null, array $excludeArgIndexes = null)
* called or about query.
*
* @return void
* @todo PHP >=7: Add comma after last element of array, no problem with code coverage.
*/
protected function after()
{
if ($this->skipLogging) {
return;
}
$call = \array_pop($this->stack);
$call = array_pop($this->stack);
$scope = [
'TIME_STAMP' => $call['timeStamp'],
'DSN' => $this->debuggingOptions['dsn'],
'USER_NAME' => $this->debuggingOptions['username'],
'EXECUTION_TIME' => \sprintf(
'EXECUTION_TIME' => sprintf(
$this->debuggingOptions['format']['precision'],
\microtime(true) - $call['microTime']
microtime(true) - $call['microTime']
),
'SOURCE' => $call['source'],
];
if (empty($call['query'])) {
$scope['ARGS'] = \json_encode($call['args'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$scope['ARGS'] = json_encode($call['args'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} else {
$this->prepareQueryForLogging($call['query']);
$scope += [
'QUERY' => $call['query'],
'COUNT' => $this->benchmarks->container['query']['count'],
];
}
$log = 0 === \sizeof($this->debuggingOptions['sources']);
$log = 0 === sizeof($this->debuggingOptions['sources']);
if (!$log) {
foreach ($this->debuggingOptions['sources'] as $pattern) {
$log = substr($pattern, 0, 1) === "/"
? \preg_match($pattern, $call['source'])
? preg_match($pattern, $call['source'])
: $pattern === $call['source'];
if ($log) {
break;
Expand All @@ -202,7 +211,7 @@ protected function after()
if ($log) {
$this->scope($scope);
if (isset($scope['COUNT'])) {
$scope['COUNT'] = \sprintf($this->debuggingOptions['format']['count'], $scope['COUNT']);
$scope['COUNT'] = sprintf($this->debuggingOptions['format']['count'], $scope['COUNT']);
}
$this->debuggingOptions['logger']->log(
$this->renderLogMessage($this->debuggingOptions['format'][$format], $scope),
Expand All @@ -219,7 +228,7 @@ protected function after()
* @param PDOException|null $e
* @return mixed
*/
protected function getResult($delay, $result, PDOException $e = null)
protected function getResult(float $delay, $result, PDOException $e = null)
{
$this->benchmarks->container['total']['time'] += $delay;
if (isset($this->statementBenchmarks)) {
Expand All @@ -240,10 +249,10 @@ protected function getResult($delay, $result, PDOException $e = null)
* @param array $scope
* @return string
*/
protected function renderLogMessage($template, array $scope)
protected function renderLogMessage(string $template, array $scope): string
{
foreach ($scope as $name => $value) {
$template = \str_replace("%$name%", $value, $template);
$template = str_replace("%$name%", $value, $template);
}
return $template;
}
Expand Down
2 changes: 1 addition & 1 deletion src/LoggerInterface.php
Expand Up @@ -22,5 +22,5 @@ interface LoggerInterface
* @return void
* @link ExcavatingTrait::after()
*/
public function log($message, array $scope);
public function log(string $message, array $scope);
}

0 comments on commit 035a942

Please sign in to comment.