Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
lazysessionstart.php
1<?php
2
4
7
8final class LazySessionStart implements \ArrayAccess
9{
10 private static $instance;
11
12 public static function register(): void
13 {
14 if (self::$instance)
15 {
16 throw new InvalidOperationException("LazySessionStart was already registered.");
17 }
18
19 // It's very important to make a reference to the LazySessionStart object,
20 // because when somebody uses $_SESSION['d'] += $value;
21 // it converts to: offsetGet & offsetSet. But PHP destroys
22 // the object because it'll be the last reference and offsetSet crashes.
23 $_SESSION = self::$instance = new self();
24 }
25
26 protected function start(): void
27 {
28 if ($this->isSessionAlreadyClosed() && !Application::getInstance()->getSession()->isAccessible())
29 {
30 $this->writeToLogError(
31 new \RuntimeException(
32 "Skipped cold session start because headers have already been sent. Be aware and fix usage of session, details in trace."
33 )
34 );
35
36 $GLOBALS['_SESSION'] = [];
37
38 return;
39 }
40
41 Application::getInstance()->getSession()->start();
42 }
43
44 public function offsetExists($offset): bool
45 {
46 $this->start();
47
48 return isset($_SESSION[$offset]);
49 }
50
51 #[\ReturnTypeWillChange]
52 public function &offsetGet($offset)
53 {
54 $this->start();
55
56 return $_SESSION[$offset];
57 }
58
59 public function offsetSet($offset, $value): void
60 {
61 $this->start();
62
63 $_SESSION[$offset] = $value;
64 }
65
66 public function offsetUnset($offset): void
67 {
68 $this->start();
69
70 unset($_SESSION[$offset]);
71 }
72
73 private function isKernelWentSessionStart(): bool
74 {
75 return defined('BX_STARTED');
76 }
77
78 private function isSessionAlreadyClosed(): bool
79 {
80 return
81 $this->isKernelWentSessionStart()
82 && !Application::getInstance()->getKernelSession()->isStarted()
83 ;
84 }
85
86 private function writeToLogError(\RuntimeException $exception): void
87 {
88 $exceptionHandler = Application::getInstance()->getExceptionHandler();
89 $exceptionHandler->writeToLog($exception);
90 }
91}
$GLOBALS['____1444769544']
Definition license.php:1