forked from pressbooks/pressbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hm-autoloader.php
46 lines (36 loc) · 1.17 KB
/
hm-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
<?php
namespace HM\Autoloader;
class Autoloader {
const NS_SEPARATOR = '\\';
protected $prefix;
protected $prefix_length;
protected $path;
public function __construct( $prefix, $path ) {
$this->prefix = $prefix;
$this->prefix_length = strlen( $prefix );
$this->path = trailingslashit( $path );
}
public function load( $class ) {
if ( strpos( $class, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
return;
}
// Strip prefix from the start (ala PSR-4)
$class = substr( $class, $this->prefix_length + 1 );
$class = strtolower( $class );
$file = '';
if ( false !== ( $last_ns_pos = strripos( $class, self::NS_SEPARATOR ) ) ) {
$namespace = substr( $class, 0, $last_ns_pos );
$class = substr( $class, $last_ns_pos + 1 );
$file = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
}
$file .= 'class-' . str_replace( '_', '-', $class ) . '.php';
$path = $this->path . $file;
if ( file_exists( $path ) ) {
require_once $path;
}
}
}
function register_class_path( $prefix, $path ) {
$loader = new Autoloader( $prefix, $path );
spl_autoload_register( [ $loader, 'load' ] );
}