PluginProbe ʕ •ᴥ•ʔ
FAPI Member / 2.2.31
FAPI Member v2.2.31
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / libs / nette / http / src / Http / SessionSection.php
fapi-member / libs / nette / http / src / Http Last commit date
Context.php 4 weeks ago FileUpload.php 4 weeks ago Helpers.php 4 weeks ago IRequest.php 4 weeks ago IResponse.php 4 weeks ago Request.php 4 weeks ago RequestFactory.php 4 weeks ago Response.php 4 weeks ago Session.php 4 weeks ago SessionSection.php 4 weeks ago Url.php 4 weeks ago UrlImmutable.php 4 weeks ago UrlScript.php 4 weeks ago UserStorage.php 4 weeks ago
SessionSection.php
195 lines
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace FapiMember\Library\Nette\Http;
9
10 use FapiMember\Library\Nette;
11 /**
12 * Session section.
13 */
14 class SessionSection implements \IteratorAggregate, \ArrayAccess
15 {
16 use Nette\SmartObject;
17 /** @var bool */
18 public $warnOnUndefined = \false;
19 /** @var Session */
20 private $session;
21 /** @var string */
22 private $name;
23 /**
24 * Do not call directly. Use Session::getSection().
25 */
26 public function __construct(Session $session, string $name)
27 {
28 $this->session = $session;
29 $this->name = $name;
30 }
31 /**
32 * Returns an iterator over all section variables.
33 */
34 public function getIterator(): \Iterator
35 {
36 $this->session->autoStart(\false);
37 return new \ArrayIterator($this->getData() ?? []);
38 }
39 /**
40 * Sets a variable in this session section.
41 * @param mixed $value
42 */
43 public function set(string $name, $value, ?string $expire = null): void
44 {
45 if ($value === null) {
46 $this->remove($name);
47 } else {
48 $this->session->autoStart(\true);
49 $this->getData()[$name] = $value;
50 $this->setExpiration($expire, $name);
51 }
52 }
53 /**
54 * Gets a variable from this session section.
55 * @return mixed
56 */
57 public function get(string $name)
58 {
59 if (func_num_args() > 1) {
60 throw new \ArgumentCountError(__METHOD__ . '() expects 1 arguments, given more.');
61 }
62 $this->session->autoStart(\false);
63 return $this->getData()[$name] ?? null;
64 }
65 /**
66 * Removes a variable or whole section.
67 * @param string|string[]|null $name
68 */
69 public function remove($name = null): void
70 {
71 $this->session->autoStart(\false);
72 if (func_num_args() > 1) {
73 throw new \ArgumentCountError(__METHOD__ . '() expects at most 1 arguments, given more.');
74 } elseif (func_num_args()) {
75 $data =& $this->getData();
76 $meta =& $this->getMeta();
77 foreach ((array) $name as $name) {
78 unset($data[$name], $meta[$name]);
79 }
80 } else {
81 unset($_SESSION['__NF']['DATA'][$this->name], $_SESSION['__NF']['META'][$this->name]);
82 }
83 }
84 /**
85 * Sets a variable in this session section.
86 * @deprecated use set() instead
87 */
88 public function __set(string $name, $value): void
89 {
90 $this->session->autoStart(\true);
91 $this->getData()[$name] = $value;
92 }
93 /**
94 * Gets a variable from this session section.
95 * @deprecated use get() instead
96 */
97 public function &__get(string $name)
98 {
99 $this->session->autoStart(\true);
100 $data =& $this->getData();
101 if ($this->warnOnUndefined && !array_key_exists($name, $data ?? [])) {
102 trigger_error("The variable '{$name}' does not exist in session section");
103 }
104 return $data[$name];
105 }
106 /**
107 * Determines whether a variable in this session section is set.
108 * @deprecated use get() instead
109 */
110 public function __isset(string $name): bool
111 {
112 $this->session->autoStart(\false);
113 return isset($this->getData()[$name]);
114 }
115 /**
116 * Unsets a variable in this session section.
117 * @deprecated use remove() instead
118 */
119 public function __unset(string $name): void
120 {
121 $this->remove($name);
122 }
123 /**
124 * Sets a variable in this session section.
125 * @deprecated use set() instead
126 */
127 public function offsetSet($name, $value): void
128 {
129 $this->__set($name, $value);
130 }
131 /**
132 * Gets a variable from this session section.
133 * @deprecated use get() instead
134 */
135 #[\ReturnTypeWillChange]
136 public function offsetGet($name)
137 {
138 return $this->get($name);
139 }
140 /**
141 * Determines whether a variable in this session section is set.
142 * @deprecated use get() instead
143 */
144 public function offsetExists($name): bool
145 {
146 return $this->__isset($name);
147 }
148 /**
149 * Unsets a variable in this session section.
150 * @deprecated use remove() instead
151 */
152 public function offsetUnset($name): void
153 {
154 $this->remove($name);
155 }
156 /**
157 * Sets the expiration of the section or specific variables.
158 * @param ?string $expire
159 * @param string|string[]|null $variables list of variables / single variable to expire
160 * @return static
161 */
162 public function setExpiration($expire, $variables = null)
163 {
164 $this->session->autoStart((bool) $expire);
165 $meta =& $this->getMeta();
166 if ($expire) {
167 $expire = Nette\Utils\DateTime::from($expire)->format('U');
168 $max = (int) ini_get('session.gc_maxlifetime');
169 if ($max !== 0 && $expire - time() > $max + 3) {
170 trigger_error("The expiration time is greater than the session expiration {$max} seconds");
171 }
172 }
173 foreach (is_array($variables) ? $variables : [$variables] as $variable) {
174 $meta[$variable]['T'] = $expire ?: null;
175 }
176 return $this;
177 }
178 /**
179 * Removes the expiration from the section or specific variables.
180 * @param string|string[]|null $variables list of variables / single variable to expire
181 */
182 public function removeExpiration($variables = null): void
183 {
184 $this->setExpiration(null, $variables);
185 }
186 private function &getData()
187 {
188 return $_SESSION['__NF']['DATA'][$this->name];
189 }
190 private function &getMeta()
191 {
192 return $_SESSION['__NF']['META'][$this->name];
193 }
194 }
195