File size: 3,643 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Maveriks\Util;

class ClassLoader
{
    private static $includePath = array();
    private static $includePathNs = array();
    private static $includeModelPath = array();
    private static $includeClassPath = array();
    protected static $instance;

    /**
     * Creates a new <tt>SplClassLoader</tt> that loads classes of the
     * specified namespace.
     *
     * @param string $ns The namespace to use.
     */
    public function __construct()
    {
        defined("DS") || define("DS", DIRECTORY_SEPARATOR);
        defined("NS") || define("NS", "\\");

        spl_autoload_register(array($this, 'loadClass'));
    }

    /**
     * @return \Maveriks\Util\ClassLoader
     */
    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Gets the base include path for all class files in the namespace of this class loader.
     *
     * @return string $includePath
     */
    public function getIncludePaths()
    {
        return self::$includePath;
    }

    /**
     * Uninstalls this class loader from the SPL autoloader stack.
     */
    public function unregister()
    {
        spl_autoload_unregister(array($this, 'loadClass'));
    }

    public function add($sourceDir, $namespace = "")
    {
        if (empty($namespace)) {
            self::$includePath[] = $sourceDir . (substr($sourceDir, -1) == DS ? "" : DS);
        } else {
            self::$includePathNs[$namespace] = $sourceDir . (substr($sourceDir, -1) == DS ? "" : DS);
        }
    }

    public function addModelClassPath($classPath)
    {
        self::$includeModelPath[] = $classPath;
    }

    public function addClass($class, $path)
    {
        self::$includeClassPath[strtolower($class)] = $path;
    }

    /**
     * Loads the given class or interface.
     *
     * @param string $className The name of the class to load.
     * @return void
     */
    function loadClass($className)
    {
        $classPath  = str_replace(NS, DS, $className);

        if (false !== strpos($className, NS)) {// has namespace?
            $lastPos = strpos($className, NS);
            $mainNs = substr($className, 0, $lastPos);

            if (isset(self::$includePathNs[$mainNs])) {
                if (file_exists(self::$includePathNs[$mainNs] . $classPath . ".php")) {
                    require_once self::$includePathNs[$mainNs] . $classPath . ".php";
                    return true;
                } else {
                    return false;
                }
            }
        }

        if (isset(self::$includeClassPath[strtolower($className)]) && file_exists(self::$includeClassPath[strtolower($className)])) {
            require self::$includeClassPath[strtolower($className)];
        }

        foreach (self::$includeModelPath as $path) {
            if (file_exists($path.$className.".php")) {
                require $path.$className.".php";
                return true;
            } elseif (file_exists($path."om".DS.$className.".php")) {
                require $path."om".DS.$className.".php";
                return true;
            } elseif (file_exists($path."map".DS.$className.".php")) {
                require $path."map".DS.$className.".php";
                return true;
            }
        }

        foreach (self::$includePath as $path) {
            $filename = $path . $classPath . ".php";

            if (file_exists($filename)) {
                require $filename;
                return true;
            }
        }

        return false;
    }



}