1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198:
<?php
namespace MvcCore\Debug;
trait Initializations {
public static function Init ($forceDevelopmentMode = NULL) {
if (static::$debugging !== NULL) return;
if (static::$strictExceptionsMode === NULL)
self::initStrictExceptionsMode(static::$strictExceptionsMode);
$app = static::$app ?: (static::$app = \MvcCore\Application::GetInstance());
static::$requestBegin = $app->GetRequest()->GetStartTime();
if (gettype($forceDevelopmentMode) == 'boolean') {
static::$debugging = $forceDevelopmentMode;
} else {
$environment = $app->GetEnvironment();
static::$debugging = !$environment->IsProduction();
}
static::$originalDebugClass = ltrim($app->GetDebugClass(), '\\') == get_class();
static::initHandlers();
$initGlobalShortHandsHandler = static::$InitGlobalShortHands;
$initGlobalShortHandsHandler(static::$debugging);
}
public static function SetStrictExceptionsMode ($strictExceptionsMode, array $errorLevelsToExceptions = []) {
if ($strictExceptionsMode && !static::$strictExceptionsMode) {
$errorLevels = array_fill_keys($errorLevelsToExceptions, TRUE);
$allLevelsToExceptions = isset($errorLevels[E_ALL]);
$prevErrorHandler = NULL;
$prevErrorHandler = set_error_handler(
function(
$errLevel, $errMessage, $errFile, $errLine
) use (
& $prevErrorHandler, $errorLevels, $allLevelsToExceptions
) {
if ($errFile === '' && defined('HHVM_VERSION'))
$errFile = func_get_arg(5)[1]['file'];
if ($allLevelsToExceptions || isset($errorLevels[$errLevel]))
throw new \ErrorException($errMessage, $errLevel, $errLevel, $errFile, $errLine);
return $prevErrorHandler
? call_user_func_array($prevErrorHandler, func_get_args())
: FALSE;
}
);
self::$prevErrorHandler = & $prevErrorHandler;
} else if (!$strictExceptionsMode && static::$strictExceptionsMode) {
if (self::$prevErrorHandler !== NULL) {
set_error_handler(self::$prevErrorHandler);
} else {
restore_error_handler();
}
}
return static::$strictExceptionsMode = $strictExceptionsMode;
}
protected static function initStrictExceptionsMode ($strictExceptionsMode) {
$errorLevelsToExceptions = [];
if ($strictExceptionsMode !== FALSE) {
$sysCfgDebug = static::getSystemCfgDebugSection();
if (isset($sysCfgDebug['strictExceptions'])) {
$rawStrictExceptions = $sysCfgDebug['strictExceptions'];
if (
$rawStrictExceptions === 0 ||
$rawStrictExceptions === FALSE
) {
$strictExceptionsMode = FALSE;
} else {
$strictExceptionsMode = TRUE;
$rawStrictExceptions = is_array($rawStrictExceptions)
? $rawStrictExceptions
: explode(',', trim($rawStrictExceptions, '[]'));
$errorLevelsToExceptions = array_map(
function ($rawErrorLevel) {
$rawErrorLevel = trim($rawErrorLevel);
if (is_numeric($rawErrorLevel)) return intval($rawErrorLevel);
return constant($rawErrorLevel);
}, $rawStrictExceptions
);
}
} else {
$strictExceptionsMode = TRUE;
$errorLevelsToExceptions = static::$strictExceptionsModeDefaultLevels;
}
}
return static::SetStrictExceptionsMode($strictExceptionsMode, $errorLevelsToExceptions);
}
protected static function initHandlers () {
$className = get_called_class();
foreach (static::$handlers as $key => $value) {
static::$handlers[$key] = [$className, $value];
}
register_shutdown_function(static::$handlers['shutdownHandler']);
}
protected static function initLogDirectory () {
$sysCfgDebug = static::getSystemCfgDebugSection();
$logDirConfiguredPath = isset($sysCfgDebug['logDirectory'])
? $sysCfgDebug['logDirectory']
: static::$LogDirectory;
if (mb_substr($logDirConfiguredPath, 0, 1) === '~') {
$app = static::$app ?: (static::$app = \MvcCore\Application::GetInstance());
$logDirAbsPath = $app->GetRequest()->GetAppRoot() . '/' . ltrim(mb_substr($logDirConfiguredPath, 1), '/');
} else {
$logDirAbsPath = $logDirConfiguredPath;
}
static::$LogDirectory = $logDirAbsPath;
try {
if (!is_dir($logDirAbsPath)) {
if (!mkdir($logDirAbsPath, 0777, TRUE))
throw new \RuntimeException(
'['.get_class()."] It was not possible to create log directory: `".$logDirAbsPath."`."
);
if (!is_writable($logDirAbsPath))
if (!chmod($logDirAbsPath, 0777))
throw new \RuntimeException(
'['.get_class()."] It was not possible to setup privileges to log directory: `".$logDirAbsPath."` to writeable mode 0777."
);
}
} catch (\Exception $e) {
die('['.get_class().'] ' . $e->getMessage());
}
static::$logDirectoryInitialized = TRUE;
return $logDirAbsPath;
}
protected static function getSystemCfgDebugSection () {
if (self::$systemConfigDebugValues !== NULL) return self::$systemConfigDebugValues;
$result = [];
$app = static::$app ?: (static::$app = \MvcCore\Application::GetInstance());
$configClass = $app->GetConfigClass();
$cfg = $configClass::GetSystem();
if ($cfg === FALSE) return $result;
$cfgProps = (object) static::$systemConfigDebugProps;
if (!isset($cfg->{$cfgProps->sectionName})) return $result;
$cfgDebug = & $cfg->{$cfgProps->sectionName};
if (isset($cfgDebug->{$cfgProps->emailRecepient}))
$result['emailRecepient'] = $cfgDebug->{$cfgProps->emailRecepient};
if (isset($cfgDebug->{$cfgProps->logDirectory}))
$result['logDirectory'] = $cfgDebug->{$cfgProps->logDirectory};
if (isset($cfgDebug->{$cfgProps->strictExceptions}))
$result['strictExceptions'] = $cfgDebug->{$cfgProps->strictExceptions};
self::$systemConfigDebugValues = $result;
return $result;
}
}