Passed
Push — 6.0 ( 85e367...f409e2 )
by
unknown
02:33
created

CheckRequestCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Cache;
17
use think\Request;
18
use think\Response;
19
20
class CheckRequestCache
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
21
{
22
    protected $cache;
23
24
    public function __construct(Cache $cache)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
25
    {
26
        $this->cache = $cache;
27
    }
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $next should have a doc-comment as per coding-style.
Loading history...
30
     * 设置当前地址的请求缓存
31
     * @access public
32
     * @param Request $request
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
33
     * @param         $next
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 9
Loading history...
34
     * @return Response
35
     */
36
    public function handle($request, Closure $next)
37
    {
38
        $cache = $request->cache();
39
40
        if ($cache) {
41
            list($key, $expire, $tag) = $cache;
42
43
            if (strtotime($request->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $request->server('REQUEST_TIME')) {
44
                // 读取缓存
45
                return Response::create()->code(304);
46
            } elseif ($this->cache->has($key)) {
47
                list($content, $header) = $this->cache->get($key);
48
49
                return Response::create($content)->header($header);
50
            }
51
        }
52
53
        return $next($request);
54
    }
55
}
56