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