Autoloader.php
3.78 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace AC;
use DirectoryIterator;
use FilesystemIterator;
class Autoloader {
/**
* @var self;
*/
protected static $instance;
/**
* Register prefixes and their path
* @var string[]
*/
protected $prefixes;
/**
* @var string[]
*/
protected $class_map = [];
protected function __construct() {
$this->prefixes = [];
spl_autoload_register( [ $this, 'autoload' ] );
}
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Register a prefix that should autoload
*
* @param $prefix string Unique prefix to this set of classes
* @param $dir string Path to directory where classes are stored
*
* @return $this
*/
public function register_prefix( $prefix, $dir ) {
$this->prefixes[ $prefix ] = trailingslashit( $dir );
// make sure that more specific prefixes are checked first
krsort( $this->prefixes );
return $this;
}
/**
* @param array $class_map
*
* @return $this
*/
public function register_class_map( array $class_map ) {
$this->class_map = array_merge( $this->class_map, $class_map );
// keep the classes organized for faster lookup
ksort( $this->class_map );
return $this;
}
/**
* @param $namespace
*
* @return false|string
*/
protected function get_prefix( $namespace ) {
foreach ( array_keys( $this->prefixes ) as $prefix ) {
if ( 0 === strpos( $namespace, $prefix ) ) {
return $prefix;
}
}
return false;
}
/**
* @param $prefix
*
* @return false|string
*/
protected function get_path( $prefix ) {
if ( ! isset( $this->prefixes[ $prefix ] ) ) {
return false;
}
return $this->prefixes[ $prefix ];
}
/**
* Get the path from a given namespace that has a registered prefix
*
* @param string $namespace
*
* @return false|string
*/
protected function get_path_from_namespace( $namespace ) {
$namespace = rtrim( $namespace, '\\' );
$prefix = $this->get_prefix( $namespace );
if ( ! $prefix ) {
return false;
}
$path = $this->get_path( $prefix ) . substr( $namespace, strlen( $prefix ) );
$path = str_replace( '\\', '/', $path );
return $path;
}
/**
* @param string $class
*
* @return bool
*/
public function autoload( $class ) {
$file = array_key_exists( $class, $this->class_map )
? $this->class_map[ $class ]
: realpath( $this->get_path_from_namespace( $class ) . '.php' );
if ( ! $file ) {
return false;
}
require_once $file;
return true;
}
/**
* Get list of all auto-loadable class names from a directory
*
* @param $namespace
*
* @return array
*/
public function get_class_names_from_dir( $namespace ) {
$classes = [];
$namespace = rtrim( $namespace, '\\' ) . '\\';
foreach ( $this->class_map as $class => $path ) {
// Check if it the same, but only 1 level deep
if ( strpos( $class, $namespace ) !== 0 || false !== strpos( '\\', str_replace( $namespace, '', $class ) ) ) {
continue;
}
$classes[] = $class;
}
if ( empty( $classes ) ) {
$classes = $this->get_class_names_from_filesystem( $namespace );
}
return $classes;
}
/**
* @param string $namespace
*
* @return array
*/
protected function get_class_names_from_filesystem( $namespace ) {
$classes = [];
$namespace_path = realpath( $this->get_path_from_namespace( $namespace ) );
if ( $namespace_path ) {
$iterator = new FilesystemIterator( $namespace_path, FilesystemIterator::SKIP_DOTS );
/* @var DirectoryIterator $leaf */
foreach ( $iterator as $leaf ) {
// Exclude system files
if ( 0 === strpos( $leaf->getBasename(), '.' ) ) {
continue;
}
if ( 'php' === $leaf->getExtension() ) {
$classes[] = $namespace . pathinfo( $leaf->getBasename(), PATHINFO_FILENAME );
}
}
}
return $classes;
}
}