2017-07-04 00:28:17 +07:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Error;
|
|
|
|
|
|
|
|
final class Warning
|
|
|
|
{
|
2017-07-04 17:10:55 +07:00
|
|
|
const NAME_WARNING = 1;
|
|
|
|
const ASSIGN_WARNING = 2;
|
|
|
|
const CONFIG_WARNING = 4;
|
2017-08-14 20:41:08 +07:00
|
|
|
const FULL_SCHEMA_SCAN_WARNING = 8;
|
2017-08-14 00:08:13 +07:00
|
|
|
const CONFIG_DEPRECATION_WARNING = 16;
|
2017-08-14 00:50:24 +07:00
|
|
|
const NOT_A_TYPE = 32;
|
2017-07-04 17:10:55 +07:00
|
|
|
|
2017-08-14 00:50:24 +07:00
|
|
|
const ALL = 63;
|
2017-07-04 17:10:55 +07:00
|
|
|
|
|
|
|
static $enableWarnings = self::ALL;
|
2017-07-04 00:28:17 +07:00
|
|
|
|
|
|
|
static $warned = [];
|
|
|
|
|
2017-08-13 02:50:34 +07:00
|
|
|
static private $warningHandler;
|
|
|
|
|
2017-08-17 02:15:49 +07:00
|
|
|
/**
|
|
|
|
* Sets warning handler which (when set) will intercept all system warnings.
|
|
|
|
* When not set, trigger_error() is used to notify about warnings.
|
|
|
|
*
|
|
|
|
* @param callable|null $warningHandler
|
|
|
|
*/
|
2017-08-13 02:50:34 +07:00
|
|
|
public static function setWarningHandler(callable $warningHandler = null)
|
|
|
|
{
|
|
|
|
self::$warningHandler = $warningHandler;
|
|
|
|
}
|
|
|
|
|
2017-08-17 02:15:49 +07:00
|
|
|
/**
|
|
|
|
* Suppress warning by id (has no effect when custom warning handler is set)
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
* Warning::suppress(Warning::NOT_A_TYPE)
|
|
|
|
*
|
|
|
|
* When passing true - suppresses all warnings.
|
|
|
|
*
|
|
|
|
* @param bool|int $suppress
|
|
|
|
*/
|
2017-07-04 17:10:55 +07:00
|
|
|
static function suppress($suppress = true)
|
2017-07-04 00:28:17 +07:00
|
|
|
{
|
2017-07-04 17:10:55 +07:00
|
|
|
if (true === $suppress) {
|
|
|
|
self::$enableWarnings = 0;
|
|
|
|
} else if (false === $suppress) {
|
|
|
|
self::$enableWarnings = self::ALL;
|
|
|
|
} else {
|
|
|
|
$suppress = (int) $suppress;
|
|
|
|
self::$enableWarnings &= ~$suppress;
|
|
|
|
}
|
2017-07-04 00:28:17 +07:00
|
|
|
}
|
|
|
|
|
2017-08-17 02:15:49 +07:00
|
|
|
/**
|
|
|
|
* Re-enable previously suppressed warning by id
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
* Warning::suppress(Warning::NOT_A_TYPE)
|
|
|
|
*
|
|
|
|
* When passing true - re-enables all warnings.
|
|
|
|
*
|
|
|
|
* @param bool|int $enable
|
|
|
|
*/
|
2017-07-10 19:38:12 +07:00
|
|
|
public static function enable($enable = true)
|
|
|
|
{
|
|
|
|
if (true === $enable) {
|
|
|
|
self::$enableWarnings = self::ALL;
|
|
|
|
} else if (false === $enable) {
|
|
|
|
self::$enableWarnings = 0;
|
|
|
|
} else {
|
|
|
|
$enable = (int) $enable;
|
|
|
|
self::$enableWarnings |= $enable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 00:08:13 +07:00
|
|
|
static function warnOnce($errorMessage, $warningId, $messageLevel = null)
|
2017-07-04 00:28:17 +07:00
|
|
|
{
|
2017-08-13 02:50:34 +07:00
|
|
|
if (self::$warningHandler) {
|
|
|
|
$fn = self::$warningHandler;
|
|
|
|
$fn($errorMessage, $warningId);
|
|
|
|
} else if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) {
|
2017-07-04 17:10:55 +07:00
|
|
|
self::$warned[$warningId] = true;
|
2017-08-14 00:08:13 +07:00
|
|
|
trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
|
2017-07-04 17:10:55 +07:00
|
|
|
}
|
|
|
|
}
|
2017-07-04 00:28:17 +07:00
|
|
|
|
2017-08-14 00:08:13 +07:00
|
|
|
static function warn($errorMessage, $warningId, $messageLevel = null)
|
2017-07-04 17:10:55 +07:00
|
|
|
{
|
2017-08-13 02:50:34 +07:00
|
|
|
if (self::$warningHandler) {
|
|
|
|
$fn = self::$warningHandler;
|
|
|
|
$fn($errorMessage, $warningId);
|
|
|
|
} else if ((self::$enableWarnings & $warningId) > 0) {
|
2017-08-14 00:08:13 +07:00
|
|
|
trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
|
2017-07-04 00:28:17 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|