-
Notifications
You must be signed in to change notification settings - Fork 13
/
autoloader.php
46 lines (38 loc) · 1.17 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
<?php
/**
* PSR-4 compliant autoload.
*
* @modified from https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*
* @param string $class The fully-qualified class name.
*
* @return void
*/
\spl_autoload_register(
function ( $class ) {
// project-specific namespace prefix
$prefix = 'PBT';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/inc';
// 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 );
$last_ns_pos = strripos( $relative_class, '\\' );
if ( false !== $last_ns_pos ) {
$namespace = substr( $relative_class, 0, $last_ns_pos );
$class = substr( $relative_class, $last_ns_pos + 1 );
$file = str_replace( '\\', DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
}
$file .= 'class-' . str_replace( '_', '-', $class ) . '.php';
$path = $base_dir . strtolower( $file );
// if the file exists, require it
if ( \file_exists( $path ) ) {
require $path;
}
}
);