1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Http\Middleware; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Config\Config; |
8
|
|
|
use Exception; |
9
|
|
|
use Opulence\Framework\Sessions\Http\Middleware\Session as BaseSession; |
10
|
|
|
use Opulence\Http\Responses\Cookie; |
11
|
|
|
use Opulence\Http\Responses\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Defines the session middleware |
15
|
|
|
*/ |
16
|
|
|
class Session extends BaseSession |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
20
|
|
|
* |
21
|
|
|
* Runs garbage collection, if necessary |
22
|
|
|
* |
23
|
|
|
* @throws Exception |
24
|
|
|
*/ |
25
|
|
|
protected function gc(): void |
26
|
|
|
{ |
27
|
|
|
$rand = random_int(1, Config::mustGetInt('sessions', 'gc.divisor')); |
28
|
|
|
$chance = Config::mustGetInt('sessions', 'gc.chance'); |
29
|
|
|
if ($rand <= $chance) { |
30
|
|
|
$this->sessionHandler->gc(Config::mustGetInt('sessions', 'lifetime')); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
36
|
|
|
* |
37
|
|
|
* Writes any session data needed in the response |
38
|
|
|
* |
39
|
|
|
* @param Response $response The response to write to |
40
|
|
|
* |
41
|
|
|
* @return Response The response with data written to it |
42
|
|
|
*/ |
43
|
|
|
protected function writeToResponse(Response $response): Response |
44
|
|
|
{ |
45
|
|
|
$response->getHeaders()->setCookie( |
46
|
|
|
new Cookie( |
47
|
|
|
$this->session->getName(), |
48
|
|
|
$this->session->getId(), |
49
|
|
|
time() + Config::mustGetInt('sessions', 'lifetime'), |
50
|
|
|
Config::mustGetString('sessions', 'cookie.path'), |
51
|
|
|
Config::mustGetString('sessions', 'cookie.domain'), |
52
|
|
|
Config::mustGetBool('sessions', 'cookie.isSecure'), |
53
|
|
|
Config::mustGetBool('sessions', 'cookie.isHttpOnly') |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
return $response; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|