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:
<?php
namespace MvcCore\Ext\Debugs\Tracys;
class SessionPanel implements \Tracy\IBarPanel {
const VERSION = '5.0.0';
const _TYPE_PHP = 0;
const _TYPE_NAMESPACE = 1;
const _EXPIRATION_HOOPS = 1;
const _EXPIRATION_TIME = 2;
public static $MetaStoreKey = \MvcCore\ISession::SESSION_METADATA_KEY;
protected $session = [];
protected $sessionMaxLifeTime = '';
public function getId() {
return 'session-panel';
}
public function getTab() {
ob_start();
include(__DIR__ . '/session.tab.phtml');
return ob_get_clean();
}
public function getPanel() {
$this->prepareSessionData();
if (!$this->session) return '';
ob_start();
include(__DIR__ . '/session.panel.phtml');
return ob_get_clean();
}
protected function prepareSessionData () {
if ($_SESSION === NULL) return;
$sessionClass = \MvcCore\Application::GetInstance()->GetSessionClass();
$now = $sessionClass::GetSessionStartTime();
$sessionClass = \MvcCore\Application::GetInstance()->GetSessionClass();
$sessionRawMetaStore = $sessionClass::GetSessionMetadata();
$sessionMetaStore = $sessionRawMetaStore instanceof \stdClass
? $sessionRawMetaStore
: (object) ['names' => []];
$maxLifeTimes = (object) [
'hoops' => 0,
'seconds' => 0,
];
$standardRecords = [];
$namespaceRecords = [];
foreach ($_SESSION as $sessionKey => $sessionData) {
if ($sessionKey === self::$MetaStoreKey) continue;
$item = new \stdClass;
$item->key = $sessionKey;
$item->value = \Tracy\Dumper::toHtml($sessionData);
if (isset($sessionMetaStore->names[$sessionKey])) {
if (count((array) $_SESSION[$sessionKey]) === 0)
continue;
$item->type = self::_TYPE_NAMESPACE;
$item->expirations = [];
if (isset($sessionMetaStore->hoops[$sessionKey])) {
$value = $sessionMetaStore->hoops[$sessionKey];
$item->expirations[] = (object) [
'type' => self::_EXPIRATION_HOOPS,
'value' => $value,
'text' => $value . ' hoops',
];
if ($value > $maxLifeTimes->hoops)
$maxLifeTimes->hoops = $value;
}
if (isset($sessionMetaStore->expirations[$sessionKey])) {
$value = $sessionMetaStore->expirations[$sessionKey] - $now;
$item->expirations[] = (object) [
'type' => self::_EXPIRATION_TIME,
'value' => $value,
'text' => $this->_formateMaxLifeTimestamp($value),
];
if ($value > $maxLifeTimes->seconds)
$maxLifeTimes->seconds = $value;
}
$namespaceRecords[$sessionKey] = $item;
} else {
$item->type = self::_TYPE_PHP;
$standardRecords[$sessionKey] = $item;
}
}
ksort($standardRecords);
ksort($namespaceRecords);
$this->session = array_merge($namespaceRecords, $standardRecords);
$maxLifeTimesItems = [];
if ($maxLifeTimes->seconds > 0)
$maxLifeTimesItems[] = $this->_formateMaxLifeTimestamp($maxLifeTimes->seconds);
if ($maxLifeTimes->hoops > 0)
$maxLifeTimesItems[] = $maxLifeTimes->hoops . ' hoops';
$this->sessionMaxLifeTime = implode(', ', $maxLifeTimesItems);
}
private function _formateMaxLifeTimestamp ($timestamp = 0) {
$result = [];
if ($timestamp >= 31557600) {
$localVal = floor($timestamp / 31557600);
$result[] = $localVal . ' year' . (($localVal > 1) ? 's' : '');
if ($localVal > 1) return 'more than ' . $result[0];
$timestamp = $timestamp - (floor($timestamp / 31557600) * 31557600);
}
if ($timestamp >= 2592000) {
$localVal = floor($timestamp / 2592000);
$result[] = $localVal . ' month' . (($localVal > 1) ? 's' : '');
if (count($result) == 1 && $localVal > 1) return 'more than ' . $result[0];
$timestamp = $timestamp - (floor($timestamp / 2592000) * 2592000);
}
if ($timestamp >= 86400) {
$localVal = floor($timestamp / 86400);
$result[] = $localVal . ' day' . (($localVal > 1) ? 's' : '');
if (count($result) == 1 && $localVal > 1) return 'more than ' . $result[0];
$timestamp = $timestamp - (floor($timestamp / 86400) * 86400);
}
if ($timestamp >= 3600) {
$localVal = floor($timestamp / 3600);
$result[] = $localVal . ' hour' . (($localVal > 1) ? 's' : '');
$timestamp = $timestamp - (floor($timestamp / 3600) * 3600);
}
if ($timestamp >= 60) {
$localVal = floor($timestamp / 60);
$result[] = $localVal . ' minute' . (($localVal > 1) ? 's' : '');
$timestamp = $timestamp - (floor($timestamp / 60) * 60);
}
if ($timestamp > 0) {
$localVal = floor($timestamp);
if ($localVal > 1) $result[] = $localVal . ' seconds';
}
return implode(', ', $result);
}
}