Session.php 2.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 5/4/16
 * Time: 1:34 PM
 */

namespace qfq;


class Session {
    private static $instance = null;
    private static $phpUnit = null;
    private static $sessionLocal = array();

    /**
     * @param bool|false $phpUnit
Carsten  Rose's avatar
Carsten Rose committed
19
     * @throws CodeException
20
21
22
23
24
25
26
     */
    private function __construct($phpUnit = false) {
        if (self::$phpUnit !== null)
            throw new CodeException("Try to set flag phpunit again - that should not happen.", ERROR_CODE_SHOULD_NOT_HAPPEN);

        self::$phpUnit = $phpUnit;

27
        if (self::$phpUnit === true) {
28
29
            self::$sessionLocal = array();
        } else {
30
            session_name(SESSION_NAME);
31
32
            session_start();
        }
33
        self::checkFeUserUid();
34
35
36
    }

    /**
37
     *
38
     */
39
    private static function checkFeUserUid() {
40

41
42
        $feUidLoggedIn = isset($GLOBALS["TSFE"]->fe_user->user["uid"]) ? $GLOBALS["TSFE"]->fe_user->user["uid"] : false;
        $feUidSession = Session::get(SESSION_FE_USER_UID);
43

44
45
46
47
48
49
50
        if ($feUidLoggedIn !== $feUidSession) {
            // destroy existing session store
            Session::clear();

            // save new feUserUid
            Session::set(SESSION_FE_USER_UID, $feUidLoggedIn);
        }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    }

    /**
     * @param $key
     * @return bool
     */
    public static function get($key) {

        if (self::$phpUnit) {
            if (isset(self::$sessionLocal[$key]))
                $value = self::$sessionLocal[$key];
            else
                $value = false;
        } else {
65
66
            if (isset($_SESSION[SESSION_NAME][$key]))
                $value = $_SESSION[SESSION_NAME][$key];
67
68
69
70
71
72
73
74
            else
                $value = false;

        }

        return $value;
    }

75
76
77
78
79
80
81
82
83
84
85
86
    /**
     *
     */
    public static function clear() {

        if (self::$phpUnit) {
            self::$sessionLocal = array();
        } else {
            $_SESSION[SESSION_NAME] = array();
        }
    }

87
88
89
90
91
92
93
94
95
    /**
     * @param $key
     * @param $value
     */
    public static function set($key, $value) {

        if (self::$phpUnit) {
            self::$sessionLocal[$key] = $value;
        } else {
96
            $_SESSION[SESSION_NAME][$key] = $value;
97
98
99
100
        }
    }

    /**
101
102
     * @param bool|false $phpUnit
     * @return null|\qfq\Store
103
     */
104
    public static function getInstance($phpUnit = false) {
105

106
107
108
        // Design Pattern: Singleton
        if (self::$instance === null) {
            self::$instance = new self($phpUnit);
109
        }
110
111

        return self::$instance;
112
113
114
    }

}