summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Session/Session.php
blob: e73e9b40d65c08ea354faf7be4d69b1af197bbce (plain)
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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Session;

use Icinga\Exception\NotImplementedError;

/**
 * Base class for handling sessions
 */
abstract class Session extends SessionNamespace
{
    /**
     * Container for session namespaces
     *
     * @var array
     */
    protected $namespaces = array();

    /**
     * The identifiers of all namespaces removed from this session
     *
     * @var array
     */
    protected $removedNamespaces = array();

    /**
     * Read all values from the underlying session implementation
     */
    abstract public function read();

    /**
     * Persists changes to the underlying session implementation
     */
    public function write()
    {
        throw new NotImplementedError('You are required to implement write() in your session implementation');
    }

    /**
     * Return whether a session exists
     *
     * @return  bool
     */
    abstract public function exists();

    /**
     * Purge session
     */
    abstract public function purge();

    /**
     * Assign a new session id to this session.
     */
    abstract public function refreshId();

    /**
     * Return the id of this session
     *
     * @return  string
     */
    abstract public function getId();

    /**
     * Get or create a new session namespace
     *
     * @param   string      $identifier     The namespace's identifier
     *
     * @return  SessionNamespace
     */
    public function getNamespace($identifier)
    {
        if (!isset($this->namespaces[$identifier])) {
            if (in_array($identifier, $this->removedNamespaces, true)) {
                unset($this->removedNamespaces[array_search($identifier, $this->removedNamespaces, true)]);
            }

            $this->namespaces[$identifier] = new SessionNamespace();
        }

        return $this->namespaces[$identifier];
    }

    /**
     * Return whether the given session namespace exists
     *
     * @param   string      $identifier     The namespace's identifier to check
     *
     * @return  bool
     */
    public function hasNamespace($identifier)
    {
        return isset($this->namespaces[$identifier]);
    }

    /**
     * Remove the given session namespace
     *
     * @param   string      $identifier     The identifier of the namespace to remove
     */
    public function removeNamespace($identifier)
    {
        unset($this->namespaces[$identifier]);
        $this->removedNamespaces[] = $identifier;
    }

    /**
     * Return whether the session has changed
     *
     * @return  bool
     */
    public function hasChanged()
    {
        return parent::hasChanged() || false === empty($this->namespaces) || false === empty($this->removedNamespaces);
    }

    /**
     * Clear all values and namespaces from the session cache
     */
    public function clear()
    {
        parent::clear();
        $this->namespaces = array();
        $this->removedNamespaces = array();
    }
}