2017-07-03 20:28:17 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Error;
|
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
/**
|
|
|
|
* Encapsulates warnings produced by the library.
|
|
|
|
*
|
|
|
|
* Warnings can be suppressed (individually or all) if required.
|
|
|
|
* Also it is possible to override warning handler (which is **trigger_error()** by default)
|
|
|
|
*/
|
2017-07-03 20:28:17 +03:00
|
|
|
final class Warning
|
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
const WARNING_NAME = 1;
|
|
|
|
const WARNING_ASSIGN = 2;
|
|
|
|
const WARNING_CONFIG = 4;
|
|
|
|
const WARNING_FULL_SCHEMA_SCAN = 8;
|
|
|
|
const WARNING_CONFIG_DEPRECATION = 16;
|
|
|
|
const WARNING_NOT_A_TYPE = 32;
|
2017-08-13 20:50:24 +03:00
|
|
|
const ALL = 63;
|
2017-07-04 13:10:55 +03:00
|
|
|
|
|
|
|
static $enableWarnings = self::ALL;
|
2017-07-03 20:28:17 +03:00
|
|
|
|
|
|
|
static $warned = [];
|
|
|
|
|
2017-08-12 22:50:34 +03:00
|
|
|
static private $warningHandler;
|
|
|
|
|
2017-08-16 22:15:49 +03:00
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Sets warning handler which can intercept all system warnings.
|
2017-08-16 22:15:49 +03:00
|
|
|
* When not set, trigger_error() is used to notify about warnings.
|
|
|
|
*
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-08-16 22:15:49 +03:00
|
|
|
* @param callable|null $warningHandler
|
|
|
|
*/
|
2017-08-12 22:50:34 +03:00
|
|
|
public static function setWarningHandler(callable $warningHandler = null)
|
|
|
|
{
|
|
|
|
self::$warningHandler = $warningHandler;
|
|
|
|
}
|
|
|
|
|
2017-08-16 22:15:49 +03:00
|
|
|
/**
|
|
|
|
* Suppress warning by id (has no effect when custom warning handler is set)
|
|
|
|
*
|
|
|
|
* Usage example:
|
2017-08-19 19:01:46 +03:00
|
|
|
* Warning::suppress(Warning::WARNING_NOT_A_TYPE)
|
2017-08-16 22:15:49 +03:00
|
|
|
*
|
|
|
|
* When passing true - suppresses all warnings.
|
|
|
|
*
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-08-16 22:15:49 +03:00
|
|
|
* @param bool|int $suppress
|
|
|
|
*/
|
2017-07-04 13:10:55 +03:00
|
|
|
static function suppress($suppress = true)
|
2017-07-03 20:28:17 +03:00
|
|
|
{
|
2017-07-04 13:10:55 +03:00
|
|
|
if (true === $suppress) {
|
|
|
|
self::$enableWarnings = 0;
|
|
|
|
} else if (false === $suppress) {
|
|
|
|
self::$enableWarnings = self::ALL;
|
|
|
|
} else {
|
|
|
|
$suppress = (int) $suppress;
|
|
|
|
self::$enableWarnings &= ~$suppress;
|
|
|
|
}
|
2017-07-03 20:28:17 +03:00
|
|
|
}
|
|
|
|
|
2017-08-16 22:15:49 +03:00
|
|
|
/**
|
|
|
|
* Re-enable previously suppressed warning by id
|
|
|
|
*
|
|
|
|
* Usage example:
|
2017-08-19 19:01:46 +03:00
|
|
|
* Warning::suppress(Warning::WARNING_NOT_A_TYPE)
|
2017-08-16 22:15:49 +03:00
|
|
|
*
|
|
|
|
* When passing true - re-enables all warnings.
|
|
|
|
*
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-08-16 22:15:49 +03:00
|
|
|
* @param bool|int $enable
|
|
|
|
*/
|
2017-07-10 15:38:12 +03: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-13 20:08:13 +03:00
|
|
|
static function warnOnce($errorMessage, $warningId, $messageLevel = null)
|
2017-07-03 20:28:17 +03:00
|
|
|
{
|
2017-08-12 22:50:34 +03:00
|
|
|
if (self::$warningHandler) {
|
|
|
|
$fn = self::$warningHandler;
|
|
|
|
$fn($errorMessage, $warningId);
|
|
|
|
} else if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) {
|
2017-07-04 13:10:55 +03:00
|
|
|
self::$warned[$warningId] = true;
|
2017-08-13 20:08:13 +03:00
|
|
|
trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
|
2017-07-04 13:10:55 +03:00
|
|
|
}
|
|
|
|
}
|
2017-07-03 20:28:17 +03:00
|
|
|
|
2017-08-13 20:08:13 +03:00
|
|
|
static function warn($errorMessage, $warningId, $messageLevel = null)
|
2017-07-04 13:10:55 +03:00
|
|
|
{
|
2017-08-12 22:50:34 +03:00
|
|
|
if (self::$warningHandler) {
|
|
|
|
$fn = self::$warningHandler;
|
|
|
|
$fn($errorMessage, $warningId);
|
|
|
|
} else if ((self::$enableWarnings & $warningId) > 0) {
|
2017-08-13 20:08:13 +03:00
|
|
|
trigger_error($errorMessage, $messageLevel ?: E_USER_WARNING);
|
2017-07-03 20:28:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|