Passed
Push — master ( c1447f...6911a9 )
by 世昌
01:47
created

Header   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sendHeader() 0 3 1
A send() 0 9 3
A __construct() 0 2 1
A addHeaderRaw() 0 3 1
A sendHeaderRaw() 0 7 2
A addHeader() 0 3 1
A instance() 0 6 2
A sended() 0 2 1
1
<?php
2
namespace nebula\application\response\provider;
3
4
/**
5
 * 响应头提供器
6
 */
7
class Header
8
{
9
10
    /**
11
     * 静态实例
12
     *
13
     * @var self
14
     */
15
    protected static $instance;
16
17
18
    protected function __construct()
19
    {
20
      
21
    }
22
23
    /**
24
     * 返回实例
25
     *
26
     * @return self
27
     */
28
    public static function instance()
29
    {
30
        if (isset(static::$instance)) {
31
            return static::$instance;
32
        }
33
        return static::$instance = new static;
34
    }
35
36
    /**
37
     * 头部信息
38
     *
39
     * @var array
40
     */
41
    protected $header = [];
42
43
    /**
44
     * 添加头部值
45
     *
46
     * @param string $name
47
     * @param string $content
48
     * @return void
49
     */
50
    public function addHeader(string $name, string $content)
51
    {
52
        $this->addHeaderRaw(trim($name).': '.trim($content));
53
    }
54
55
    /**
56
     * 添加头部
57
     *
58
     * @param string $content
59
     * @return void
60
     */
61
    public function addHeaderRaw(string $content)
62
    {
63
        $this->header[] = trim($content);
64
    }
65
66
    /**
67
     * 直接发送头部信息
68
     *
69
     * @param string $name
70
     * @param string $content
71
     * @param boolean $replace
72
     * @return boolean
73
     */
74
    public function sendHeader(string $name, string $content, bool $replace = true)
75
    {
76
        return $this->sendHeaderRaw(trim($name).': '.trim($content), $replace);
77
    }
78
79
    /**
80
     * 发送原始头部
81
     *
82
     * @param string $content
83
     * @param boolean $replace
84
     * @return bool
85
     */
86
    public function sendHeaderRaw(string $content, bool $replace = true)
87
    {
88
        if (!$this->sended()) {
89
            header($content, $replace);
90
            return true;
91
        } 
92
        return false;
93
    }
94
95
    /**
96
     * 发送头部信息
97
     *
98
     * @return boolean
99
     */
100
    public function send():bool
101
    {
102
        if ($this->sended()) {
103
            return false;
104
        }
105
        foreach ($this->header as $value) {
106
            $this->sendHeaderRaw($value);
107
        }
108
        return true;
109
    }
110
111
112
    public function sended():bool {
113
        return headers_sent();
114
    }
115
}
116