Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
sessionlocalstoragemanager.php
1
<?php
2
namespace
Bitrix\Main\Data\LocalStorage
;
3
4
use
Bitrix\Main\Application
;
5
use
Bitrix\Main\Data\LocalStorage\Storage
;
6
use
Bitrix\Main\Event
;
7
use
Bitrix\Main\EventManager
;
8
use
Bitrix\Main\Session\CompositeSessionManager
;
9
10
final
class
SessionLocalStorageManager
11
{
12
private
const
POINTER_STORE =
'__store'
;
13
15
private
array $collection = [];
16
private
string
$uniqueId;
17
private
Storage\StorageInterface
$storage;
18
private
int
$ttl = 86400;
19
20
public
function
__construct
(Storage\
StorageInterface
$storage)
21
{
22
$this->storage = $storage;
23
24
EventManager::getInstance
()->addEventHandler(
'main'
,
'OnAfterEpilog'
, [$this,
'save'
]);
25
EventManager::getInstance
()->addEventHandler(
26
'main'
,
27
CompositeSessionManager::EVENT_REGENERATE_SESSION_ID
,
28
[$this,
'handleRegenerateSessionId'
]
29
);
30
}
31
32
public
function
save
(): void
33
{
34
foreach
($this->collection as $item)
35
{
36
$this->storage->write($item->getUniqueName(), $item->getData(), $this->getTtl());
37
}
38
}
39
40
public
function
handleRegenerateSessionId
(
Event
$event): void
41
{
42
$oldSessionId = $this->
getUniqueId
();
43
$newSessionId = $event->
getParameter
(
'newSessionId'
);
44
if
(!$newSessionId)
45
{
46
return
;
47
}
48
49
$pointerStore = $this->getPointerStore();
50
$list = $pointerStore[
'list'
] ?? [];
51
foreach
($list as $registeredName)
52
{
53
$this->load($registeredName);
54
}
55
56
$oldLocalStorageManager =
new
self
($this->storage);
57
$oldLocalStorageManager
58
->setUniqueId($oldSessionId)
59
->clearAll()
60
;
61
62
$this->
setUniqueId
($newSessionId);
63
foreach
($this->collection as $item)
64
{
65
$item->setUniqueName($this->
buildUniqueKey
($item->getName()));
66
}
67
}
68
69
public
function
getUniqueId
(): string
70
{
71
return
$this->uniqueId;
72
}
73
74
public
function
setUniqueId
(
string
$uniqueId): self
75
{
76
$this->uniqueId = $uniqueId;
77
78
return
$this;
79
}
80
81
public
function
getTtl
(): int
82
{
83
return
$this->ttl;
84
}
85
86
public
function
setTtl
(
int
$ttl): self
87
{
88
$this->ttl = $ttl;
89
90
return
$this;
91
}
92
93
private
function
getPointerStore():
SessionLocalStorage
94
{
95
return
$this->
get
(self::POINTER_STORE);
96
}
97
98
private
function
registerInStore(
SessionLocalStorage
$localStorage): void
99
{
100
if
($localStorage->
getName
() === self::POINTER_STORE)
101
{
102
return
;
103
}
104
105
$pointerStore = $this->getPointerStore();
106
$pointerStore[
'list'
][] = $localStorage->
getName
();
107
}
108
109
private
function
load(
string
$name): void
110
{
111
$this->
get
($name);
112
}
113
114
public
function
get
(
string
$name):
SessionLocalStorage
115
{
116
if
(!$this->
exists
($name))
117
{
118
$item =
new
SessionLocalStorage
($this->
buildUniqueKey
($name), $name);
119
$data = $this->storage->read($item->getUniqueName(), $this->getTtl());
120
if
(isset($data) && is_array($data))
121
{
122
$item->setData($data);
123
}
124
125
$this->registerInStore($item);
126
$this->collection[$name] = $item;
127
}
128
129
return
$this->collection[$name];
130
}
131
132
public
function
exists
(
string
$name): bool
133
{
134
return
isset($this->collection[$name]);
135
}
136
137
public
function
clear
(
string
$name): void
138
{
139
$this->
get
($name)->
clear
();
140
}
141
142
public
function
clearAll
(): void
143
{
144
$pointerStore = $this->getPointerStore();
145
$list = $pointerStore[
'list'
] ?? [];
146
foreach
($list as $registeredName)
147
{
148
$this->
clear
($registeredName);
149
}
150
$pointerStore->clear();
151
}
152
153
protected
function
buildUniqueKey
(
string
$name): string
154
{
155
return
$this->uniqueId .
'_'
. $name;
156
}
157
158
public
function
isReady
()
159
{
160
return
Application::getInstance
()->getKernelSession()->isStarted();
161
}
162
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\Data\LocalStorage\SessionLocalStorage
Definition
sessionlocalstorage.php:5
Bitrix\Main\Data\LocalStorage\SessionLocalStorage\getName
getName()
Definition
sessionlocalstorage.php:27
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager
Definition
sessionlocalstoragemanager.php:11
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\setUniqueId
setUniqueId(string $uniqueId)
Definition
sessionlocalstoragemanager.php:74
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\clearAll
clearAll()
Definition
sessionlocalstoragemanager.php:142
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\isReady
isReady()
Definition
sessionlocalstoragemanager.php:158
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\setTtl
setTtl(int $ttl)
Definition
sessionlocalstoragemanager.php:86
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\getUniqueId
getUniqueId()
Definition
sessionlocalstoragemanager.php:69
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\buildUniqueKey
buildUniqueKey(string $name)
Definition
sessionlocalstoragemanager.php:153
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\handleRegenerateSessionId
handleRegenerateSessionId(Event $event)
Definition
sessionlocalstoragemanager.php:40
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\getTtl
getTtl()
Definition
sessionlocalstoragemanager.php:81
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\exists
exists(string $name)
Definition
sessionlocalstoragemanager.php:132
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\__construct
__construct(Storage\StorageInterface $storage)
Definition
sessionlocalstoragemanager.php:20
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\clear
clear(string $name)
Definition
sessionlocalstoragemanager.php:137
Bitrix\Main\Data\LocalStorage\SessionLocalStorageManager\save
save()
Definition
sessionlocalstoragemanager.php:32
Bitrix\Main\Event
Definition
event.php:5
Bitrix\Main\Event\getParameter
getParameter($key)
Definition
event.php:80
Bitrix\Main\EventManager
Definition
eventmanager.php:15
Bitrix\Main\EventManager\getInstance
static getInstance()
Definition
eventmanager.php:33
Bitrix\Main\Session\CompositeSessionManager
Definition
compositesessionmanager.php:8
Bitrix\Main\Session\CompositeSessionManager\EVENT_REGENERATE_SESSION_ID
const EVENT_REGENERATE_SESSION_ID
Definition
compositesessionmanager.php:9
Bitrix\Main\Data\LocalStorage\Storage\StorageInterface
Definition
storageinterface.php:5
Bitrix\Main\Data\LocalStorage\Storage
Definition
cacheengineinterface.php:2
Bitrix\Main\Data\LocalStorage
Definition
sessionlocalstorage.php:2
modules
main
lib
data
localstorage
sessionlocalstoragemanager.php
Создано системой
1.10.0