1: <?php
2:
3: namespace Session;
4:
5: class SessionContainer {
6:
7: private static $instance;
8:
9: public static function getInstance()
10: {
11: if (null === static::$instance) {
12: static::$instance = new static();
13: }
14: return static::$instance;
15: }
16:
17: private $sessions;
18:
19: protected function __construct()
20: {
21: $this->sessions = array ();
22: }
23:
24: function existsSession ($id)
25: {
26: if (isset ($this->sessions[$id]))
27: {
28: return true;
29: } else {
30: return false;
31: }
32: }
33:
34: function deleteSession ($id)
35: {
36: if ($this->existsSession ($id))
37: {
38: unset($this->sessions[$id]);
39: }
40: }
41:
42: function cleanupInvalidSessions ()
43: {
44: foreach ($this->sessions as $id => $session)
45: {
46: if (!$session->isValid ())
47: {
48: $this->deleteSession ($id);
49: }
50: }
51: }
52:
53: function getSessionIds ()
54: {
55: $ret = array ();
56: foreach ($this->sessions as $id => $session)
57: {
58: if ($session->isValid ())
59: {
60: $ret[] = $id;
61: }
62: }
63: return $ret;
64: }
65:
66: function getSession ($id, $timeout = -1)
67: {
68: if (!isset ($this->sessions[$id]))
69: {
70: $this->sessions[$id] = SessionFactory::create ($id, $timeout);
71: }
72: $session = $this->sessions[$id];
73: if ($session->isValid ())
74: {
75: return $session;
76: } else {
77:
78: $this->sessions[$id] = SessionFactory::create ($id, $timeout);
79: $session = $this->sessions[$id];
80: return $session;
81: }
82: }
83:
84: private function __clone() {}
85: private function __wakeup() {}
86:
87: }
88:
89: