This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
class-wppr-autoloader.php
222 lines (200 loc) · 5.62 KB
/
class-wppr-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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* The file that defines autoload class
*
* A simple autoloader that loads class files recursively starting in the directory
* where this class resides. Additional options can be provided to control the naming
* convention of the class files.
*
* @link https://themeisle.com
* @copyright Copyright (c) 2017, Bogdan Preda
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*
* @since 1.0.0
* @package WPPR
*/
/**
* The WPPR_Autoloader class.
*
* @since 3.0.0
* @package WPPR
* @author Themeisle <[email protected]>
*/
class WPPR_Autoloader {
/**
* File extension as a string. Defaults to ".php".
*
* @since 3.0.0
* @access protected
* @var string $file_ext The file extension to look for.
*/
protected static $file_ext = '.php';
/**
* The top level directory where recursion will begin. Defaults to the current
* directory.
*
* @since 3.0.0
* @access protected
* @var string $path_top The root directory.
*/
protected static $path_top = __DIR__;
/**
* Holds an array of namespaces to filter in autoloading if set.
*
* @since 3.0.0
* @access protected
* @var array $namespaces The namespace array, used if not empty on autoloading.
*/
protected static $namespaces = array();
/**
* An array of files to exclude when looking to autoload.
*
* @since 3.0.0
* @access protected
* @var array $excluded_files The excluded files list.
*/
protected static $excluded_files = array( 'node_modules' );
/**
* A placeholder to hold the file iterator so that directory traversal is only
* performed once.
*
* @since 3.0.0
* @access protected
* @var RecursiveIteratorIterator $file_iterator Holds an instance of the iterator class.
*/
protected static $file_iterator = null;
/**
* Autoload function for registration with spl_autoload_register
*
* Looks recursively through project directory and loads class files based on
* filename match.
*
* @since 3.0.0
* @access public
*
* @param string $class_name The class name requested.
*
* @return mixed
*/
public static function loader( $class_name ) {
if ( ! empty( static::$namespaces ) ) {
$found = static::check_namespaces( $class_name );
if ( ! $found ) {
return $found;
}
}
$directory = new RecursiveDirectoryIterator( static::$path_top . DIRECTORY_SEPARATOR . 'includes', RecursiveDirectoryIterator::SKIP_DOTS );
require_once 'class-wppr-recursive-filter.php';
if ( is_null( static::$file_iterator ) ) {
$iterator = new RecursiveIteratorIterator(
new Wppr_Recursive_Filter(
$directory,
array(
'WPPR_Autoloader',
'filter_excluded_files',
)
)
);
$regex = new RegexIterator( $iterator, '/^.+\.php$/i', RecursiveRegexIterator::MATCH );
static::$file_iterator = iterator_to_array( $regex, false );
}
$filename = 'class-' . str_replace( '_', '-', strtolower( $class_name ) ) . static::$file_ext;
foreach ( static::$file_iterator as $file ) {
if ( strtolower( $file->getFileName() ) === strtolower( $filename ) && is_readable( $file->getPathName() ) ) {
require( $file->getPathName() );
return true;
}
}
}
/**
* Method to check in allowed namespaces.
*
* @since 3.0.0
* @access protected
*
* @param string $class_name the class name to check with the namespaces.
*
* @return bool
*/
protected static function check_namespaces( $class_name ) {
$found = false;
foreach ( static::$namespaces as $namespace ) {
if ( substr( $class_name, 0, strlen( $namespace ) ) === $namespace ) {
$found = true;
}
}
return $found;
}
/**
* Sets the $file_ext property
*
* @since 3.0.0
* @access public
*
* @param string $file_ext The file extension used for class files. Default is "php".
*/
public static function set_file_ext( $file_ext ) {
static::$file_ext = $file_ext;
}
/**
* Sets the $plugins_path property
*
* @since 3.0.0
* @access public
*
* @param string $path The path representing the top level where recursion should
* begin for plugins. Defaults to empty ( does not look in plugins ).
*/
public static function set_plugins_path( $path ) {
static::$plugins_path = $path;
}
/**
* Sets the $path property
*
* @since 3.0.0
* @access public
*
* @param string $path The path representing the top level where recursion should
* begin. Defaults to the current directory.
*/
public static function set_path( $path ) {
static::$path_top = $path;
}
/**
* Adds a new file to the exclusion list.
*
* @since 3.0.0
* @access public
*
* @param string $file_name The file name to exclude from autoload.
*/
public static function exclude_file( $file_name ) {
static::$excluded_files[] = $file_name;
}
/**
* Sets the namespaces used in autoloading if any.
*
* @since 3.0.0
* @access public
*
* @param array $namespaces The namespaces to use.
*/
public static function define_namespaces( $namespaces = array() ) {
static::$namespaces = $namespaces;
}
/**
* Utility to filter out the excluded directories.
*
* @param \SplFileInfo $file The file info array.
* @param string $key File key.
* @param \RecursiveDirectoryIterator $iterator The recursive directory iterator.
*
* @return bool
*/
public static function filter_excluded_files( \SplFileInfo $file, $key, \RecursiveDirectoryIterator $iterator ) {
if ( ! in_array( $file->getFilename(), static::$excluded_files, true ) ) {
return true;
}
return false;
}
}