Passed
Push — master ( b45126...8af3f1 )
by 世昌
03:18
created

IncludeManager::requireOnce()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\framework\loader;
3
4
use function in_array;
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
     * @param string $path
41
     */
42
    public static function requireOnce(string $path)
43
    {
44
        @require_once $path;
45
    }
46
47
    /**
48
     * 获取真实或者虚拟存在的地址
49
     *
50
     * @param string $name
51
     * @return string|null
52
     */
53
    public static function realPath(string $name):?string
54
    {
55
        return Path::format($name);
56
    }
57
58
    /**
59
     * @param string $path
60
     * @param string|null $namespace
61
     */
62
    public function addIncludePath(string $path, ?string $namespace = null)
63
    {
64
        if ($path = static::realPath($path)) {
65
            $namespace = $namespace ?? 0;
66
            if (array_key_exists($namespace, $this->includePath)) {
67
                if (!in_array($path, $this->includePath[$namespace])) {
68
                    $this->includePath[$namespace][] = $path;
69
                }
70
            } else {
71
                $this->includePath[$namespace][] = $path;
72
            }
73
        }
74
    }
75
76
    public function getIncludePath()
77
    {
78
        return $this->includePath;
79
    }
80
81
    public function getNamespace()
82
    {
83
        return $this->namespace;
84
    }
85
86
    /**
87
     * @param string $namespace
88
     */
89
    public function setNamespace(string $namespace)
90
    {
91
        if (!in_array($namespace, $this->namespace)) {
92
            $this->namespace[] = $namespace;
93
        }
94
    }
95
}
96