Skip to content

Commit

Permalink
add setTraceOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghostff committed Jan 12, 2021
1 parent 36d600a commit 58ad5c4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
13 changes: 12 additions & 1 deletion src/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Dump

private $isPosix = false;

private static $trace_offset = 1;

private $colors = [
'string' => ['0000FF', 'blue'],
'integer' => ['1BAABB', 'light_green'],
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -271,7 +282,7 @@ private function output(string $data)
}
else
{
$bt = $bt[((int) $key) + 1];
$bt = $bt[((int) $key) + self::$trace_offset];
break;
}
}
Expand Down

0 comments on commit 58ad5c4

Please sign in to comment.