1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Session; |
6
|
|
|
|
7
|
|
|
use Dflydev\FigCookies\SetCookie; |
8
|
|
|
use Lcobucci\JWT\Parser; |
9
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
10
|
|
|
use Pimple\Container; |
11
|
|
|
use Pimple\ServiceProviderInterface; |
12
|
|
|
use PSR7Sessions\Storageless\Http\SessionMiddleware; |
13
|
|
|
use PSR7Sessions\Storageless\Time\SystemCurrentTime; |
14
|
|
|
|
15
|
|
|
final class SessionProvider implements ServiceProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param Container $container |
19
|
|
|
*/ |
20
|
1 |
|
public function register(Container $container) |
21
|
|
|
{ |
22
|
1 |
|
$this->registerSettings($container); |
|
|
|
|
23
|
1 |
|
$this->registerSetCookie($container); |
|
|
|
|
24
|
1 |
|
$this->registerMiddleware($container); |
|
|
|
|
25
|
|
|
|
26
|
1 |
|
$container['session'] = function () use ($container) { |
27
|
1 |
|
return new Session($container['logger'] ?? null); |
28
|
|
|
}; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Container $container |
33
|
|
|
*/ |
34
|
1 |
|
private function registerSettings(Container $container) |
35
|
|
|
{ |
36
|
1 |
|
$container['session.expirationTime'] = 1200; |
37
|
1 |
|
$container['session.privateRsaKey'] = ''; |
38
|
1 |
|
$container['session.publicRsaKey'] = ''; |
39
|
|
|
|
40
|
1 |
|
$container['session.setCookieHttpOnly'] = true; |
41
|
1 |
|
$container['session.setCookiePath'] = '/'; |
42
|
1 |
|
$container['session.setCookieSecureOnly'] = true; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Container $container |
47
|
|
|
*/ |
48
|
|
|
private function registerSetCookie(Container $container) |
49
|
|
|
{ |
50
|
1 |
|
$container['session.setCookie'] = function () use ($container) { |
51
|
1 |
|
return SetCookie::create(SessionMiddleware::DEFAULT_COOKIE) |
52
|
1 |
|
->withHttpOnly($container['session.setCookieHttpOnly']) |
53
|
1 |
|
->withPath($container['session.setCookiePath']) |
54
|
1 |
|
->withSecure($container['session.setCookieSecureOnly']) |
55
|
|
|
; |
56
|
|
|
}; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Container $container |
61
|
|
|
*/ |
62
|
|
|
private function registerMiddleware(Container $container) |
63
|
|
|
{ |
64
|
1 |
|
$container['session.middleware'] = function () use ($container) { |
65
|
1 |
|
return new SessionMiddleware( |
66
|
1 |
|
new Sha256(), |
67
|
1 |
|
$container['session.privateRsaKey'], |
68
|
1 |
|
$container['session.publicRsaKey'], |
69
|
1 |
|
$container['session.setCookie'], |
70
|
1 |
|
new Parser(), |
71
|
1 |
|
$container['session.expirationTime'], |
72
|
1 |
|
new SystemCurrentTime() |
73
|
|
|
); |
74
|
|
|
}; |
75
|
1 |
|
} |
76
|
|
|
} |
77
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: