Passed
Push — 6.0 ( 109fed...9bd2c2 )
by liu
12:10
created

LoadLangPack::saveToCookie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2021 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\Config;
18
use think\Cookie;
19
use think\Lang;
20
use think\Request;
21
use think\Response;
22
23
/**
24
 * 多语言加载
25
 */
26
class LoadLangPack
27
{
28
    protected $app;
29
    protected $lang;
30
    protected $config;
31
32
    public function __construct(App $app, Lang $lang, Config $config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
    public function __construct(App $app, Lang $lang, /** @scrutinizer ignore-unused */ Config $config)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        $this->app    = $app;
35
        $this->lang   = $lang;
36
        $this->config = $lang->getConfig();
37
    }
38
39
    /**
40
     * 路由初始化(路由规则注册)
41
     * @access public
42
     * @param Request $request
43
     * @param Closure $next
44
     * @return Response
45
     */
46
    public function handle($request, Closure $next)
47
    {
48
        // 自动侦测当前语言
49
        $langset = $this->detect($request);
50
51
        if ($this->lang->defaultLangSet() != $langset) {
52
            $this->lang->switchLangSet($langset);
53
        }
54
55
        $this->saveToCookie($this->app->cookie, $langset);
56
57
        return $next($request);
58
    }
59
60
    /**
61
     * 自动侦测设置获取语言选择
62
     * @access protected
63
     * @param Request $request
64
     * @return string
65
     */
66
    protected function detect(Request $request): string
67
    {
68
        // 自动侦测设置获取语言选择
69
        $langSet = '';
70
71
        if ($request->get($this->config['detect_var'])) {
72
            // url中设置了语言变量
73
            $langSet = strtolower($request->get($this->config['detect_var']));
74
        } elseif ($request->header($this->config['header_var'])) {
75
            // Header中设置了语言变量
76
            $langSet = strtolower($request->header($this->config['header_var']));
0 ignored issues
show
Bug introduced by
It seems like $request->header($this->config['header_var']) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            $langSet = strtolower(/** @scrutinizer ignore-type */ $request->header($this->config['header_var']));
Loading history...
77
        } elseif ($request->cookie($this->config['cookie_var'])) {
78
            // Cookie中设置了语言变量
79
            $langSet = strtolower($request->cookie($this->config['cookie_var']));
80
        } elseif ($request->server('HTTP_ACCEPT_LANGUAGE')) {
81
            // 自动侦测浏览器语言
82
            $match = preg_match('/^([a-z\d\-]+)/i', $request->server('HTTP_ACCEPT_LANGUAGE'), $matches);
83
            if ($match) {
84
                $langSet = strtolower($matches[1]);
85
                if (isset($this->config['accept_language'][$langSet])) {
86
                    $langSet = $this->config['accept_language'][$langSet];
87
                }
88
            }
89
        }
90
91
        if (empty($this->config['allow_lang_list']) || in_array($langSet, $this->config['allow_lang_list'])) {
92
            // 合法的语言
93
            $range = $langSet;
94
            $this->lang->setLangSet($range);
95
        } else {
96
            $range = $this->lang->getLangSet();
97
        }
98
99
        return $range;
100
    }
101
102
    /**
103
     * 保存当前语言到Cookie
104
     * @access protected
105
     * @param Cookie $cookie Cookie对象
106
     * @param string $langSet 语言
107
     * @return void
108
     */
109
    protected function saveToCookie(Cookie $cookie, string $langSet)
110
    {
111
        if ($this->config['use_cookie']) {
112
            $cookie->set($this->config['cookie_var'], $langSet);
113
        }
114
    }
115
116
}
117