[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. 4 * @license GNU General Public License version 2 or later; see LICENSE 5 */ 6 7 namespace Joomla\Session\Storage; 8 9 use Joomla\Session\StorageInterface; 10 11 /** 12 * Session storage object that stores objects in Runtime memory. This is designed for use in CLI Apps, including 13 * unit testing applications in PHPUnit. 14 * 15 * @since 2.0.0 16 */ 17 class RuntimeStorage implements StorageInterface 18 { 19 /** 20 * Flag if the session is active 21 * 22 * @var boolean 23 * @since 2.0.0 24 */ 25 private $active = false; 26 27 /** 28 * Internal flag identifying whether the session has been closed 29 * 30 * @var boolean 31 * @since 2.0.0 32 */ 33 private $closed = false; 34 35 /** 36 * Internal data store 37 * 38 * @var array 39 * @since 2.0.0 40 */ 41 private $data = []; 42 43 /** 44 * Session ID 45 * 46 * @var string 47 * @since 2.0.0 48 */ 49 private $id = ''; 50 51 /** 52 * Session Name 53 * 54 * @var string 55 * @since 2.0.0 56 */ 57 private $name = 'MockSession'; 58 59 /** 60 * Internal flag identifying whether the session has been started 61 * 62 * @var boolean 63 * @since 2.0.0 64 */ 65 private $started = false; 66 67 /** 68 * Retrieves all variables from the session store 69 * 70 * @return array 71 */ 72 public function all(): array 73 { 74 return $this->data; 75 } 76 77 /** 78 * Clears all variables from the session store 79 * 80 * @return void 81 * 82 * @since 2.0.0 83 */ 84 public function clear(): void 85 { 86 $this->data = []; 87 } 88 89 /** 90 * Writes session data and ends session 91 * 92 * @return void 93 * 94 * @see session_write_close() 95 * @since 2.0.0 96 */ 97 public function close(): void 98 { 99 $this->closed = true; 100 $this->started = false; 101 } 102 103 /** 104 * Perform session data garbage collection 105 * 106 * @return integer|boolean Number of deleted sessions on success or boolean false on failure or if the function is unsupported 107 * 108 * @see session_gc() 109 * @since 2.0.0 110 */ 111 public function gc() 112 { 113 return 0; 114 } 115 116 /** 117 * Aborts the current session 118 * 119 * @return boolean 120 * 121 * @see session_abort() 122 * @since 2.0.0 123 */ 124 public function abort(): bool 125 { 126 $this->closed = true; 127 $this->started = false; 128 129 return true; 130 } 131 132 /** 133 * Generates a session ID 134 * 135 * @return string 136 * 137 * @since 2.0.0 138 */ 139 private function generateId(): string 140 { 141 return hash('sha256', uniqid(mt_rand())); 142 } 143 144 /** 145 * Get data from the session store 146 * 147 * @param string $name Name of a variable 148 * @param mixed $default Default value of a variable if not set 149 * 150 * @return mixed Value of a variable 151 * 152 * @since 2.0.0 153 */ 154 public function get(string $name, $default) 155 { 156 if (!$this->isStarted()) 157 { 158 $this->start(); 159 } 160 161 if (isset($this->data[$name])) 162 { 163 return $this->data[$name]; 164 } 165 166 return $default; 167 } 168 169 /** 170 * Get the session ID 171 * 172 * @return string The session ID 173 * 174 * @since 2.0.0 175 */ 176 public function getId(): string 177 { 178 return $this->id; 179 } 180 181 /** 182 * Get the session name 183 * 184 * @return string The session name 185 * 186 * @since 2.0.0 187 */ 188 public function getName(): string 189 { 190 return $this->name; 191 } 192 193 /** 194 * Check whether data exists in the session store 195 * 196 * @param string $name Name of variable 197 * 198 * @return boolean 199 * 200 * @since 2.0.0 201 */ 202 public function has(string $name): bool 203 { 204 if (!$this->isStarted()) 205 { 206 $this->start(); 207 } 208 209 return isset($this->data[$name]); 210 } 211 212 /** 213 * Check if the session is active 214 * 215 * @return boolean 216 * 217 * @since 2.0.0 218 */ 219 public function isActive(): bool 220 { 221 return $this->active = $this->started; 222 } 223 224 /** 225 * Check if the session is started 226 * 227 * @return boolean 228 * 229 * @since 2.0.0 230 */ 231 public function isStarted(): bool 232 { 233 return $this->started; 234 } 235 236 /** 237 * Unset a variable from the session store 238 * 239 * @param string $name Name of variable 240 * 241 * @return mixed The value from session or NULL if not set 242 * 243 * @since 2.0.0 244 */ 245 public function remove(string $name) 246 { 247 if (!$this->isStarted()) 248 { 249 $this->start(); 250 } 251 252 $old = $this->data[$name] ?? null; 253 254 unset($this->data[$name]); 255 256 return $old; 257 } 258 259 /** 260 * Regenerates the session ID that represents this storage. 261 * 262 * This method must invoke session_regenerate_id($destroy) unless this interface is used for a storage object designed for unit 263 * or functional testing where a real PHP session would interfere with testing. 264 * 265 * @param boolean $destroy Destroy session when regenerating? 266 * 267 * @return boolean True on success 268 * 269 * @see session_regenerate_id() 270 * @since 2.0.0 271 */ 272 public function regenerate(bool $destroy = false): bool 273 { 274 if (!$this->isActive()) 275 { 276 return false; 277 } 278 279 if ($destroy) 280 { 281 $this->id = $this->generateId(); 282 } 283 284 return true; 285 } 286 287 /** 288 * Set data into the session store 289 * 290 * @param string $name Name of a variable 291 * @param mixed $value Value of a variable 292 * 293 * @return mixed Old value of a variable 294 * 295 * @since 2.0.0 296 */ 297 public function set(string $name, $value = null) 298 { 299 if (!$this->isStarted()) 300 { 301 $this->start(); 302 } 303 304 $old = $this->data[$name] ?? null; 305 306 $this->data[$name] = $value; 307 308 return $old; 309 } 310 311 /** 312 * Set the session ID 313 * 314 * @param string $id The session ID 315 * 316 * @return $this 317 * 318 * @since 2.0.0 319 * @throws \LogicException 320 */ 321 public function setId(string $id) 322 { 323 if ($this->isActive()) 324 { 325 throw new \LogicException('Cannot change the ID of an active session'); 326 } 327 328 $this->id = $id; 329 330 return $this; 331 } 332 333 /** 334 * Set the session name 335 * 336 * @param string $name The session name 337 * 338 * @return $this 339 * 340 * @since 2.0.0 341 * @throws \LogicException 342 */ 343 public function setName(string $name) 344 { 345 if ($this->isActive()) 346 { 347 throw new \LogicException('Cannot change the name of an active session'); 348 } 349 350 $this->name = $name; 351 352 return $this; 353 } 354 355 /** 356 * Start a session 357 * 358 * @return void 359 * 360 * @since 2.0.0 361 */ 362 public function start(): void 363 { 364 if ($this->isStarted()) 365 { 366 return; 367 } 368 369 if ($this->isActive()) 370 { 371 throw new \RuntimeException('Failed to start the session: already started by PHP.'); 372 } 373 374 if (empty($this->id)) 375 { 376 $this->setId($this->generateId()); 377 } 378 379 $this->closed = false; 380 $this->started = true; 381 $this->isActive(); 382 } 383 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |