Completed
Push — 6.0 ( a1342f...a265e4 )
by liu
03:24
created

SessionInit   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 38
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 26 6
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\middleware;
14
15
use Closure;
16
use think\App;
17
use think\Request;
18
use think\response\Redirect as RedirectResponse;
19
use think\Session;
20
21
/**
22
 * Session初始化
23
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
24
class SessionInit
25
{
26
27
    /**
28
     * Session初始化
29
     * @access public
30
     * @param Request $request
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
31
     * @param Closure $next
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
32
     * @param App     $app
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
33
     * @param Session $session
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
34
     * @return void
35
     */
36
    public function handle($request, Closure $next, App $app, Session $session)
37
    {
38
        // Session初始化
39
        $varSessionId = $app->config->get('session.var_session_id');
40
        $cookieName   = $app->config->get('session.name') ?: 'PHPSESSID';
41
42
        if ($varSessionId && $request->request($varSessionId)) {
43
            $sessionId = $request->request($varSessionId);
44
        } else {
45
            $sessionId = $request->cookie($cookieName) ?: '';
46
        }
47
48
        $session->setId($sessionId);
49
50
        $request->withSession($session);
51
52
        $response = $next($request)->setSession($session);
53
54
        $app->cookie->set($cookieName, $session->getId());
55
56
        // 清空当次请求有效的数据
57
        if (!($response instanceof RedirectResponse)) {
58
            $session->flush();
59
        }
60
61
        return $response;
62
    }
63
}
64