1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Error; |
6
|
|
|
|
7
|
|
|
use function trigger_error; |
8
|
|
|
use const E_USER_WARNING; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Encapsulates warnings produced by the library. |
12
|
|
|
* |
13
|
|
|
* Warnings can be suppressed (individually or all) if required. |
14
|
|
|
* Also it is possible to override warning handler (which is **trigger_error()** by default) |
15
|
|
|
*/ |
16
|
|
|
final class Warning |
17
|
|
|
{ |
18
|
|
|
const WARNING_ASSIGN = 2; |
19
|
|
|
const WARNING_CONFIG = 4; |
20
|
|
|
const WARNING_FULL_SCHEMA_SCAN = 8; |
21
|
|
|
const WARNING_CONFIG_DEPRECATION = 16; |
22
|
|
|
const WARNING_NOT_A_TYPE = 32; |
23
|
|
|
const ALL = 63; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
private static $enableWarnings = self::ALL; |
27
|
|
|
|
28
|
|
|
/** @var mixed[] */ |
29
|
|
|
private static $warned = []; |
30
|
|
|
|
31
|
|
|
/** @var callable|null */ |
32
|
|
|
private static $warningHandler; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets warning handler which can intercept all system warnings. |
36
|
|
|
* When not set, trigger_error() is used to notify about warnings. |
37
|
|
|
* |
38
|
|
|
* @api |
39
|
|
|
*/ |
40
|
|
|
public static function setWarningHandler(?callable $warningHandler = null) |
41
|
|
|
{ |
42
|
|
|
self::$warningHandler = $warningHandler; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Suppress warning by id (has no effect when custom warning handler is set) |
47
|
|
|
* |
48
|
|
|
* Usage example: |
49
|
|
|
* Warning::suppress(Warning::WARNING_NOT_A_TYPE) |
50
|
|
|
* |
51
|
|
|
* When passing true - suppresses all warnings. |
52
|
|
|
* |
53
|
|
|
* @param bool|int $suppress |
54
|
|
|
* |
55
|
|
|
* @api |
56
|
|
|
*/ |
57
|
73 |
|
public static function suppress($suppress = true) |
58
|
|
|
{ |
59
|
73 |
|
if ($suppress === true) { |
60
|
|
|
self::$enableWarnings = 0; |
61
|
73 |
|
} elseif ($suppress === false) { |
62
|
|
|
self::$enableWarnings = self::ALL; |
63
|
|
|
} else { |
64
|
73 |
|
self::$enableWarnings &= ~$suppress; |
65
|
|
|
} |
66
|
73 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Re-enable previously suppressed warning by id |
70
|
|
|
* |
71
|
|
|
* Usage example: |
72
|
|
|
* Warning::suppress(Warning::WARNING_NOT_A_TYPE) |
73
|
|
|
* |
74
|
|
|
* When passing true - re-enables all warnings. |
75
|
|
|
* |
76
|
|
|
* @param bool|int $enable |
77
|
|
|
* |
78
|
|
|
* @api |
79
|
|
|
*/ |
80
|
73 |
|
public static function enable($enable = true) |
81
|
|
|
{ |
82
|
73 |
|
if ($enable === true) { |
83
|
|
|
self::$enableWarnings = self::ALL; |
84
|
73 |
|
} elseif ($enable === false) { |
85
|
|
|
self::$enableWarnings = 0; |
86
|
|
|
} else { |
87
|
73 |
|
self::$enableWarnings |= $enable; |
88
|
|
|
} |
89
|
73 |
|
} |
90
|
|
|
|
91
|
1 |
|
public static function warnOnce($errorMessage, $warningId, $messageLevel = null) |
92
|
|
|
{ |
93
|
1 |
|
if (self::$warningHandler) { |
94
|
|
|
$fn = self::$warningHandler; |
95
|
|
|
$fn($errorMessage, $warningId); |
96
|
1 |
|
} elseif ((self::$enableWarnings & $warningId) > 0 && ! isset(self::$warned[$warningId])) { |
97
|
1 |
|
self::$warned[$warningId] = true; |
98
|
1 |
|
trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING); |
99
|
|
|
} |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
public static function warn($errorMessage, $warningId, $messageLevel = null) |
103
|
|
|
{ |
104
|
|
|
if (self::$warningHandler) { |
105
|
|
|
$fn = self::$warningHandler; |
106
|
|
|
$fn($errorMessage, $warningId); |
107
|
|
|
} elseif ((self::$enableWarnings & $warningId) > 0) { |
108
|
|
|
trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|