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