-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoloader.php
62 lines (51 loc) · 1.82 KB
/
autoloader.php
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
<?php
class ZWPC_Plugin_Autoloader{
public function __construct(){
spl_autoload_register( array( $this, 'autoloadEngine' ) );
spl_autoload_register( array( $this, 'autoloadComponents' ) );
}
/*
-------------------------------------------------------------------------------
Engine Autoloader
-------------------------------------------------------------------------------
*/
public function autoloadEngine( $class ){
$prefix = zwpc_config( 'namespace' ) .'\\';
$base_dir = ZWPC_PATH .'engine/';
$this->locateFile( $class, $prefix, $base_dir );
}
/*
-------------------------------------------------------------------------------
Components Autoloader
-------------------------------------------------------------------------------
*/
public function autoloadComponents( $class ){
$prefix = zwpc_config( 'namespace' ) .'\\Component\\';
$base_dir = ZWPC_PATH .'components/';
$this->locateFile( $class, $prefix, $base_dir );
}
/*
-------------------------------------------------------------------------------
Locate file for autoload
-------------------------------------------------------------------------------
*/
public function locateFile( $class, $prefix, $base_dir ){
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .class.php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.class.php';
// if the file exists, require it
if (file_exists($file)) {
require_once $file;
}
}
}
new ZWPC_Plugin_Autoloader;