-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoloader.php
76 lines (61 loc) · 1.85 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Filename autoloader.php
*
* @package dev
* @author Peter Toi <peter@petertoi.com>
*/
namespace Toi\Plugin_Name;
/**
* An example of a project-specific implementation.
*
* After registering this autoload function with SPL, the following line
* would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
* from /path/to/project/src/Baz/Qux.php:
*
* new \Foo\Bar\Baz\Qux;
*
* @param string $class The fully-qualified class name.
*
* @return void
*/
try {
\spl_autoload_register( function ( $class ) {
$namespace = __NAMESPACE__;
$allowed_file_prefixes = [
'abstract',
'class',
'interface',
'trait',
];
$base_dir = __DIR__ . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR;
// Check if the class is a member of this package.
if ( substr( $class, 0, strlen( $namespace ) ) !== $namespace ) {
// Fail if not a member.
return false;
}
$tokens = array_filter( explode( '\\', substr( $class, strlen( $namespace ) ) ) );
$tokens = array_map( function ( $token ) {
$token = strtolower( $token );
$token = str_replace( '_', '-', $token );
return $token;
}, $tokens );
$file = array_pop( $tokens );
$path = ( count( $tokens ) )
? implode( DIRECTORY_SEPARATOR, $tokens ) . DIRECTORY_SEPARATOR
: '';
foreach ( $allowed_file_prefixes as $file_prefix ) {
$filepath = "${base_dir}${path}${file_prefix}-${file}.php";
if ( file_exists( $filepath ) ) {
require_once $filepath;
}
}
return false;
} );
} catch ( \Exception $e ) {
wp_die(
__( 'File not found', '' ),
__( 'Forecast by WP Perf', '' ),
$e
);
}