-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathautoload.php
67 lines (54 loc) · 1.66 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
60
61
62
63
64
65
66
67
<?php
namespace Sitewide_Sales;
defined( 'ABSPATH' ) || die( 'File cannot be accessed directly' );
/**
* This autoloader looks for class declarations as follows:
*
* Sitewide_Sales\classes\SWSales_Settings
* ^ ^ ^
* ^ ^ class-swsales-settings.php
* ^ in the /classes/ folder
* Project root
*/
function autoload( $class ) {
// project-specific namespace prefix.
$prefix = 'Sitewide_Sales\\';
// base directory for the namespace prefix.
$base_dir = __DIR__ . '/';
// does the class use the namespace prefix?
$len = strlen( $prefix );
if ( 0 !== strncmp( $prefix, $class, $len ) ) {
// no, move to the next registered autoloader.
return;
}
// get the relative class name.
$relative_class = substr( $class, $len );
$relative_class = strtolower( $relative_class );
$relative_class = str_replace( '_', '-', $relative_class );
$relative_class = prepend_class( $relative_class );
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php.
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
// if the file exists, require it.
if ( file_exists( $file ) ) {
require $file;
}
}
function prepend_class( $string ) {
$parts = explode( '\\', $string );
$new_name = '';
for ( $i = 0; $i < count( $parts ); $i++ ) {
if ( count( $parts ) - 1 === $i ) {
$new_name .= 'class-';
$new_name .= $parts[ $i ];
} else {
$new_name .= $parts[ $i ];
$new_name .= '\\';
}
}
return $new_name;
}
if ( function_exists( 'spl_autoload_register' ) ) {
spl_autoload_register( __NAMESPACE__ . '\\autoload' );
}