1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Peridot\Leo\Responder\Exception; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Thrown by ExceptionResponder when an assertion fails. |
10
|
|
|
* |
11
|
|
|
* @package Peridot\Leo\Responder\Exception |
12
|
|
|
*/ |
13
|
|
|
final class AssertionException extends Exception |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Trim the supplied exception's stack trace to only include relevant |
17
|
|
|
* information. |
18
|
|
|
* |
19
|
|
|
* Also replaces the file path and line number. |
20
|
|
|
* |
21
|
|
|
* @param Exception $exception The exception. |
22
|
|
|
*/ |
23
|
|
|
public static function trim(Exception $exception) |
24
|
|
|
{ |
25
|
|
|
$reflector = new ReflectionClass('Exception'); |
26
|
|
|
|
27
|
|
|
$traceProperty = $reflector->getProperty('trace'); |
28
|
|
|
$traceProperty->setAccessible(true); |
29
|
|
|
$fileProperty = $reflector->getProperty('file'); |
30
|
|
|
$fileProperty->setAccessible(true); |
31
|
|
|
$lineProperty = $reflector->getProperty('line'); |
32
|
|
|
$lineProperty->setAccessible(true); |
33
|
|
|
|
34
|
|
|
$call = static::traceLeoCall($traceProperty->getValue($exception)); |
35
|
|
|
|
36
|
|
|
if ($call) { |
37
|
|
|
list($file, $line) = self::traceCallPosition($call); |
38
|
|
|
|
39
|
|
|
$traceProperty->setValue($exception, array($call)); |
40
|
|
|
$fileProperty->setValue($exception, $file); |
41
|
|
|
$lineProperty->setValue($exception, $line); |
42
|
|
|
} else { |
43
|
|
|
$traceProperty->setValue($exception, array()); |
44
|
|
|
$fileProperty->setValue($exception, null); |
45
|
|
|
$lineProperty->setValue($exception, null); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Find the Leo entry point call in a stack trace. |
51
|
|
|
* |
52
|
|
|
* @param array $trace The stack trace. |
53
|
|
|
* |
54
|
|
|
* @return array|null The call, or null if unable to determine the entry point. |
55
|
|
|
*/ |
56
|
|
|
public static function traceLeoCall(array $trace) |
57
|
|
|
{ |
58
|
|
|
for ($i = count($trace) - 1; $i >= 0; --$i) { |
59
|
|
|
if (self::isLeoTraceEntry($trace[$i])) { |
60
|
|
|
return $trace[$i]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Construct a new assertion exception. |
69
|
|
|
* |
70
|
|
|
* @param string $message The message. |
71
|
|
|
*/ |
72
|
|
|
public function __construct($message) |
73
|
|
|
{ |
74
|
|
|
parent::__construct($message); |
75
|
|
|
|
76
|
|
|
static::trim($this); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private static function isLeoTraceEntry($entry) |
80
|
|
|
{ |
81
|
|
|
$prefix = 'Peridot\\Leo\\'; |
82
|
|
|
|
83
|
|
|
if (isset($entry['class'])) { |
84
|
|
|
return 0 === strpos($entry['class'], $prefix); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return 0 === strpos($entry['function'], $prefix); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private static function traceCallPosition($call) |
91
|
|
|
{ |
92
|
|
|
return array( |
93
|
|
|
isset($call['file']) ? $call['file'] : null, |
94
|
|
|
isset($call['line']) ? $call['line'] : null, |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|