-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoloader.php
54 lines (43 loc) · 1.46 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
<?php
/**
* Plugin Autoloader
*
* @package Launch With Words
*/
/**
* Register Autoloader
*/
spl_autoload_register(
function ( $class ) {
// Assume we're using namespaces (because that's how the plugin is structured).
$namespace = explode( '\\', $class );
$root = array_shift( $namespace );
// If a class ends with "Trait" then prefix the filename with 'trait-', else use 'class-'.
$class_trait = preg_match( '/Trait$/', $class ) ? 'trait-' : 'class-';
// If we're not in the plugin's namespace then just return.
if ( 'LWW' !== $root ) {
return;
}
// Class name is the last part of the FQN.
$class_name = array_pop( $namespace );
// Remove "Trait" from the class name.
if ( 'trait-' === $class_trait ) {
$class_name = str_replace( 'Trait', '', $class_name );
}
$filename = $class_trait . $class_name . '.php';
// For file naming, the namespace is everything but the class name and the root namespace.
$namespace = trim( implode( DIRECTORY_SEPARATOR, $namespace ) );
// Because WordPress file naming conventions are odd.
$filename = strtolower( str_replace( '_', '-', $filename ) );
$namespace = strtolower( str_replace( '_', '-', $namespace ) );
// Get the path to our files.
$directory = dirname( __FILE__ );
if ( ! empty( $namespace ) ) {
$directory .= DIRECTORY_SEPARATOR . $namespace;
}
$file = $directory . DIRECTORY_SEPARATOR . $filename;
if ( file_exists( $file ) ) {
require_once $file;
}
}
);