-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathautoloader.php
46 lines (39 loc) · 1.03 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
<?php
/**
* Autoloader, inspired by PSR4.
*
* @package WPForms
* @author WPForms
* @since 1.4.7
* @license GPL-2.0+
* @copyright Copyright (c) 2018, WPForms LLC
*/
/**
* Register the autoloader.
*
* @since 1.4.7
*
* @param string $class
*/
spl_autoload_register( function ( $class ) {
// Our base namespace for all plugin classes.
$prefix = 'WPForms\\';
// Does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
// No, move to the next registered autoloader.
return;
}
// Base directory for the namespace prefix.
$base_dir = __DIR__ . '/src/';
// Get the relative class name.
$relative_class = substr( $class, $len );
// 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';
// Finally, require the file.
if ( file_exists( $file ) ) {
require_once $file;
}
} );