Passed
Push — master ( 6668a8...516163 )
by 世昌
02:16
created

IncludeManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

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