1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @license MIT |
4
|
|
|
*/ |
5
|
|
|
namespace Pivasic\Bundle\Common\Session; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Session |
9
|
|
|
* @package Pivasic\Bundle\Common\Session |
10
|
|
|
*/ |
11
|
|
|
class Session |
12
|
|
|
{ |
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->session = null; |
16
|
|
|
//ini_set('session.use_cookies', 1); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return SessionBag|null |
21
|
|
|
*/ |
22
|
|
|
public function bag() |
23
|
|
|
{ |
24
|
|
|
return $this->session; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return bool |
29
|
|
|
* @throws \RuntimeException |
30
|
|
|
*/ |
31
|
|
|
public function start() |
32
|
|
|
{ |
33
|
|
|
if (null !== $this->session) { |
34
|
|
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (\PHP_SESSION_ACTIVE === session_status()) { |
38
|
|
|
throw new \RuntimeException('Failed to start the session: already started by PHP.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (ini_get('session.use_cookies') && headers_sent($file, $line)) { |
42
|
|
|
throw new \RuntimeException(sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!session_start()) { |
46
|
|
|
throw new \RuntimeException('Failed to start the session'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->session = new SessionBag(); |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param bool|false $destroy |
56
|
|
|
* @param null $lifetime |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function regenerate(bool $destroy = false, $lifetime = null): bool |
60
|
|
|
{ |
61
|
|
|
if (\PHP_SESSION_ACTIVE !== session_status()) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (null !== $lifetime) { |
66
|
|
|
ini_set('session.cookie_lifetime', $lifetime); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return session_regenerate_id($destroy); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Destroy session. |
74
|
|
|
*/ |
75
|
|
|
public function destroy() |
76
|
|
|
{ |
77
|
|
|
session_unset(); |
78
|
|
|
session_destroy(); |
79
|
|
|
session_write_close(); |
80
|
|
|
if (!headers_sent()) { |
81
|
|
|
setcookie(session_name(), '', 0, '/'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets the session ID. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getId(): string |
91
|
|
|
{ |
92
|
|
|
return session_id(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets the session ID. |
97
|
|
|
* |
98
|
|
|
* @param string $id |
99
|
|
|
* @throws \LogicException |
100
|
|
|
*/ |
101
|
|
|
public function setId(string $id) |
102
|
|
|
{ |
103
|
|
|
if (null === $this->session) { |
104
|
|
|
throw new \LogicException('Cannot change the ID of an active session'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
session_id($id); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets the session name. |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getName(): string |
115
|
|
|
{ |
116
|
|
|
return session_name(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets the session name. |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* @throws \LogicException |
124
|
|
|
*/ |
125
|
|
|
public function setName(string $name) |
126
|
|
|
{ |
127
|
|
|
if (null === $this->session) { |
128
|
|
|
throw new \LogicException('Cannot change the name of an active session'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
session_name($name); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private $session; |
135
|
|
|
} |