Passed
Push — master ( f2e76c...2ddbad )
by 世昌
03:02
created

URIAttribute::getUrl()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\request\attribute;
3
4
/**
5
 * HTTP请求解析器 URI
6
 *
7
 */
8
trait URIAttribute
9
{
10
    /**
11
     * 请求URI
12
     *
13
     * @var string
14
     */
15
    protected $uri;
16
17
    /**
18
     * 请求URL
19
     *
20
     * @var string
21
     */
22
    protected $url;
23
24
    /**
25
     * 入口文件
26
     *
27
     * @var string
28
     */
29
    protected $entranceFile;
30
31
    /**
32
     * 激活URL重写
33
     *
34
     * @var boolean
35
     */
36
    protected $enabledRewrite = true;
37
    /**
38
     * 激活美化URI
39
     *
40
     * @var boolean
41
     */
42
    protected $enabledBeautifyURL = true;
43
44
    /**
45
     * 引导文件
46
     *
47
     * @var string
48
     */
49
    protected $indexFile;
50
51
    /**
52
     * 请求参数
53
     *
54
     * @var array
55
     */
56
    protected $queryParameter = [];
57
58
    /**
59
     * 文本根目录
60
     *
61
     * @var string
62
     */
63
    protected $documentRoot;
64
65
    /**
66
     * 设置URI
67
     *
68
     * @param string $uri
69
     * @return void
70
     */
71
    public function setURI(string $uri)
72
    {
73
        $this->uri = $uri;
74
    }
75
76
    /**
77
     * 设置入口文件
78
     *
79
     * @param string $entrance
80
     * @return void
81
     */
82
    public function setEntranceFile(string $entrance)
83
    {
84
        $this->entranceFile = $entrance;
85
    }
86
87
    /**
88
     * 获取请求URL
89
     *
90
     * @return string
91
     */
92
    public function getUrl(): string
93
    {
94
        return $this->url;
95
    }
96
97
    /**
98
     * 获取请求参数
99
     *
100
     * @param string|null $name
101
     * @param mixed $default
102
     * @return mixed
103
     */
104
    public function getParameter(?string $name = null, $default = null) {
105
        if (is_null($name)) {
106
            return $this->queryParameter;
107
        }
108
        return $this->queryParameter[$name] ?? $default;
109
    }
110
111
    /**
112
     * 入口文件
113
     *
114
     * @return string
115
     */
116
    public function getEntranceFile():string
117
    {
118
        if (isset($this->entranceFile)) {
119
            return $this->entranceFile;
120
        }
121
        return $this->entranceFile = \get_included_files()[0];
122
    }
123
    
124
125
    /**
126
     * 引导文件
127
     *
128
     * @return string
129
     */
130
    public function getIndexFile():string
131
    {
132
        if (isset($this->indexFile)) {
133
            return $this->indexFile;
134
        }
135
        if (strpos(__DIR__, 'phar://') === 0) {
136
            $this->indexFile = substr($this->getEntranceFile(), strlen('phar://'.$this->getDocumentRoot()));
137
        } else {
138
            $this->indexFile = substr($this->getEntranceFile(), strlen($this->getDocumentRoot()));
139
        }
140
        return $this->indexFile;
141
    }
142
143
    /**
144
     * 解析URI
145
     *
146
     * @return void
147
     */
148
    public  function apply()
149
    {
150
        $url = $this->uri;
151
152
        $queryString='';
153
        // for /?/xx
154
        if (\strpos($url, '/?/') === 0) {
155
            $url = substr($url, 2);
156
        }
157
        $indexFile = $this->getIndexFile();
158
        if (\strpos($url, $indexFile) ===0) {
159
            // for /index.php/
160
            $url = \substr($url, strlen($indexFile));// for /index.php?/
161
            if (\strpos($url, '?/') === 0) {
162
                $url = ltrim($url, '?');
163
            }
164
            // for /index.php
165
            elseif (\strpos($url, '/')!== 0) {
166
                $url = '/'.$url;
167
            }
168
        }
169
        $queryStart = \strpos($url, '?');
170
        if ($queryStart !== false) {
171
            $queryString = \substr($url, $queryStart+1);
172
            $url = \substr($url, 0, $queryStart);
173
        }
174
        $this->url = $url;
175
        if (strlen($queryString) > 0) {
176
            parse_str($queryString, $this->queryParameter);
177
        }
178
    }
179
180
    /**
181
     * Get 激活美化URI
182
     *
183
     * @return  boolean
184
     */ 
185
    public function getEnabledBeautifyURL()
186
    {
187
        return $this->enabledBeautifyURL;
188
    }
189
190
    /**
191
     * Set 激活美化URI
192
     *
193
     * @param  boolean  $enabledBeautifyURL  激活美化URI
194
     *
195
     * @return  self
196
     */ 
197
    public function setEnabledBeautifyURL(bool $enabledBeautifyURL)
198
    {
199
        $this->enabledBeautifyURL = $enabledBeautifyURL;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Get 激活URL重写
206
     *
207
     * @return  boolean
208
     */ 
209
    public function getEnabledRewrite()
210
    {
211
        return $this->enabledRewrite;
212
    }
213
214
    /**
215
     * Set 激活URL重写
216
     *
217
     * @param  boolean  $enabledRewrite  激活URL重写
218
     *
219
     * @return  self
220
     */ 
221
    public function setEnabledRewrite(bool $enabledRewrite)
222
    {
223
        $this->enabledRewrite = $enabledRewrite;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Set 文本根目录
230
     *
231
     * @param  string  $documentRoot  文本根目录
232
     *
233
     * @return  self
234
     */ 
235
    public function setDocumentRoot(string $documentRoot)
236
    {
237
        $this->documentRoot = $documentRoot;
238
239
        return $this;
240
    }
241
242
    /**
243
     * Get 文本根目录
244
     *
245
     * @return  string
246
     */ 
247
    public function getDocumentRoot()
248
    {
249
        return $this->documentRoot ?? $_SERVER['DOCUMENT_ROOT'];
250
    }
251
}
252