Completed
Push — master ( 2fa707...d4d829 )
by Sinnarasa
02:39
created

Autoload.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
4
/**
5
 * Class Autoload
6
 * @package JetFire\Autoload
7
 */
8
class Autoload {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
10
    /**
11
     * @var array
12
     */
13
    private $namespaces = [];
14
    /**
15
     * @var array
16
     */
17
    private $class_collection = [];
18
    /**
19
     * @var array
20
     */
21
    private $loaded_classes = [];
22
23
    /**
24
     * @var string
25
     */
26
    private $base_path = __DIR__;
27
28
    /**
29
     * @return array
30
     */
31
    public function getLoadedClass(){
32
        return $this->loaded_classes;
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function getNamespaces(){
39
        return $this->loaded_classes;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getClassCollection(){
46
        return $this->loaded_classes;
47
    }
48
49
    /**
50
     * @param null $base_path
51
     */
52
    public function register($base_path = null){
53
        if(!is_null($base_path))$this->base_path = rtrim($base_path,'/');
54
        spl_autoload_register(array($this,'loadClass'));
55
    }
56
57
    /**
58
     *
59
     */
60
    public function unregister()
61
    {
62
        spl_autoload_unregister(array($this, 'loadClass'));
63
    }
64
65
    /**
66
     * @param $class
67
     */
68
    private function loadClass($class){
69
        if(!isset($this->loaded_classes[$class])) {
70
            if (isset($this->class_collection[$class]) && $this->loadFile($this->class_collection[$class], $class)) return;
71
            elseif($this->findClass($class))return;
72
            else{
73
                $file = $this->base_path.'/'.str_replace('\\', '/', $class).'.php';
74
                $this->loadFile($file,$class);
75
                return;
76
            }
77
        }
78
    }
79
80
    /**
81
     * @param $file
82
     * @param $class
83
     * @return bool
84
     */
85
    private function loadFile($file,$class){
86
        if(is_file($file)){
87
            require $file;
88
            $this->loaded_classes[$class] = $file;
89
            return true;
90
        }
91
        return false;
92
    }
93
94
95
    /**
96
     * @param $class
97
     * @return bool
98
     */
99
    private function findClass($class){
100
        $prefix = $class;
101
        while (false !== $pos = strrpos($prefix, '\\')) {
102
            $prefix = substr($class, 0, $pos + 1);
103
            $relative_class = substr($class, $pos + 1);
104
            if(isset($this->namespaces[$prefix])){
105
                foreach($this->namespaces[$prefix] as $dir) {
106
                    $file = $dir . str_replace('\\', '/', $relative_class) . '.php';
107
                    if ($this->loadFile($file,$class)) return true;
108
                }
109
            }
110
            $prefix = rtrim($prefix, '\\');
111
        }
112
        return false;
113
    }
114
115
116
    /**
117
     * @param $prefix
118
     * @param $base_dirs
119
     * @param bool $prepend
120
     */
121
    public function addNamespace($prefix, $base_dirs, $prepend = false)
122
    {
123
        if(is_array($base_dirs))
124
            foreach($base_dirs as $dir)
125
                $this->addNamespace($prefix, $dir, $prepend);
126
        else{
127
            $prefix = trim($prefix, '\\').'\\';
128
            $base_dir = rtrim($base_dirs, '/').'/';
129
            if(!isset($this->namespaces[$prefix]))
130
                $this->namespaces[$prefix] = [];
131
            ($prepend)
132
                ? array_unshift($this->namespaces[$prefix], $base_dir)
133
                : array_push($this->namespaces[$prefix], $base_dir);
134
        }
135
    }
136
137
    /**
138
     * @param array $prefixes
139
     */
140
    public function setNamespaces($prefixes){
141
        if(is_string($prefixes))$prefixes = include $prefixes;
142
        if(is_array($prefixes))
143
            foreach($prefixes as $prefix => $base_dir)
144
                $this->addNamespace($prefix,$base_dir);
145
    }
146
147
    /**
148
     * @param $class
149
     * @param $path
150
     */
151
    public function addClass($class,$path){
152
        $this->class_collection[$class] = $path;
153
    }
154
155
    /**
156
     * @param array $collection
157
     */
158
    public function setClassCollection($collection){
159
        if(is_string($collection))$collection = include $collection;
160
        if(is_array($collection))
161
            $this->class_collection = array_merge($this->class_collection,$collection);
162
    }
163
}
164