Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |