Completed
Branch BUG-9548-transaction-completio... (b1c41e)
by
unknown
519:42 queued 503:28
created

EEH_Autoloader::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
if (!defined('EVENT_ESPRESSO_VERSION') )
3
	exit('NO direct script access allowed');
4
5
/**
6
 * EEH_Autoloader
7
 *
8
 * This is a helper utility class for setting up autoloaders.
9
 *
10
 * @package		Event Espresso
11
 * @subpackage	/helpers/EEH_Autoloader.helper.php
12
 * @author		Darren Ethier
13
 *
14
 * ------------------------------------------------------------------------
15
 */
16
class EEH_Autoloader extends EEH_Base {
17
18
19
	/**
20
	 *    instance of the EE_System object
21
	 *
22
	 * @var    $_instance
23
	 * @access    private
24
	 */
25
	private static $_instance = null;
26
27
	/**
28
	* 	$_autoloaders
29
	* 	@var array $_autoloaders
30
	* 	@access 	private
31
	*/
32
	private static $_autoloaders;
33
34
	/**
35
	 * set to "paths" to display autoloader class => path mappings
36
	 * set to "times" to display autoloader loading times
37
	 * set to "all" to display both
38
	 *
39
	 * @var string $debug
40
	 * @access    private
41
	 */
42
	public static $debug = false;
43
44
45
46
	/**
47
	 *    class constructor
48
	 *
49
	 * @access    private
50
	 * @return \EEH_Autoloader
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
51
	 */
52
	private function __construct() {
53
		if ( self::$_autoloaders === null ) {
54
			self::$_autoloaders = array();
55
			$this->_register_custom_autoloaders();
56
			spl_autoload_register( array( $this, 'espresso_autoloader' ) );
57
		}
58
	}
59
60
61
62
	/**
63
	 * @access public
64
	 * @return EEH_Autoloader
65
	 */
66
	public static function instance() {
67
		// check if class object is instantiated
68
		if ( ! self::$_instance instanceof EEH_Autoloader ) {
69
			self::$_instance = new self();
70
		}
71
		return self::$_instance;
72
	}
73
74
75
76
	/**
77
	 *    espresso_autoloader
78
	 *
79
	 * @access    public
80
	 * @param 	$class_name
81
	 * @internal  param $className
82
	 * @internal  param string $class_name - simple class name ie: session
83
	 * @return 	void
84
	 */
85
	public static function espresso_autoloader( $class_name ) {
86
		if ( isset( self::$_autoloaders[ $class_name ] ) ) {
87
			require_once( self::$_autoloaders[ $class_name ] );
88
		}
89
	}
90
91
92
93
	/**
94
	 *    register_autoloader
95
	 *
96
	 * @access    public
97
	 * @param array | string $class_paths - array of key => value pairings between class names and paths
98
	 * @param bool           $read_check true if we need to check whether the file is readable or not.
99
	 * @return void
100
	 * @throws \EE_Error
101
	 */
102
	public static function register_autoloader( $class_paths, $read_check = true ) {
103
		$class_paths = is_array( $class_paths ) ? $class_paths : array( $class_paths );
104
		foreach ( $class_paths as $class => $path ) {
105
			// don't give up! you gotta...
106
			// get some class
107
			if ( empty( $class )) {
108
				throw new EE_Error ( sprintf( __( 'No Class name was specified while registering an autoloader for the following path: %s.','event_espresso' ), $path ));
109
			}
110
			// one day you will find the path young grasshopper
111
			if ( empty( $path )) {
112
				throw new EE_Error ( sprintf( __( 'No path was specified while registering an autoloader for the %s class.','event_espresso' ), $class ));
113
			}
114
			// is file readable ?
115
			if ( $read_check && ! is_readable( $path )) {
116
				throw new EE_Error ( sprintf( __( 'The file for the %s class could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s','event_espresso' ), $class, $path ));
117
			}
118
			if ( ! isset( self::$_autoloaders[ $class ] )) {
119
				self::$_autoloaders[ $class ] = str_replace( array( '/', '\\' ), DS, $path );
120
				if ( EE_DEBUG && ( EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' ) ) {
121
					EEH_Debug_Tools::printr( self::$_autoloaders[ $class ], $class, __FILE__, __LINE__ );
122
				}
123
			}
124
		}
125
	}
126
127
128
129
130
	/**
131
	 * 	get_autoloaders
132
	 *
133
	 * 	@access public
134
	 * 	@return array
135
	 */
136
	public static function get_autoloaders() {
137
		return self::$_autoloaders;
138
	}
139
140
141
142
143
	/**
144
	 * 	register core, model and class 'autoloaders'
145
	 *
146
	 * 	@access private
147
	 * 	@return void
148
	 */
149
	private function _register_custom_autoloaders() {
150
		EEH_Autoloader::$debug = '';
151
		\EEH_Autoloader::register_helpers_autoloaders();
152
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_CORE . 'interfaces' );
153
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_CORE );
154
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_INTERFACES, true );
155
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_MODELS, true );
156
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_CLASSES );
157
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_FORM_SECTIONS, true );
158
		if ( EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all' ) {
159
			EEH_Debug_Tools::instance()->show_times();
160
		}
