• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / services / 9626649839

22 Jun 2024 03:31PM UTC coverage: 5.206% (-0.2%) from 5.451%
9626649839

push

github

twinh
feat(services): 增加 `Cors` 中间件

0 of 25 new or added lines in 1 file covered. (0.0%)

29 of 557 relevant lines covered (5.21%)

1.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Middleware/Cors.php
1
<?php
2

3
namespace Miaoxing\Services\Middleware;
4

5
/**
6
 * @experimental will move to wei/wei
7
 */
8
class Cors extends BaseMiddleware
9
{
10
    /**
11
     * @var string[]
12
     */
13
    protected $allowOrigins = ['*'];
14

15
    /**
16
     * @var string[]
17
     */
18
    protected $allowMethods = ['*'];
19

20
    /**
21
     * @var string[]
22
     */
23
    protected $allowHeaders = ['*'];
24

25
    /**
26
     * @var int|null
27
     */
28
    protected $maxAge = 0;
29

30
    public function __invoke($next)
31
    {
NEW
32
        $this->processAllowOriginHeader();
×
33

NEW
34
        if ($this->req->isPreflight()) {
×
NEW
35
            $this->processAllowMethods();
×
NEW
36
            $this->processAllowHeaders();
×
NEW
37
            $this->processMaxAge();
×
38
            // Response without content
NEW
39
            return;
×
40
        }
41

NEW
42
        return $next();
×
43
    }
44

45
    protected function processAllowOriginHeader()
46
    {
NEW
47
        $header = '';
×
NEW
48
        if ($this->allowOrigins === ['*']) {
×
NEW
49
            $header = '*';
×
50
        } else {
NEW
51
            $origin = $this->req->getHeader('Origin');
×
NEW
52
            if (in_array($origin, $this->allowOrigins, true)) {
×
NEW
53
                $header = $origin;
×
54
            }
55
        }
NEW
56
        if ($header) {
×
NEW
57
            $this->res->setHeader('Access-Control-Allow-Origin', $header);
×
58
        }
59
    }
60

61
    protected function processAllowMethods()
62
    {
NEW
63
        if ($this->allowMethods === ['*']) {
×
NEW
64
            $header = $this->req->getHeader('Access-Control-Request-Method');
×
65
        } else {
NEW
66
            $header = implode(', ', $this->allowMethods);
×
67
        }
NEW
68
        $this->res->setHeader('Access-Control-Allow-Methods', $header);
×
69
    }
70

71
    protected function processAllowHeaders()
72
    {
NEW
73
        if ($this->allowHeaders === ['*']) {
×
NEW
74
            $header = $this->req->getHeader('Access-Control-Request-Headers');
×
75
        } else {
NEW
76
            $header = implode(', ', $this->allowHeaders);
×
77
        }
NEW
78
        $this->res->setHeader('Access-Control-Allow-Headers', $header);
×
79
    }
80

81
    protected function processMaxAge()
82
    {
NEW
83
        if (null !== $this->maxAge) {
×
NEW
84
            $this->res->setHeader('Access-Control-Max-Age', $this->maxAge);
×
85
        }
86
    }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc