ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
SessionRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\KeyValueStorage\SessionRepository as KeyValueSessionRepository;
25
40final readonly class SessionRepository implements KeyValueSessionRepository
41{
42 private const string PREFIX = 'kvs:';
43
44 private const string SEPARATOR = ':';
45
46 public function has(StorageNamespace $namespace, string $key): bool
47 {
48 return \ilSession::has($this->sessionKey($namespace, $key));
49 }
50
51 public function read(StorageNamespace $namespace, string $key): ?string
52 {
53 $value = \ilSession::get($this->sessionKey($namespace, $key));
54
55 return \is_string($value) ? $value : null;
56 }
57
58 public function readAll(StorageNamespace $namespace): array
59 {
60 $prefix = $this->namespacePrefix($namespace);
61 $prefix_length = \strlen($prefix);
62 $entries = [];
63
64 foreach (\ilSession::keys() as $session_key) {
65 if (!\str_starts_with($session_key, $prefix)) {
66 continue;
67 }
68
69 $value = \ilSession::get($session_key);
70 if (!\is_string($value)) {
71 continue;
72 }
73
74 $entries[\substr($session_key, $prefix_length)] = $value;
75 }
76
77 return $entries;
78 }
79
80 public function write(StorageNamespace $namespace, string $key, string $value): void
81 {
82 \ilSession::set($this->sessionKey($namespace, $key), $value);
83 }
84
85 public function remove(StorageNamespace $namespace, string $key): void
86 {
87 \ilSession::clear($this->sessionKey($namespace, $key));
88 }
89
90 public function removeAll(StorageNamespace $namespace): void
91 {
92 $prefix = $this->namespacePrefix($namespace);
93
94 foreach (\ilSession::keys() as $session_key) {
95 if (\str_starts_with($session_key, $prefix)) {
96 \ilSession::clear($session_key);
97 }
98 }
99 }
100
101 private function sessionKey(StorageNamespace $namespace, string $key): string
102 {
103 return $this->namespacePrefix($namespace) . $key;
104 }
105
106 private function namespacePrefix(StorageNamespace $namespace): string
107 {
108 return self::PREFIX . $namespace->value() . self::SEPARATOR;
109 }
110}
Keeps key-value storage in the ILIAS session.
readAll(StorageNamespace $namespace)
Every present entry of one namespace.
sessionKey(StorageNamespace $namespace, string $key)
read(StorageNamespace $namespace, string $key)
write(StorageNamespace $namespace, string $key, string $value)
has(StorageNamespace $namespace, string $key)
Isolates the keys of one consumer from every other consumer of a storage.
static get(string $a_var)
static clear(string $a_var)
static keys()
The names of all variables currently held in the session.
static set(string $a_var, $a_val)
Set a value.
Persistence of the session scope, implemented by the component owning the ILIAS session.
has(string $class_name)