161
	}
162
163
164
165
	/**
166
	 *    register core, model and class 'autoloaders'
167
	 *
168
	 * @access public
169
	 */
170
	public static function register_helpers_autoloaders() {
171
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_HELPERS );
172
	}
173
174
175
176
177
	/**
178
	 * 	register core, model and class 'autoloaders'
179
	 *
180
	 * 	@access public
181
	 * 	@return void
182
	 */
183
	public static function register_form_sections_autoloaders() {
184
		//EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_FORM_SECTIONS, true );
185
	}
186
187
188
189
190
	/**
191
	 * 	register core, model and class 'autoloaders'
192
	 *
193
	 * 	@access public
194
	 * 	@return void
195
	 */
196
	public static function register_line_item_display_autoloaders() {
197
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(  EE_LIBRARIES . 'line_item_display' , true );
198
	}
199
200
201
202
203
	/**
204
	 * 	register core, model and class 'autoloaders'
205
	 *
206
	 * 	@access public
207
	 * 	@return void
208
	 */
209
	public static function register_line_item_filter_autoloaders() {
210
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(  EE_LIBRARIES . 'line_item_filters' , true );
211
	}
212
213
214
215
216
	/**
217
	 * 	register template part 'autoloaders'
218
	 *
219
	 * 	@access public
220
	 * 	@return void
221
	 */
222
	public static function register_template_part_autoloaders() {
223
		EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_LIBRARIES . 'template_parts', true );
224
	}
225
226
227
228
	/**
229
	 * Assumes all the files in this folder have the normal naming scheme (namely that their classname
230
	 * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list.
231
	 * If that's not the case, you'll need to improve this function or just use EEH_File::get_classname_from_filepath_with_standard_filename() directly.
232
	 * Yes this has to scan the directory for files, but it only does it once -- not on EACH
233
	 * time the autoloader is used
234
	 *
235
	 * @param string $folder name, with or without trailing /, doesn't matter
236
	 * @param bool   $recursive
237
	 * @return void
238
	 * @throws \EE_Error
239
	 */
240
	public static function register_autoloaders_for_each_file_in_folder( $folder, $recursive = false){
241 View Code Duplication
		if ( EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all' ) {
242
			EEH_Debug_Tools::instance()->start_timer( basename( $folder ) );
243
		}
244
		// make sure last char is a /
245
		$folder .= $folder[strlen($folder)-1] !== DS ? DS : '';
246
		$class_to_filepath_map = array();
247
		$exclude = array( 'index' );
248
		//get all the files in that folder that end in php
249
		$filepaths = glob( $folder.'*');
250
251
		if ( empty( $filepaths ) ) {
252
			return;
253
		}
254
255
		foreach( $filepaths as $filepath ) {
256
			if ( substr( $filepath, -4, 4 ) === '.php' ) {
257
				$class_name = EEH_File::get_classname_from_filepath_with_standard_filename( $filepath );
258
				if ( ! in_array( $class_name, $exclude )) {
259
					$class_to_filepath_map [ $class_name ] = $filepath;
260
				}
261
			} else if ( $recursive ) {
262
				EEH_Autoloader::register_autoloaders_for_each_file_in_folder( $filepath, $recursive );
263
			}
264
		}
265
		// we remove the necessity to do a is_readable() check via the $read_check flag because glob by nature will not return non_readable files/directories.
266
		self::register_autoloader( $class_to_filepath_map, false );
267 View Code Duplication
		if ( EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all' ) {
268
			EEH_Debug_Tools::instance()->stop_timer( basename( $folder ) );
269
		}
270
	}
271
272
273
274
275
276
}
277