1 | <?php |
||
2 | namespace JClaveau\Exceptions; |
||
3 | |||
4 | /** |
||
5 | */ |
||
6 | class UsageException extends \Exception |
||
7 | { |
||
8 | /** |
||
9 | * / |
||
10 | public function new_($message) |
||
11 | { |
||
12 | return new static($message); |
||
13 | } |
||
14 | |||
15 | /** |
||
16 | */ |
||
17 | public function __construct() |
||
18 | { |
||
19 | parent::__construct(...func_get_args()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
20 | |||
21 | $this->rewindStackWhile( function($backtrace, $level) { |
||
22 | // Finds the closest caller |
||
23 | // TODO get the class of the caller instead of comparing |
||
24 | // with UsageException |
||
25 | return isset($backtrace[ $level ]['class']) |
||
26 | && $backtrace[ $level ]['class'] == __CLASS__; |
||
27 | }, 0 ); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | */ |
||
32 | public function setStackLocationHere() |
||
33 | { |
||
34 | $this->rewindStackWhile( function($backtrace, $level) { |
||
35 | // Finds the closest caller |
||
36 | return $level < 2; |
||
37 | }, 4 ); |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | */ |
||
44 | protected function rewindStackWhile(callable $scope_checker, $stack_max_depth=20) |
||
45 | { |
||
46 | $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $stack_max_depth); |
||
47 | $i = 1; |
||
48 | $caller = $backtrace[$i]; |
||
49 | while ( $scope_checker( $backtrace, $i ) ) { |
||
50 | $i++; |
||
51 | $caller = $backtrace[$i]; |
||
52 | // TODO remove the previous levels of the stack? |
||
53 | } |
||
54 | |||
55 | // var_export($backtrace); |
||
56 | // var_export($caller); |
||
57 | |||
58 | // TODO How to handle perfectly the missing fields of the backtrace? |
||
59 | if (isset($caller['file'])) |
||
60 | $this->file = $caller['file']; |
||
61 | |||
62 | if (isset($caller['line'])) |
||
63 | $this->line = $caller['line']; |
||
64 | |||
65 | // var_export($this->stack); |
||
66 | } |
||
67 | |||
68 | /**/ |
||
69 | } |
||
70 |