-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoloader.php
More file actions
44 lines (39 loc) · 1.25 KB
/
autoloader.php
File metadata and controls
44 lines (39 loc) · 1.25 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
<?php
/**
* Autoloader 101
*
* @since 1.0.1
*/
spl_autoload_register( function( $class ) {
static $base_dir = null;
static $subfolders = null;
// 1. Initialize base directory and scan subfolders once
if ( $base_dir === null ) {
$base_dir = get_template_directory() . '/classes/';
$subfolders = array_filter(
scandir( $base_dir ),
function( $item ) use ( $base_dir ) {
// skip ".", ".." and keep only directories
return $item[0] !== '.' && is_dir( $base_dir . $item );
}
);
}
// 2. Extract the base class name (without namespace)
$parts = explode( '\\', $class );
$class_name = array_pop( $parts );
// 3. Normalize: lowercase and convert underscores to dashes
$normalized = str_replace( '_', '-', strtolower( $class_name ) );
// 4. Loop through each subfolder and look for class-theming-*.php
foreach ( $subfolders as $folder ) {
$file = sprintf(
'%1$s%2$s/class-theming-%3$s.php',
$base_dir,
$folder,
$normalized
);
if ( file_exists( $file ) ) {
require_once $file; // load and stop scanning
return;
}
}
} );