From 58ad5c43e652fe2a6c35c1f9c9157ddbf51950f6 Mon Sep 17 00:00:00 2001 From: ghostff Date: Tue, 12 Jan 2021 05:48:03 -0600 Subject: [PATCH] add setTraceOffset --- README.md | 13 +++++++++++++ index.php | 11 +++++++++++ src/Dump.php | 13 ++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b7b535e..e1d963b 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,19 @@ Replacing predefined colors: # set($name, [$cgi_color, $cli_color]); Dump::set('boolean', ['bb02ff', 'purple']); ``` + +By default, when `Dump` is called inside a function, the call line is set to `new Dump` inside the function instead of the function +call. With `setTraceOffset` you can set the offset of each call line. +```php +function dump() +{ + Dump::setTraceOffset(2); + new Dump(...func_get_args()); # Dont use this test.php(line:4) as call line +} + +dump('foo', 22, 'bar', true); // Use test.php(line:7) instead +``` + CGI output: ![cgi screenshot](https://github.com/Ghostff/Dump7/blob/master/cgi.png) diff --git a/index.php b/index.php index 7ca2a50..9c08caf 100644 --- a/index.php +++ b/index.php @@ -26,8 +26,11 @@ class Foo extends Bar 'foo' => 'bar' ]; protected static $bool = false; + public static $foo = null; } +Foo::$foo = new Foo(); + $string = 'Foobar'; $array = ['foo', 'bar']; $int = 327626; @@ -53,3 +56,11 @@ class Foo extends Bar ], $resource); new Dump(1 == '1', 1 === '1'); + +function dump() +{ + Dump::setTraceOffset(2); + new Dump(...func_get_args()); +} + +dump('foo', 22, 'bar', true); diff --git a/src/Dump.php b/src/Dump.php index e26e0ad..d20fd7d 100644 --- a/src/Dump.php +++ b/src/Dump.php @@ -51,6 +51,8 @@ class Dump private $isPosix = false; + private static $trace_offset = 1; + private $colors = [ 'string' => ['0000FF', 'blue'], 'integer' => ['1BAABB', 'light_green'], @@ -143,6 +145,15 @@ class Dump 'negative' => 7, ]; + /** + * Set backtrace offset. + * + * @param int $offset + */ + public static function setTraceOffset(int $offset) + { + self::$trace_offset = $offset; + } /** * Dump constructor. @@ -271,7 +282,7 @@ private function output(string $data) } else { - $bt = $bt[((int) $key) + 1]; + $bt = $bt[((int) $key) + self::$trace_offset]; break; } }