-
Notifications
You must be signed in to change notification settings - Fork 5
/
ffwp-autoload.php
74 lines (63 loc) · 1.53 KB
/
ffwp-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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/* * * * * * * * * * * * * * * * * * * * * *
* @author : Daan van den Bergh
* @url : https://daan.dev/wordpress-plugins/
* @copyright: © 2021 - 2024 Daan van den Bergh
* @license : GPL2v2 or later
* * * * * * * * * * * * * * * * * * * * * */
class FFWP_Autoloader {
/** @var string $class */
private $class;
/** @var string $file */
private $file;
/**
* FFWP_Autoloader constructor.
*
* @param $class
*/
public function __construct(
$class
) {
$this->class = $class;
}
/**
* Build filepath for requested class.
*/
public function load() {
$path = explode( '_', $this->class );
$this->file = '';
$i = 0;
if ( count( $path ) > 1 ) {
array_shift( $path );
}
end( $path );
/**
* Build directory path.
*/
while ( $i < key( $path ) ) {
$this->build( $path[ $i ], '', '/' );
$i++;
}
/**
* Build filename.
*/
$this->build( $path[ $i ], 'class', '.php' );
return $this->file;
}
/**
* Checks if $path is written uppercase entirely, otherwise it'll split $path up and build a string glued with
* dashes.
*
* @param $path
* @param string $prefix
* @param string $suffix
*/
private function build( $path, $prefix = '', $suffix = '/' ) {
if ( ctype_upper( $path ) ) {
$this->file .= ( $prefix ? $prefix . '-' : '' ) . strtolower( $path ) . $suffix;
} else {
$parts = preg_split( '/(?=[A-Z])/', lcfirst( $path ) );
$this->file .= ( $prefix ? $prefix . '-' : '' ) . strtolower( implode( '-', $parts ) ) . $suffix;
}
}
}