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