Completed
Branch 6.0 (d30585)
by yun
06:27
created

AllowCrossDomain   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 38
ccs 10
cts 11
cp 0.9091
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 15 6
1
<?php
2
// +----------------------------------------------------------------------
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\Config;
17
use think\Request;
18
use think\Response;
19
20
/**
21
 * 跨域请求支持
22
 */
23
class AllowCrossDomain
24
{
25
    protected $cookieDomain;
26
27
    protected $header = [
28
        'Access-Control-Allow-Credentials' => 'true',
29
        'Access-Control-Allow-Methods'     => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
30
        'Access-Control-Allow-Headers'     => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With',
31
    ];
32
33 1
    public function __construct(Config $config)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
34
    {
35 1
        $this->cookieDomain = $config->get('cookie.domain', '');
36 1
    }
37
38
    /**
39
     * 允许跨域请求
40
     * @access public
41
     * @param Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
42
     * @param Closure $next
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
43
     * @param array   $header
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     * @return Response
45
     */
46 1
    public function handle($request, Closure $next, ?array $header = [])
47
    {
48 1
        $header = !empty($header) ? array_merge($this->header, $header) : $this->header;
49
50 1
        if (!isset($header['Access-Control-Allow-Origin'])) {
51 1
            $origin = $request->header('origin');
52
53 1
            if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) {
0 ignored issues
show
Bug introduced by
It seems like $origin can also be of type array; however, parameter $haystack of strpos() 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

53
            if ($origin && ('' == $this->cookieDomain || strpos(/** @scrutinizer ignore-type */ $origin, $this->cookieDomain))) {
Loading history...
54
                $header['Access-Control-Allow-Origin'] = $origin;
55
            } else {
56 1
                $header['Access-Control-Allow-Origin'] = '*';
57
            }
58
        }
59
60 1
        return $next($request)->header($header);
61
    }
62
}
63