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: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264:
<?php
namespace MvcCore\Tool;
trait Helpers {
protected static $tmpDir = NULL;
public static function GetSystemTmpDir () {
if (self::$tmpDir === NULL) {
$tmpDir = sys_get_temp_dir();
if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
$sysRoot = getenv('SystemRoot');
if (!$tmpDir || $tmpDir === $sysRoot) {
$tmpDir = !empty($_SERVER['TEMP'])
? $_SERVER['TEMP']
: (!empty($_SERVER['TMP'])
? $_SERVER['TMP']
: (!empty($_SERVER['WINDIR'])
? $_SERVER['WINDIR'] . '/Temp'
: $sysRoot . '/Temp'
)
);
}
$tmpDir = str_replace('\\', '/', $tmpDir);
} else if (!$tmpDir) {
$iniSysTempDir = @ini_get('sys_temp_dir');
$tmpDir = !empty($_SERVER['TMPDIR'])
? $_SERVER['TMPDIR']
: (!empty($_SERVER['TMP'])
? $_SERVER['TMP']
: (!empty($iniSysTempDir)
? $iniSysTempDir
: '/tmp'
)
);
}
self::$tmpDir = $tmpDir;
}
return self::$tmpDir;
}
public static function IsQueryString ($queryStr) {
$queryStr = trim($queryStr, '&=');
$apmsCount = substr_count($queryStr, '&');
$equalsCount = substr_count($queryStr, '=');
$firstAndLast = mb_substr($queryStr, 0, 1) . mb_substr($queryStr, -1, 1);
if ($firstAndLast === '{}' || $firstAndLast === '[]') return FALSE;
if ($apmsCount === 0 && $equalsCount === 0) return FALSE;
if ($equalsCount > 0) $equalsCount -= 1;
if ($equalsCount === 0) return TRUE;
return $apmsCount / $equalsCount >= 1;
}
public static function Invoke ($internalFnOrHandler, array $args, callable $onError) {
$prevErrorHandler = NULL;
$prevErrorHandler = set_error_handler(
function ($errLevel, $errMessage, $errFile, $errLine, $errContext) use ($onError, & $prevErrorHandler, $internalFnOrHandler) {
if ($errFile === '' && defined('HHVM_VERSION'))
$errFile = func_get_arg(5)[1]['file'];
if ($errFile === __FILE__) {
$funcNameStr = is_string($internalFnOrHandler)
? $internalFnOrHandler
: (is_array($internalFnOrHandler) && count($internalFnOrHandler) === 2
? $internalFnOrHandler[1]
: strval($internalFnOrHandler)
);
$errMessage = preg_replace("#^$funcNameStr\(.*?\): #", '', $errMessage);
if ($onError($errMessage, $errLevel, $errFile, $errLine, $errContext) !== FALSE)
return TRUE;
}
return $prevErrorHandler
? call_user_func_array($prevErrorHandler, func_get_args())
: FALSE;
}
);
try {
return call_user_func_array($internalFnOrHandler, $args);
} catch (\Exception $e) {
} catch (\Throwable $e) {
}
restore_error_handler();
return NULL;
}
public static function AtomicWrite (
$fullPath,
$content,
$writeMode = 'w',
$lockWaitMilliseconds = 100,
$maxLockWaitMilliseconds = 5000,
$oldLockMillisecondsTolerance = 30000
) {
$waitUTime = $lockWaitMilliseconds * 1000;
$lockHandle = NULL;
$tmpDir = self::GetSystemTmpDir();
$lockFullPath = $tmpDir . '/mvccore_lock_' . sha1($fullPath) . '.tmp';
set_error_handler(function ($level, $msg, $file, $line, $args) use (& $fullPath, & $lockFullPath, & $lockHandle) {
if ($level == E_WARNING) {
if (
mb_strpos($msg, 'fopen(' . $fullPath) === 0 ||
mb_strpos($msg, 'filemtime(' . $fullPath) === 0 ||
mb_strpos($msg, 'fopen(' . $lockFullPath) === 0 ||
mb_strpos($msg, 'filemtime(' . $lockFullPath) === 0
) {
if ($lockHandle !== NULL) {
@flock($lockHandle, LOCK_UN);
fclose($lockHandle);
unlink($lockFullPath);
}
throw new \Exception ($msg);
}
}
return FALSE;
}, E_WARNING);
clearstatcache(TRUE, $lockFullPath);
if (file_exists($lockFullPath)) {
$fileModTime = @filemtime($lockFullPath);
if ($fileModTime !== FALSE) {
if (time() > $fileModTime + $oldLockMillisecondsTolerance)
unlink($lockFullPath);
}
}
$waitingTime = 0;
while (TRUE) {
clearstatcache(TRUE, $lockFullPath);
$lockHandle = @fopen($lockFullPath, 'x');
if ($lockHandle !== FALSE) break;
$waitingTime += $lockWaitMilliseconds;
if ($waitingTime > $maxLockWaitMilliseconds) {
throw new \Exception(
'Unable to create lock handle: `' . $lockFullPath
. '` for file: `' . $fullPath
. '`. Lock creation timeout. Try to clear cache: `'
. $tmpDir . '`'
);
}
usleep($waitUTime);
}
if (!flock($lockHandle, LOCK_EX)) {
fclose($lockHandle);
unlink($lockFullPath);
throw new \Exception(
'Unable to create lock handle: `' . $lockFullPath
. '` for file: `' . $fullPath
. '`. Lock creation timeout. Try to clear cache: `'
. $tmpDir . '`'
);
}
fwrite($lockHandle, $fullPath);
fflush($lockHandle);
clearstatcache(TRUE, $fullPath);
$handle = @fopen($fullPath, $writeMode);
if ($handle && !flock($handle, LOCK_EX))
$handle = FALSE;
if (!$handle) {
flock($lockHandle, LOCK_UN);
fclose($lockHandle);
unlink($lockFullPath);
throw new \Exception(
'Unable to create locked handle for file: `' . $fullPath . '`.'
);
}
fwrite($handle, $content);
fflush($handle);
flock($handle, LOCK_UN);
flock($lockHandle, LOCK_UN);
fclose($lockHandle);
$success = unlink($lockFullPath);
restore_error_handler();
return $success;
}
public static function RealPathVirtual ($path) {
$path = str_replace('\\', '/', $path);
$rawParts = explode('/', $path);
$parts = array_filter($rawParts, 'strlen');
if ($rawParts[0] == '' && mb_substr($path, 0, 1) == '/')
array_unshift($parts, '');
$items = [];
foreach ($parts as $part) {
if ('.' == $part) continue;
if ('..' == $part) {
array_pop($items);
} else {
$items[] = $part;
}
}
return implode('/', $items);
}
}