Passed
Push — master ( c547da...5d2787 )
by 世昌
02:10 queued 15s
created

IncludeManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setNamespace() 0 4 2
A addIncludePath() 0 10 4
A realName() 0 3 1
A getIncludePath() 0 3 1
A realPath() 0 3 1
A import() 0 14 4
A getNamespace() 0 3 1
1
<?php
2
namespace suda\component\loader;
3
4
use suda\component\loader\Path;
5
use suda\component\loader\PathTrait;
6
7
/**
8
 * 包含管理器
9
 *
10
 */
11
class IncludeManager implements PathInterface
12
{
13
    use PathTrait;
14
    /**
15
     * 默认命名空间
16
     *
17
     * @var array
18
     */
19
    protected $namespace=[ __NAMESPACE__ ];
20
21
    /**
22
     * 包含路径
23
     *
24
     * @var array
25
     */
26
    protected $includePath=[];
27
28
    /**
29
     * 将JAVA,路径分割转换为PHP分割符
30
     *
31
     * @param string $name 类名
32
     * @return string 真实分隔符
33
     */
34
    public static function realName(string $name):string
35
    {
36
        return str_replace(['.','/'], '\\', $name);
37
    }
38
39
    /**
40
     * 获取真实或者虚拟存在的地址
41
     *
42
     * @param string $name
43
     * @return string|null
44
     */
45
    public static function realPath(string $name):?string
46
    {
47
        return Path::format($name);
48
    }
49
50
    /**
51
     * 导入文件
52
     *
53
     * @param string $filename
54
     * @return string|null
55
     */
56
    public function import(string $filename):?string
57
    {
58
        if ($filename = static::realPath($filename)) {
59
            @require_once $filename;
60
            return $filename;
61
        } else {
62
            foreach ($this->includePath[0] as $includePath) {
63
                if ($path = static::realPath($includePath.DIRECTORY_SEPARATOR.$filename)) {
64
                    @require_once $path;
65
                    return $path;
66
                }
67
            }
68
        }
69
        return null;
70
    }
71
72
    public function addIncludePath(string $path, string $namespace=null)
73
    {
74
        if ($path = static::realPath($path)) {
75
            $namespace = $namespace ?? 0;
76
            if (array_key_exists($namespace, $this->includePath)) {
77
                if (!\in_array($path, $this->includePath[$namespace])) {
78
                    $this->includePath[$namespace][]=$path;
79
                }
80
            } else {
81
                $this->includePath[$namespace][]=$path;
82
            }
83
        }
84
    }
85
86
    public function getIncludePath()
87
    {
88
        return $this->includePath;
89
    }
90
91
    public function getNamespace()
92
    {
93
        return $this->namespace;
94
    }
95
96
    public function setNamespace(string $namespace)
97
    {
98
        if (!in_array($namespace, $this->namespace)) {
99
            $this->namespace[]=$namespace;
100
        }
101
    }
102
}
103