Skip to content

Commit

Permalink
add TraceFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpts committed Nov 30, 2020
1 parent deb1102 commit dc70d4c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -24,3 +24,6 @@

### [RegExpFactory](https://github.com/alexpts/php-tools/blob/master/docs/regExpFactory.md)
Сервис для карирования регулярных выражений

### [TraceFormatter](https://github.com/alexpts/php-tools/blob/master/docs/traceFormatter.md)
Сервис для форматирования трейса в компактный вид для логов
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -10,7 +10,8 @@
}],
"minimum-stability": "stable",
"require": {
"php": "~8.0"
"php": "~8.0",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "~9.4"
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions docs/traceFormatter.md
@@ -0,0 +1,12 @@
### TraceFormatter

Преобразует массив трейсов в компактный формат для логов, позволяет вырезать префикс из трейсов для усменьшения размеров

```php
use PTS\Tools\TraceFormatter;

$projectDir = dirname(__DIR__, 2);
$formatter = new TraceFormatter($projectDir);
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$compactTrace = $formatter->formatTrace($trace);
```
37 changes: 37 additions & 0 deletions src/TraceFormatter.php
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace PTS\Tools;

class TraceFormatter
{
protected int $ignoreFilePrefixSize = 0;

public function __construct(string $ignorePath = '')
{
$path = rtrim($ignorePath, '/');
$this->ignoreFilePrefixSize = $path ? mb_strlen($path) + 1 : 0;
}

/**
* Преобразуем в более компактный вид трейс, для логов
*
* @param array[] $traces
*
* @return string[]
*/
public function formatTrace(array $traces): array
{
return array_map(function(array $t): string {
$file = $this->getFile($t['file'] ?? '');
return $file . ':' . $t['line'] . ' :: ' . $t['class'] . $t['type'] . $t['function'];
}, $traces);
}

protected function getFile(string $file): string
{
return $this->ignoreFilePrefixSize
? './' . mb_substr($file, $this->ignoreFilePrefixSize)
: $file;
}
}
36 changes: 36 additions & 0 deletions test/unit/TraceFormatterTest.php
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use PTS\Tools\TraceFormatter;

class TraceFormatterTest extends TestCase
{
protected TraceFormatterTest $formatter;

public function testFormatter(): void
{
$relDir = dirname(__DIR__, 2);
$formatter = new TraceFormatter($relDir);

$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$actual = $formatter->formatTrace($trace);

$expected = [
'./vendor/phpunit/phpunit/src/Framework/TestCase.php:1536 :: TraceFormatterTest->testFormatter',
'./vendor/phpunit/phpunit/src/Framework/TestCase.php:1142 :: PHPUnit\Framework\TestCase->runTest',
'./vendor/phpunit/phpunit/src/Framework/TestResult.php:730 :: PHPUnit\Framework\TestCase->runBare',
'./vendor/phpunit/phpunit/src/Framework/TestCase.php:883 :: PHPUnit\Framework\TestResult->run',
'./vendor/phpunit/phpunit/src/Framework/TestSuite.php:669 :: PHPUnit\Framework\TestCase->run',
'./vendor/phpunit/phpunit/src/Framework/TestSuite.php:669 :: PHPUnit\Framework\TestSuite->run',
'./vendor/phpunit/phpunit/src/Framework/TestSuite.php:669 :: PHPUnit\Framework\TestSuite->run',
'./vendor/phpunit/phpunit/src/TextUI/TestRunner.php:667 :: PHPUnit\Framework\TestSuite->run',
'./vendor/phpunit/phpunit/src/TextUI/Command.php:148 :: PHPUnit\TextUI\TestRunner->run',
'./vendor/phpunit/phpunit/src/TextUI/Command.php:101 :: PHPUnit\TextUI\Command->run',
'./vendor/phpunit/phpunit/phpunit:61 :: PHPUnit\TextUI\Command::main',
];

static::assertSame($expected, $actual);
}

}

0 comments on commit dc70d4c

Please sign in to comment.