Overview

Namespaces

  • MvcCore
    • Ext
      • Auth
        • Virtual
      • Debug
        • Tracy
      • Form
        • Core
        • Validators
      • Request
      • Router
        • Lang
      • View
        • Helpers
  • None

Classes

  • MvcCore
  • MvcCore\Config
  • MvcCore\Controller
  • MvcCore\Debug
  • MvcCore\Ext\Auth
  • MvcCore\Ext\Auth\Controller
  • MvcCore\Ext\Auth\SignInForm
  • MvcCore\Ext\Auth\SignOutForm
  • MvcCore\Ext\Auth\User
  • MvcCore\Ext\Auth\Virtual\Controller
  • MvcCore\Ext\Auth\Virtual\Form
  • MvcCore\Ext\Auth\Virtual\User
  • MvcCore\Ext\Debug\Tracy
  • MvcCore\Ext\Debug\Tracy\AuthPanel
  • MvcCore\Ext\Debug\Tracy\IncludePanel
  • MvcCore\Ext\Debug\Tracy\MvcCorePanel
  • MvcCore\Ext\Debug\Tracy\RoutingPanel
  • MvcCore\Ext\Debug\Tracy\SessionPanel
  • MvcCore\Ext\Form
  • MvcCore\Ext\Form\Button
  • MvcCore\Ext\Form\Checkbox
  • MvcCore\Ext\Form\CheckboxGroup
  • MvcCore\Ext\Form\Core\Base
  • MvcCore\Ext\Form\Core\Configuration
  • MvcCore\Ext\Form\Core\Field
  • MvcCore\Ext\Form\Core\FieldGroup
  • MvcCore\Ext\Form\Core\Helpers
  • MvcCore\Ext\Form\Core\Validator
  • MvcCore\Ext\Form\Core\View
  • MvcCore\Ext\Form\CountrySelect
  • MvcCore\Ext\Form\Date
  • MvcCore\Ext\Form\DateTime
  • MvcCore\Ext\Form\Email
  • MvcCore\Ext\Form\Hidden
  • MvcCore\Ext\Form\NoType
  • MvcCore\Ext\Form\Number
  • MvcCore\Ext\Form\Password
  • MvcCore\Ext\Form\RadioGroup
  • MvcCore\Ext\Form\Range
  • MvcCore\Ext\Form\ResetButton
  • MvcCore\Ext\Form\ResetInput
  • MvcCore\Ext\Form\Select
  • MvcCore\Ext\Form\SubmitButton
  • MvcCore\Ext\Form\SubmitInput
  • MvcCore\Ext\Form\Text
  • MvcCore\Ext\Form\Textarea
  • MvcCore\Ext\Form\Time
  • MvcCore\Ext\Form\Validators\CompanyId
  • MvcCore\Ext\Form\Validators\CompanyVatId
  • MvcCore\Ext\Form\Validators\Date
  • MvcCore\Ext\Form\Validators\Email
  • MvcCore\Ext\Form\Validators\FloatVal
  • MvcCore\Ext\Form\Validators\Integer
  • MvcCore\Ext\Form\Validators\Maxlength
  • MvcCore\Ext\Form\Validators\MaxSelectedOptions
  • MvcCore\Ext\Form\Validators\MinSelectedOptions
  • MvcCore\Ext\Form\Validators\NumberField
  • MvcCore\Ext\Form\Validators\Pattern
  • MvcCore\Ext\Form\Validators\Phone
  • MvcCore\Ext\Form\Validators\RangeField
  • MvcCore\Ext\Form\Validators\SafeString
  • MvcCore\Ext\Form\Validators\Time
  • MvcCore\Ext\Form\Validators\Url
  • MvcCore\Ext\Form\Validators\ValueInOptions
  • MvcCore\Ext\Form\Validators\ZipCode
  • MvcCore\Ext\Request\ApacheDpi
  • MvcCore\Ext\Request\Cli
  • MvcCore\Ext\Router\Lang
  • MvcCore\Ext\Router\Lang\Route
  • MvcCore\Ext\Router\Media
  • MvcCore\Ext\Router\MediaSiteKey
  • MvcCore\Ext\View\Helpers\Assets
  • MvcCore\Ext\View\Helpers\Css
  • MvcCore\Ext\View\Helpers\Js
  • MvcCore\Ext\View\Helpers\LineBreaks
  • MvcCore\Model
  • MvcCore\Request
  • MvcCore\Response
  • MvcCore\Route
  • MvcCore\Router
  • MvcCore\Session
  • MvcCore\Tool
  • MvcCore\View

Exceptions

  • MvcCore\Ext\Form\Core\Exception
  • Overview
  • Namespace
  • Class
  • Tree
  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

/**
 * MvcCore
 *
 * This source file is subject to the BSD 3 License
 * For the full copyright and license information, please view
 * the LICENSE.md file that are distributed with this source code.
 *
 * @copyright   Copyright (c) 2016 Tom FlĂ­dr (https://github.com/mvccore/mvccore)
 * @license     https://mvccore.github.io/docs/mvccore/4.0.0/LICENCE.md
 */

namespace MvcCore\Ext\Debug\Tracy;

class SessionPanel implements \Tracy\IBarPanel
{
    /**
     * MvcCore Extension - Debug - Tracy Panel - Session - version:
     * Comparation by PHP function version_compare();
     * @see http://php.net/manual/en/function.version-compare.php
     */
    const VERSION = '4.2.0';
    
    const TYPE_PHP = 0;
    const TYPE_NAMESPACE = 1;

    const EXPIRATION_HOOPS = 1;
    const EXPIRATION_TIME = 2;

    /**
     * \MvcCore\Session meta info store key in $_SESSION
     * @var string 
     */
    public static $MetaStoreKey = \MvcCore\Session::SESSION_METADATA_KEY;
    /**
     * Readed session store as data printed in template
     * @var array
     */
    public static $Session = array();
    /**
     * Formated max. lifetime from \MvcCore\Session namespace
     * @var string
     */
    public static $SessionMaxLifeTime = '';
    /**
     * Debug panel id
     * @var string
     */
    public static $Id = 'session-panel';
    /**
     * Now time at \MvcCore\Ext\Debug\SessionPanel static init - mostly in request begin
     * @var int
     */
    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;

        // read \MvcCore\Session storage
        $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();

        // look for each record in $_SESSION if data are defined as namespace in \MvcCore\Session meta store
        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) {
        //$timeFormated = '';
        $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);
    }

    /**
     * Dumps any variable to clickable html string reprezentation.
     * @param  mixed  $dump
     * @return string
     */
    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>&#x25bc;</abbr> </a><code>'
                        :
                            '<abbr>&#x25ba;</abbr> </a><code class="nette-collapsed">'
                    );
            },
            \Tracy\Dumper::toHtml($dump)
        ) . '</pre>';
    }
}
MvcCore API documentation generated by ApiGen