Passed
Branch sudav3 (acb061)
by 世昌
02:16
created

IndexFinder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexFile() 0 11 3
A getDocumentRoot() 0 3 1
A getEntranceFile() 0 3 1
A __construct() 0 4 1
1
<?php
2
namespace suda\framework\server\request;
3
4
/**
5
 * HTTP 入口解析查找
6
 */
7
class IndexFinder
8
{
9
    /**
10
     * 入口文件
11
     *
12
     * @var string
13
     */
14
    protected $indexFile;
15
16
    /**
17
     * 文本根目录
18
     *
19
     * @var string
20
     */
21
    protected $documentRoot;
22
23
    /**
24
     * 入口文件
25
     *
26
     * @var string
27
     */
28
    protected $entranceFile;
29
30
    public function __construct(?string $entranceFile = null, ?string $documentRoot = null)
31
    {
32
        $this->entranceFile = $entranceFile ?? \get_included_files()[0];
33
        $this->documentRoot = $documentRoot ?? $_SERVER['DOCUMENT_ROOT'];
34
    }
35
36
37
    /**
38
     * 入口文件
39
     *
40
     * @return string
41
     */
42
    private function getEntranceFile():string
43
    {
44
        return $this->entranceFile;
45
    }
46
47
    /**
48
     * 引导文件
49
     *
50
     * @return string
51
     */
52
    public function getIndexFile():string
53
    {
54
        if (isset($this->indexFile)) {
55
            return $this->indexFile;
56
        }
57
        if (strpos(__DIR__, 'phar://') === 0) {
58
            $indexFile = substr($this->getEntranceFile(), strlen('phar://'.$this->getDocumentRoot()));
59
        } else {
60
            $indexFile = substr($this->getEntranceFile(), strlen($this->getDocumentRoot()));
61
        }
62
        return $this->indexFile = str_replace('\\', '/', $indexFile);
63
    }
64
65
    /**
66
     * Get 文本根目录
67
     *
68
     * @return  string
69
     */
70
    public function getDocumentRoot()
71
    {
72
        return $this->documentRoot;
73
    }
74
}
75