-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathautoload.php
59 lines (41 loc) · 1.43 KB
/
autoload.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
<?php
/**
* Autoload classes within the namespace `willow`
*/
spl_autoload_register( function( $class ) {
// error_log( 'Autoload Class: '.$class );
// project-specific namespace prefix
$prefix = 'q\\eud\\';
/**
* Does the class being called use the namespace prefix?
*
* - Compare the first {$len} characters of the class name against our prefix
* - If no match, move to the next registered autoloader
*/
// character length of our prefix
$len = strlen( $prefix );
// if the first {$len} characters don't match
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
// error_log( 'Autoload Class Rejected, as outside namespace: '.$class );
return;
}
// base directory where our class files and folders live
$base_dir = __DIR__ . '/library/';
/**
* Perform normalizing operations on the requested class string
*
* - Remove the prefix from the class name (so that willow\Plugin looks at src/plugin.php)
* - Replace namespace separators with directory separators in the class name
* - Prepend the base directory
* - Append with .php
* - Convert to lower case
*/
$class_name = str_replace( $prefix, '', $class );
// error_log( 'Class Name: '.$class_name );
$possible_file = $base_dir . strtolower( str_replace('\\', '/', $class_name ) . '.php' );
// require the file if it exists
if( file_exists( $possible_file ) ) {
// error_log( 'Willow auto-loading: '.$possible_file );
require $possible_file;
}
});