1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Scrawler package. |
4
|
|
|
* |
5
|
|
|
* (c) Pranjal Pandey <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Scrawler\Http; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Session class adds magic to the Symfony session. |
17
|
|
|
*/ |
18
|
|
|
class Session extends \Symfony\Component\HttpFoundation\Session\Session |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Magic method to directly set session variable. |
22
|
|
|
*/ |
23
|
|
|
public function __set(string $key, string $value): void |
24
|
|
|
{ |
25
|
|
|
$this->set($key, $value); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Magic method to directly get session data. |
30
|
|
|
*/ |
31
|
|
|
public function __get(string $key): mixed |
32
|
|
|
{ |
33
|
|
|
return $this->get($key); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* check if session or flashbag has key. |
38
|
|
|
*/ |
39
|
|
|
#[\Override] |
40
|
|
|
public function has(string $key): bool |
41
|
|
|
{ |
42
|
|
|
return parent::has($key) || parent::getFlashBag()->has($key); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Invalidates the current session. |
47
|
|
|
* |
48
|
|
|
* Clears all session attributes and flashes and regenerates the |
49
|
|
|
* session and deletes the old session from persistence. |
50
|
|
|
*/ |
51
|
|
|
public function stop(): void |
52
|
|
|
{ |
53
|
|
|
$this->invalidate(0); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get/Set flash message |
58
|
|
|
* If message and type is provided then set the flash message |
59
|
|
|
* If only type is provided then return all messages of that type. |
60
|
|
|
* |
61
|
|
|
* @return array<mixed>|null |
62
|
|
|
*/ |
63
|
|
|
public function flash(?string $type = null, ?string $message = null): ?array |
64
|
|
|
{ |
65
|
|
|
if (!is_null($message) && !is_null($type)) { |
66
|
|
|
$this->getFlashBag()->add($type, $message); |
67
|
|
|
|
68
|
|
|
return null; |
69
|
|
|
} else { |
70
|
|
|
if (!is_null($type)) { |
71
|
|
|
return $this->getFlashBag()->get($type); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->getFlashBag()->all(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|