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:
<?php
namespace MvcCore\Ext\Debug\Tracy;
class SessionPanel implements \Tracy\IBarPanel
{
const VERSION = '4.2.0';
const TYPE_PHP = 0;
const TYPE_NAMESPACE = 1;
const EXPIRATION_HOOPS = 1;
const EXPIRATION_TIME = 2;
public static $MetaStoreKey = \MvcCore\Session::SESSION_METADATA_KEY;
public static $Session = array();
public static $SessionMaxLifeTime = '';
public static $Id = 'session-panel';
public static $Now = 0;
public function __construct () {
self::$Now = time();
}
public function getId() {
return self::$Id;
}
public function getTab() {
ob_start();
require(__DIR__ . '/assets/Bar/session.tab.phtml');
return ob_get_clean();
}
public function getPanel() {
$this->readSession();
if (!self::$Session) return '';
ob_start();
require(__DIR__ . '/assets/Bar/session.panel.phtml');
return ob_get_clean();
}
protected function readSession () {
if (is_null($_SESSION)) return;
$sessionRawMetaStore = isset($_SESSION[self::$MetaStoreKey]) ? $_SESSION[self::$MetaStoreKey] : '';
$sessionMetaStore = $sessionRawMetaStore ? unserialize($sessionRawMetaStore) : (object) array('names' => array());
$maxLifeTimes = (object) array(
'hoops' => 0,
'seconds' => 0,
);
$standardRecords = array();
$namespaceRecords = array();
foreach ($_SESSION as $sessionKey => $sessionData) {
if ($sessionKey === self::$MetaStoreKey) continue;
$item = new \stdClass;
$item->key = $sessionKey;
$item->value = $this->_clickableDump($sessionData);
if (isset($sessionMetaStore->names[$sessionKey])) {
$item->type = self::TYPE_NAMESPACE;
$item->expirations = array();
if (isset($sessionMetaStore->hoops[$sessionKey])) {
$value = $sessionMetaStore->hoops[$sessionKey];
$item->expirations[] = (object) array(
'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] - self::$Now;
$item->expirations[] = (object) array(
'type' => self::EXPIRATION_TIME,
'value' => $value,
'text' => $this->_formateDate($value),
);
if ($value > $maxLifeTimes->seconds) $maxLifeTimes->seconds = $value;
}
$namespaceRecords[$sessionKey] = $item;
} else {
$item->type = self::TYPE_PHP;
$standardRecords[$sessionKey] = $item;
}
}
ksort($standardRecords);
ksort($namespaceRecords);
self::$Session = array_merge($namespaceRecords, $standardRecords);
$maxLifeTimesItems = array();
if ($maxLifeTimes->seconds > 0) $maxLifeTimesItems[] = $this->_formateDate($maxLifeTimes->seconds);
if ($maxLifeTimes->hoops > 0) $maxLifeTimesItems[] = $maxLifeTimes->hoops . ' hoops';
self::$SessionMaxLifeTime = implode(', ', $maxLifeTimesItems);
}
private function _formateDate ($timestamp = 0) {
$result = array();
if ($timestamp >= 31557600) {
$localVal = floor($timestamp / 31557600);
$result[] = $localVal . ' year' . (($localVal > 1) ? 's' : '');
$timestamp = $timestamp - (floor($timestamp / 31557600) * 31557600);
}
if ($timestamp >= 2592000) {
$localVal = floor($timestamp / 2592000);
$result[] = $localVal . ' month' . (($localVal > 1) ? 's' : '');
$timestamp = $timestamp - (floor($timestamp / 2592000) * 2592000);
}
if ($timestamp >= 86400) {
$localVal = floor($timestamp / 86400);
$result[] = $localVal . ' day' . (($localVal > 1) ? 's' : '');
$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);
}
private function _clickableDump ($dump) {
return '<pre class="nette-dump">' . preg_replace_callback(
'#^( *)((?>[^(]{1,200}))\((\d+)\) <code>#m',
function ($m) {
return "$m[1]<a href='#' rel='next'>$m[2]($m[3]) " . (
trim($m[1]) || $m[3] < 7
?
'<abbr>▼</abbr> </a><code>'
:
'<abbr>►</abbr> </a><code class="nette-collapsed">'
);
},
\Tracy\Dumper::toHtml($dump)
) . '</pre>';
}
}