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