1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Carpediem.Errors library |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
* @link https://github.com/carpediem/errors/ |
7
|
|
|
* @version 0.2.0 |
8
|
|
|
* @package Carpediem.errors |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
namespace Carpediem\Errors; |
14
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A class to convert Error into Exception |
19
|
|
|
* |
20
|
|
|
* <?php |
21
|
|
|
* |
22
|
|
|
* use Carpediem\Errors\CaptureError; |
23
|
|
|
* use Carpediem\Errors\ErrorToException; |
24
|
|
|
* |
25
|
|
|
* $touch = new ErrorToException(new CaptureError('touch', E_WARNING), 'RuntimeException'); |
26
|
|
|
* try { |
27
|
|
|
* $touch('/usr/bin/foo.bar'); |
28
|
|
|
* } catch (RuntimeException $e) { |
29
|
|
|
* echo $e->getMessage(); |
30
|
|
|
* } |
31
|
|
|
*/ |
32
|
|
|
class ErrorToException |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var CaptureErrorInterface |
36
|
|
|
*/ |
37
|
|
|
protected $callable; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Exception class to instantiate |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $exception_class_name; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A new instance |
48
|
|
|
* |
49
|
|
|
* @param CaptureErrorInterface $callable |
50
|
|
|
* @param string $exception_class_name The exception to be thrown |
51
|
|
|
*/ |
52
|
|
|
public function __construct(CaptureErrorInterface $callable, $exception_class_name = 'RuntimeException') |
53
|
|
|
{ |
54
|
|
|
$this->callable = $callable; |
55
|
|
|
$this->setExceptionClassName($exception_class_name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the Exception class |
60
|
|
|
* |
61
|
|
|
* @param string $className error reporting level |
62
|
|
|
* |
63
|
|
|
* @throws InvalidArgumentException If the class is not a throwable object |
64
|
|
|
*/ |
65
|
|
|
public function setExceptionClassName($className) |
66
|
|
|
{ |
67
|
|
|
if (!$this->isThrowable($className) && !$this->isException($className)) { |
68
|
|
|
throw new InvalidArgumentException(sprintf( |
69
|
|
|
'The class name provided `%s` is neither a Throwable nor an Exception object', |
70
|
|
|
$className |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->exception_class_name = $className; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Is the ClassName a Throwable Object (PHP7+) |
79
|
|
|
* |
80
|
|
|
* @param string $className |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function isThrowable($className) |
85
|
|
|
{ |
86
|
|
|
return interface_exists('Throwable') && is_subclass_of($className, 'Throwable'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Is the ClassName an Exception Object (PHP5+) |
91
|
|
|
* |
92
|
|
|
* @param string $className |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
protected function isException($className) |
97
|
|
|
{ |
98
|
|
|
return $className === 'Exception' || is_subclass_of($className, 'Exception'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the current Error Reporting Level |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getExceptionClassName() |
107
|
|
|
{ |
108
|
|
|
return $this->exception_class_name; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Process the callable |
113
|
|
|
* |
114
|
|
|
* @param mixed ...$args the arguments associated with the callable |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function __invoke() |
119
|
|
|
{ |
120
|
|
|
$result = call_user_func_array($this->callable, func_get_args()); |
121
|
|
|
$errorMessage = $this->callable->getLastErrorMessage(); |
122
|
|
|
if (!empty($errorMessage)) { |
123
|
|
|
throw new $this->exception_class_name( |
124
|
|
|
$errorMessage, |
125
|
|
|
$this->callable->getLastErrorCode() |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|