1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebPConvert\Convert\Converters\BaseTraits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait for handling warnings (by logging them) |
7
|
|
|
* |
8
|
|
|
* This trait is currently only used in the AbstractConverter class. It has been extracted into a |
9
|
|
|
* trait in order to bundle the methods concerning options. |
10
|
|
|
* |
11
|
|
|
* @package WebPConvert |
12
|
|
|
* @author Bjørn Rosell <[email protected]> |
13
|
|
|
* @since Class available since Release 2.0.0 |
14
|
|
|
*/ |
15
|
|
|
trait WarningLoggerTrait |
16
|
|
|
{ |
17
|
|
|
abstract protected function logLn($msg, $style = ''); |
18
|
|
|
|
19
|
|
|
/** @var string|array|null Previous error handler (stored in order to be able pass warnings on) */ |
20
|
|
|
private $previousErrorHandler; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Handle warnings and notices during conversion by logging them and passing them on. |
24
|
|
|
* |
25
|
|
|
* The function is a callback used with "set_error_handler". |
26
|
|
|
* It is declared public because it needs to be accessible from the point where the warning happened. |
27
|
|
|
* |
28
|
|
|
* @param integer $errno |
29
|
|
|
* @param string $errstr |
30
|
|
|
* @param string $errfile |
31
|
|
|
* @param integer $errline |
32
|
|
|
* |
33
|
|
|
* @return false|null |
34
|
|
|
*/ |
35
|
1 |
|
public function warningHandler($errno, $errstr, $errfile, $errline) |
36
|
|
|
{ |
37
|
|
|
/* |
38
|
|
|
We do NOT do the following (even though it is generally recommended): |
39
|
|
|
|
40
|
|
|
if (!(error_reporting() & $errno)) { |
41
|
|
|
// This error code is not included in error_reporting, so let it fall |
42
|
|
|
// through to the standard PHP error handler |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
- Because we want to log all warnings and errors (also the ones that was suppressed with @) |
47
|
|
|
https://secure.php.net/manual/en/language.operators.errorcontrol.php |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
$errorTypes = [ |
51
|
1 |
|
E_WARNING => "Warning", |
52
|
1 |
|
E_NOTICE => "Notice", |
53
|
1 |
|
E_STRICT => "Strict Notice", |
54
|
1 |
|
E_DEPRECATED => "Deprecated", |
55
|
1 |
|
E_USER_DEPRECATED => "User Deprecated", |
56
|
|
|
|
57
|
|
|
/* |
58
|
|
|
The following can never be catched by a custom error handler: |
59
|
|
|
E_PARSE, E_ERROR, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING |
60
|
|
|
|
61
|
|
|
We do do not currently trigger the following: |
62
|
|
|
E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE |
63
|
|
|
|
64
|
|
|
But we may want to do that at some point, like this: |
65
|
|
|
trigger_error('Your version of Gd is very old', E_USER_WARNING); |
66
|
|
|
in that case, remember to add them to this array |
67
|
|
|
*/ |
68
|
|
|
]; |
69
|
|
|
|
70
|
1 |
|
if (isset($errorTypes[$errno])) { |
71
|
1 |
|
$errType = $errorTypes[$errno]; |
72
|
|
|
} else { |
73
|
|
|
$errType = "Unknown error/warning/notice ($errno)"; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$msg = $errType . ': ' . $errstr . ' in ' . $errfile . ', line ' . $errline . ', PHP ' . PHP_VERSION . |
77
|
1 |
|
' (' . PHP_OS . ')'; |
78
|
1 |
|
$this->logLn(''); |
79
|
1 |
|
$this->logLn($msg, 'italic'); |
80
|
1 |
|
$this->logLn(''); |
81
|
|
|
|
82
|
1 |
|
if (!is_null($this->previousErrorHandler)) { |
83
|
|
|
// If previousErrorHandler is this very error handler, exit to avoid recursion |
84
|
|
|
// (this could happen if ::activateWarningLogger() were called twice) |
85
|
|
|
if ( |
86
|
1 |
|
is_array($this->previousErrorHandler) && |
87
|
1 |
|
isset($this->previousErrorHandler[0]) && |
88
|
1 |
|
($this->previousErrorHandler[0] == $this) |
89
|
|
|
) { |
90
|
|
|
return false; |
91
|
|
|
} else { |
92
|
1 |
|
return call_user_func($this->previousErrorHandler, $errno, $errstr, $errfile, $errline); |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Activate warning logger. |
101
|
|
|
* |
102
|
|
|
* Sets the error handler and stores the previous so our error handler can bubble up warnings |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
12 |
|
protected function activateWarningLogger() |
107
|
|
|
{ |
108
|
12 |
|
$this->previousErrorHandler = set_error_handler( |
109
|
12 |
|
array($this, "warningHandler"), |
110
|
12 |
|
E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE |
111
|
|
|
); |
112
|
12 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Deactivate warning logger. |
116
|
|
|
* |
117
|
|
|
* Restores the previous error handler. |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
3 |
|
protected function deactivateWarningLogger() |
122
|
|
|
{ |
123
|
3 |
|
restore_error_handler(); |
124
|
3 |
|
} |
125
|
|
|
} |
126
|
|
|
|