-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoloader.php
More file actions
45 lines (39 loc) · 875 Bytes
/
autoloader.php
File metadata and controls
45 lines (39 loc) · 875 Bytes
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
<?php
/**
* Class Autoloader 101
*
* @since 1.0.0
*/
spl_autoload_register( function( $class ) {
static $base_dir = null;
static $subfolders = null;
if ( $base_dir === null ) {
$base_dir = plugin_dir_path( __FILE__ ) . 'classes/';
$subfolders = array_filter(
scandir( $base_dir ),
function( $item ) use ( $base_dir ) {
return $item[0] !== '.' && is_dir( $base_dir . $item );
}
);
}
$parts = explode( '\\', $class );
$class_name = array_pop( $parts );
$normalized = strtolower(
str_replace( '_', '-', preg_replace( '/^Wasp_/', '', $class_name ) )
);
foreach ( $subfolders as $folder ) {
foreach ( [ 'class', 'interface' ] as $type ) {
$file = sprintf(
'%s%s/%s-wasp-%s.php',
$base_dir,
$folder,
$type,
$normalized
);
if ( file_exists( $file ) ) {
require_once $file;
return;
}
}
}
} );