Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sessionlocalstoragemanager.php
1<?php
3
9
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',
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}
getParameter($key)
Definition event.php:80