Test Failed
Push — master ( c4920f...241378 )
by 世昌
02:23
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
    /**
39
     * 激活美化Uri
40
     *
41
     * @var boolean
42
     */
43
    protected $enabledBeautifyUrl = false;
44
45
    /**
46
     * 基本默认入口
47
     *
48
     * @var array
49
     */
50
    protected static $defaultIndexs = ['index.php'];
51
52
    /**
53
     * 引导文件
54
     *
55
     * @var string
56
     */
57
    protected $indexFile;
58
59
    /**
60
     * 请求参数
61
     *
62
     * @var array
63
     */
64
    protected $queryParameter = [];
65
66
    /**
67
     * 文本根目录
68
     *
69
     * @var string
70
     */
71
    protected $documentRoot;
72
73
    /**
74
     * 基本Uri
75
     *
76
     * @var string
77
     */
78
    protected $basicUri;
79
80
    /**
81
     * 设置Uri
82
     *
83
     * @param string $Uri
84
     * @return void
85
     */
86
    public function setUri(string $Uri)
87
    {
88
        $this->Uri = $Uri;
89
    }
90
91
    /**
92
     * 设置入口文件
93
     *
94
     * @param string $entrance
95
     * @return void
96
     */
97
    public function setEntranceFile(string $entrance)
98
    {
99
        $this->entranceFile = $entrance;
100
    }
101
102
    /**
103
     * 获取请求Url
104
     *
105
     * @return string
106
     */
107
    public function getUrl(): string
108
    {
109
        return $this->Url;
110
    }
111
112
    /**
113
     * 获取请求参数
114
     *
115
     * @param string|null $name
116
     * @param mixed $default
117
     * @return mixed
118
     */
119
    public function getParameter(?string $name = null, $default = null) {
120
        if (is_null($name)) {
121
            return $this->queryParameter;
122
        }
123
        return $this->queryParameter[$name] ?? $default;
124
    }
125
126
    /**
127
     * 入口文件
128
     *
129
     * @return string
130
     */
131
    public function getEntranceFile():string
132
    {
133
        if (isset($this->entranceFile)) {
134
            return $this->entranceFile;
135
        }
136
        return $this->entranceFile = \get_included_files()[0];
137
    }
138
    
139
140
    /**
141
     * 引导文件
142
     *
143
     * @return string
144
     */
145
    public function getIndexFile():string
146
    {
147
        if (isset($this->indexFile)) {
148
            return $this->indexFile;
149
        }
150
        if (strpos(__DIR__, 'phar://') === 0) {
151
            $indexFile = substr($this->getEntranceFile(), strlen('phar://'.$this->getDocumentRoot()));
152
        } else {
153
            $indexFile = substr($this->getEntranceFile(), strlen($this->getDocumentRoot()));
154
        }
155
        return $this->indexFile = str_replace('\\', '/' ,$indexFile);
156
    }
157
158
    /**
159
     * 应用解析Uri
160
     *
161
     * @param boolean $applyServer 应用到服务环境
162
     * @return void
163
     */
164
    public  function apply(bool $applyServer = false)
165
    {
166
        $Url = $this->Uri;
167
168
        $queryString='';
169
        // for /?/xx
170
        if (\strpos($Url, '/?/') === 0) {
171
            $Url = substr($Url, 2);
172
        }
173
        $indexFile = $this->getIndexFile();
174
        if (\strpos($Url, $indexFile) ===0) {
175
            // for /index.php/
176
            $Url = \substr($Url, strlen($indexFile));// for /index.php?/
177
            if (\strpos($Url, '?/') === 0) {
178
                $Url = ltrim($Url, '?');
179
            }
180
            // for /index.php
181
            elseif (\strpos($Url, '/')!== 0) {
182
                $Url = '/'.$Url;
183
            }
184
        }
185
        $queryStart = \strpos($Url, '?');
186
        if ($queryStart !== false) {
187
            $queryString = \substr($Url, $queryStart+1);
188
            $Url = \substr($Url, 0, $queryStart);
189
        }
190
        $this->Url = $Url;
191
        if (strlen($queryString) > 0) {
192
            parse_str($queryString, $this->queryParameter);
193
        }
194
        if ($applyServer) {
195
            $_SERVER['PATH_INFO'] =  $this->Url;
196
            $_SERVER['SCRIPT_NAME'] = $this->getIndexFile();
197
            $_SERVER['PHP_SELF'] = $this->getIndexFile();
198
            $_GET = $this->queryParameter;
199
        }
200
    }
201
202
    /**
203
     * 获取基础Uri
204
     *
205
     * @return string
206
     */
207
    public function getBasicUri():string {
208
        if (isset($this->basicUri)) {
209
            return $this->basicUri;
210
        }
211
        $serverBasicUri = $this->getServerUri();
212
        $indexFile = $this->getIndexFile();
213
        $isWindows = DIRECTORY_SEPARATOR ===  '\\';
214
        $isRoot= in_array(substr($indexFile, 1), static::getDefaultIndexs());
215
        if ($this->enabledRewrite && $isRoot) {
216
            if ($isWindows && !$this->enabledBeautifyUrl) {
217
                return $this->basicUri = $serverBasicUri.'/?/';
218
            }
219
            return $this->basicUri = $serverBasicUri.'/';
220
        }
221
        return $this->basicUri = $serverBasicUri.$indexFile.'/';
222
    }
223
224
    /**
225
     * Get 激活美化Uri
226
     *
227
     * @return  boolean
228
     */ 
229
    public function getEnabledBeautifyUrl()
230
    {
231
        return $this->enabledBeautifyUrl;
232
    }
233
234
    /**
235
     * Set 激活美化Uri
236
     *
237
     * @param  boolean  $enabledBeautifyUrl  激活美化Uri
238
     *
239
     * @return  self
240
     */ 
241
    public function setEnabledBeautifyUrl(bool $enabledBeautifyUrl)
242
    {
243
        $this->enabledBeautifyUrl = $enabledBeautifyUrl;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Get 激活Url重写
250
     *
251
     * @return  boolean
252
     */ 
253
    public function getEnabledRewrite()
254
    {
255
        return $this->enabledRewrite;
256
    }
257
258
    /**
259
     * Set 激活Url重写
260
     *
261
     * @param  boolean  $enabledRewrite  激活Url重写
262
     *
263
     * @return  self
264
     */ 
265
    public function setEnabledRewrite(bool $enabledRewrite)
266
    {
267
        $this->enabledRewrite = $enabledRewrite;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Set 文本根目录
274
     *
275
     * @param  string  $documentRoot  文本根目录
276
     *
277
     * @return  self
278
     */ 
279
    public function setDocumentRoot(string $documentRoot)
280
    {
281
        $this->documentRoot = $documentRoot;
282
283
        return $this;
284
    }
285
286
    /**
287
     * Get 文本根目录
288
     *
289
     * @return  string
290
     */ 
291
    public function getDocumentRoot()
292
    {
293
        return $this->documentRoot ?? $_SERVER['DOCUMENT_ROOT'];
294
    }
295
296
    public abstract function getServerUri();
297
298
    /**
299
     * Get 基本默认入口
300
     *
301
     * @return  array
302
     */ 
303
    public static function getDefaultIndexs()
304
    {
305
        return static::$defaultIndexs;
306
    }
307
308
    /**
309
     * Set 基本默认入口
310
     *
311
     * @param  array  $defaultIndexs  基本默认入口
312
     *
313
     * @return  self
314
     */ 
315
    public static function setDefaultIndexs(array $defaultIndexs)
316
    {
317
        static::$defaultIndexs = $defaultIndexs;
318
    }
319
}
320