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

Loader::getClassPathByName()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 3
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace suda\component\loader;
3
4
use suda\component\loader\IncludeManager;
5
6
7
/**
8
 * 类自动加载器
9
 */
10
class Loader extends IncludeManager
11
{
12
    /**
13
     * 注册加载器
14
     *
15
     * @return void
16
     */
17
    public function register()
18
    {
19
        // 注册加载器
20
        spl_autoload_register(array($this, 'classLoader'));
21
    }
22
23
24
    /**
25
     * 自动类加载器
26
     *
27
     * @param string $className
28
     * @return void
29
     */
30
    public function classLoader(string $className)
31
    {
32
        if ($path = $this->getClassPath($className)) {
33
            if (!class_exists($className, false)) {
34
                @require_once $path;
35
            }
36
        }
37
    }
38
39
    /**
40
     * 获取类路径
41
     *
42
     * @param string $className
43
     * @return string|null
44
     */
45
    public  function getClassPath(string $className):?string
46
    {
47
        // 搜索路径
48
        foreach ($this->includePath as $includeNamespace => $includePaths) {
49
            foreach ($includePaths as $includePath) {
50
                if ($path = $this->getClassPathByName($includeNamespace, $includePath, $className)) {
51
                    return $path;
52
                } elseif ($path = $this->getClassPathByAlias($includePath, $className)) {
53
                    return $path;
54
                }
55
            }
56
        }
57
        return null;
58
    }
59
60
    /**
61
     * 根据类别名获取路径
62
     *
63
     * @param string $includePath
64
     * @param string $className
65
     * @return string|null
66
     */
67
    protected  function getClassPathByAlias(string $includePath, string $className):?string
68
    {
69
        $namepath=static::formatSeparator($className);
70
        $className=static::realName($className);
71
        foreach ($this->namespace as $namespace) {
72
            $path = $includePath.DIRECTORY_SEPARATOR.$namespace.DIRECTORY_SEPARATOR.$namepath.'.php';
73
            if ($path = static::realPath($path)) {
74
                // 精简类名
75
                if (!class_exists($className, false)) {
76
                    class_alias($namespace.'\\'.$className, $className);
77
                }
78
                return $path;
79
            }
80
        }
81
        return null;
82
    }
83
84
    /**
85
     * 根据类名获取路径
86
     *
87
     * @param string $includeNamespace
88
     * @param string $includePath
89
     * @param string $className
90
     * @return string|null
91
     */
92
    protected  function getClassPathByName(string $includeNamespace, string $includePath, string $className):?string
93
    {
94
        if (is_numeric($includeNamespace)) {
95
            $path = $includePath.DIRECTORY_SEPARATOR.static::formatSeparator($className).'.php';
96
        } else {
97
            $nl = strlen($includeNamespace);
98
            if (substr(static::realName($className), 0, $nl) === $includeNamespace) {
99
                $path=$includePath.DIRECTORY_SEPARATOR.static::formatSeparator(substr($className, $nl)).'.php';
100
            } else {
101
                $path=$includePath.DIRECTORY_SEPARATOR.static::formatSeparator($className).'.php';
102
            }
103
        }
104
        if ($path = static::realPath($path)) {
105
            return $path;
106
        }
107
        return null;
108
    }
109
}
110