1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Http; |
3
|
|
|
|
4
|
|
|
use Predis\Client; |
5
|
|
|
use SessionHandlerInterface; |
6
|
|
|
use Wandu\Config\Contracts\Config; |
7
|
|
|
use Wandu\DI\ContainerInterface; |
8
|
|
|
use Wandu\DI\ServiceProviderInterface; |
9
|
|
|
use Wandu\Http\Factory\ResponseFactory; |
10
|
|
|
use Wandu\Http\Session\Configuration; |
11
|
|
|
use Wandu\Http\Session\Handler\FileHandler; |
12
|
|
|
use Wandu\Http\Session\Handler\GlobalHandler; |
13
|
|
|
use Wandu\Http\Session\Handler\RedisHandler; |
14
|
|
|
|
15
|
|
|
class HttpServiceProvider implements ServiceProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
1 |
|
public function register(ContainerInterface $app) |
21
|
|
|
{ |
22
|
|
|
$app->closure(ResponseFactory::class, function () { |
23
|
|
|
return response(); // singleton |
24
|
1 |
|
}); |
25
|
|
|
$app->closure(Configuration::class, function (Config $config) { |
26
|
1 |
|
return new Configuration([ |
27
|
1 |
|
'timeout' => $config->get('session.timeout', 3600), |
28
|
1 |
|
'name' => $config->get('session.name', ini_get('session.name') ?: 'WdSessId'), |
29
|
1 |
|
'gc_frequency' => $config->get('session.gc_frequency', 100), |
30
|
|
|
]); |
31
|
1 |
|
}); |
32
|
1 |
|
$app->closure(SessionHandlerInterface::class, function (Config $config, ContainerInterface $app) { |
33
|
1 |
|
switch ($config->get('session.type')) { |
34
|
1 |
|
case 'file': |
35
|
|
|
return new FileHandler($config->get('session.path', 'cache/sessions')); |
36
|
1 |
|
case 'redis': |
37
|
|
|
return new RedisHandler($app[Client::class], $config->get('session.timeout', 3600)); |
38
|
|
|
default: |
39
|
1 |
|
return new GlobalHandler(); |
40
|
|
|
} |
41
|
1 |
|
}); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
1 |
|
public function boot(ContainerInterface $app) |
48
|
|
|
{ |
49
|
1 |
|
} |
50
|
|
|
} |
51
|
|
|
|