-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoloader.php
71 lines (61 loc) · 1.35 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
<?php
/**
* Autoloader.
*
* @package pmc-wp-local-data-cli
*/
declare( strict_types = 1 );
namespace PMC\WP_Local_Data_CLI;
/**
* Class Autoloader.
*/
final class Autoloader {
private const NS_SEPARATOR = '\\';
/**
* Autoload plugin's classes.
*
* @param string $class_name Fully-qualified class name.
* @return void
*/
public static function do( string $class_name ): void {
if (
! str_starts_with(
$class_name,
__NAMESPACE__ . self::NS_SEPARATOR
)
) {
return;
}
$class_name = str_replace(
[
__NAMESPACE__ . self::NS_SEPARATOR,
'_',
],
[
'',
'-',
],
$class_name
);
$class_name = strtolower( $class_name );
$class_name = explode( self::NS_SEPARATOR, $class_name );
$file_key = array_key_last( $class_name );
$class_name[ $file_key ] = sprintf(
'class-%1$s.php',
$class_name[ $file_key ]
);
$class_name = implode( DIRECTORY_SEPARATOR, $class_name );
$file_path = sprintf(
'%2$s%1$sclasses%1$s%3$s',
DIRECTORY_SEPARATOR,
__DIR__,
$class_name
);
if ( is_file( $file_path ) && 0 === validate_file( $file_path ) ) {
// Path is restricted to a particular directory and validated.
// phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
require_once $file_path;
}
}
}
spl_autoload_register( [ Autoloader::class, 'do' ] );