-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoload.php
57 lines (47 loc) · 1.42 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
<?php
/**
* The PSR-4 autoloader for the Wapuus API plugin.
*
* @package Wapuus_API
* @author Glauber Silva <[email protected]>
* @link https://glaubersilva.me/
*/
defined( 'ABSPATH' ) || exit;
/**
* Register autoload.
*
* Based in the PSR-4 autoloader example found here:
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(
function ( $class ) {
// The project-specific namespace prefix.
$prefix = 'Wapuus_API\\';
// Base directory for the namespace prefix.
$base_dir = trailingslashit( dirname( __FILE__ ) );
// Does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
// No, move to the next registered autoloader.
return;
}
$relative_class = substr( $class, $len ) . '.php';
$path = explode( '\\', strtolower( str_replace( '_', '-', $relative_class ) ) );
$file = array_pop( $path );
if ( strpos( strtolower( $relative_class ), 'trait' ) !== false ) {
$file = 'trait-' . $file;
} elseif ( strpos( strtolower( $relative_class ), 'interface' ) !== false ) {
$file = 'interface-' . $file;
} else {
$file = 'class-' . $file;
}
$file = $base_dir . implode( '/', $path ) . '/' . $file;
// if the file exists, require it.
if ( file_exists( $file ) ) {
require $file;
}
}
);