ILIAS  release_8 Revision v8.24
class.ilSessionIStorage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
42{
43 private string $session_id = "";
44 private string $component_id;
45 private static array $values = [];
46
53 public function __construct(string $a_component_id, string $a_sess_id = "")
54 {
55 $this->component_id = $a_component_id;
56 if ($a_sess_id !== "") {
57 $this->session_id = $a_sess_id;
58 } else {
59 $this->session_id = session_id();
60 }
61 }
62
63 private function initComponentCacheIfNotExists(): void
64 {
65 if (!isset(self::$values[$this->component_id]) || !is_array(self::$values[$this->component_id])) {
66 self::$values[$this->component_id] = [];
67 }
68 }
69
75 public function set(string $a_key, string $a_val): void
76 {
77 global $DIC;
78
79 $ilDB = $DIC['ilDB'];
80
82
83 self::$values[$this->component_id][$a_key] = $a_val;
84 $ilDB->replace(
85 "usr_sess_istorage",
86 array(
87 "session_id" => array("text", $this->session_id),
88 "component_id" => array("text", $this->component_id),
89 "vkey" => array("text", $a_key)
90 ),
91 array("value" => array("text", $a_val))
92 );
93 }
94
99 public function get(string $a_key): string
100 {
101 global $DIC;
102
103 $ilDB = $DIC['ilDB'];
104
105 if (isset(self::$values[$this->component_id][$a_key]) && is_array(self::$values[$this->component_id])) {
106 return self::$values[$this->component_id][$a_key];
107 }
108
109 $set = $ilDB->query(
110 "SELECT value FROM usr_sess_istorage " .
111 " WHERE session_id = " . $ilDB->quote($this->session_id, "text") .
112 " AND component_id = " . $ilDB->quote($this->component_id, "text") .
113 " AND vkey = " . $ilDB->quote($a_key, "text")
114 );
115 $rec = $ilDB->fetchAssoc($set);
116 $value = (string) ($rec['value'] ?? '');
117
119
120 self::$values[$this->component_id][$a_key] = $value;
121
122 return $value;
123 }
124
129 public static function destroySession($a_session_id): void
130 {
131 global $DIC;
132
133 if (!is_array($a_session_id)) {
134 $q = "DELETE FROM usr_sess_istorage WHERE session_id = " .
135 $DIC->database()->quote($a_session_id, "text");
136 } else {
137 $q = "DELETE FROM usr_sess_istorage WHERE " .
138 $DIC->database()->in("session_id", $a_session_id, false, "text");
139 }
140
141 $DIC->database()->manipulate($q);
142 }
143}
Session based immediate storage.
__construct(string $a_component_id, string $a_sess_id="")
Constructor.
static destroySession($a_session_id)
Destroy session(s).
global $DIC
Definition: feed.php:28