1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Accessories; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Accessories\Caller\CallerData; |
8
|
|
|
use AlecRabbit\Accessories\Caller\CallerDataFormatter; |
9
|
|
|
use AlecRabbit\Accessories\Caller\Contracts\CallerConstants; |
10
|
|
|
use AlecRabbit\Reports\Contracts\ReportInterface; |
11
|
|
|
use AlecRabbit\Reports\Core\Reportable; |
12
|
|
|
|
13
|
|
|
class Caller extends Reportable implements CallerConstants |
14
|
|
|
{ |
15
|
|
|
/** @var null|CallerDataFormatter */ |
16
|
|
|
protected static $formatter; |
17
|
|
|
|
18
|
|
|
/** @var int */ |
19
|
|
|
protected static $limit = 0; |
20
|
|
|
|
21
|
|
|
/** @var int */ |
22
|
|
|
protected static $options = DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return CallerDataFormatter |
26
|
|
|
*/ |
27
|
6 |
|
public static function getFormatter(): CallerDataFormatter |
28
|
|
|
{ |
29
|
6 |
|
if (null === static::$formatter) { |
30
|
1 |
|
static::$formatter = new CallerDataFormatter(); |
31
|
|
|
} |
32
|
6 |
|
return static::$formatter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param CallerDataFormatter $formatter |
37
|
|
|
*/ |
38
|
1 |
|
public static function setFormatter(CallerDataFormatter $formatter): void |
39
|
|
|
{ |
40
|
1 |
|
self::$formatter = $formatter; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
1 |
|
protected function createEmptyReport(): ReportInterface |
44
|
|
|
{ |
45
|
1 |
|
return static::get(3); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param null|int $depth |
50
|
|
|
* @return CallerData |
51
|
|
|
*/ |
52
|
5 |
|
public static function get(?int $depth = null): CallerData |
53
|
|
|
{ |
54
|
|
|
return |
55
|
5 |
|
new CallerData( |
56
|
5 |
|
self::getCallerData($depth ?? 2) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $depth |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
5 |
|
protected static function getCallerData(int $depth): array |
65
|
|
|
{ |
66
|
|
|
return |
67
|
5 |
|
debug_backtrace(static::getOptions(), static::getLimit())[++$depth] ?? self::UNDEFINED; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
6 |
|
public static function getOptions(): int |
74
|
|
|
{ |
75
|
6 |
|
return self::$options; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $options |
80
|
|
|
*/ |
81
|
1 |
|
public static function setOptions(int $options): void |
82
|
|
|
{ |
83
|
1 |
|
self::$options = $options; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
6 |
|
public static function getLimit(): int |
90
|
|
|
{ |
91
|
6 |
|
return self::$limit; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param int $limit |
96
|
|
|
*/ |
97
|
1 |
|
public static function setLimit(int $limit): void |
98
|
|
|
{ |
99
|
1 |
|
self::$limit = $limit; |
100
|
1 |
|
} |
101
|
|
|
} |
102
|
|
|
|