PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.9
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.9
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / vendor / wpdeveloper / lib-utility / lib / class-loader.php

class-loader.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.9, at vendor/wpdeveloper/lib-utility/lib/class-loader.php

446 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PHP Loader
4 *
5 * @namespace UsabilityDynamics
6 * @module Utility
7 * @version 0.2.3
8 * @author potanin@UD
9 */
10 namespace UsabilityDynamics {
11
12 if( !class_exists( 'UsabilityDynamics\Loader' ) ) {
13
14 /**
15 * Loader implements a PSR-0 class loader
16 *
17 * $loader = new \UsabilityDynamics\Loader();
18 *
19 * // register classes with namespaces
20 * $loader->add( 'Symfony', __DIR__ . '/framework' );
21 *
22 * // register classes with namespaces by passing an array
23 * $loader->add( array(
24 * 'UsabilityDynamics\\' => __DIR__ . '/usabilitydynamics'
25 * 'JsonSchema\\' => __DIR__ . '/jsonschema/src'
26 * ));
27 *
28 * // activate the autoloader
29 * $loader->register();
30 *
31 * This class is loosely based on the Symfony UniversalClassLoader.
32 *
33 * @class Loader
34 * @author potanin@UD
35 */
36 class Loader {
37
38 /**
39 * Loader Class version.
40 *
41 * @property $version
42 * @type {Object}
43 */
44 public static $version = '0.0.3';
45
46 /**
47 * Extra header parameters.
48 *
49 * @property $headers
50 * @type {Object}
51 */
52 public static $headers = array(
53 'theme' => array(
54 'Name' => 'Theme Name',
55 'ThemeURI' => 'Theme URI',
56 'Description' => 'Description',
57 'Author' => 'Author',
58 'AuthorURI' => 'Author URI',
59 'Version' => 'Version',
60 'Template' => 'Template',
61 'Status' => 'Status',
62 'Tags' => 'Tags'
63 )
64 );
65
66 /**
67 * Configuration.
68 *
69 * @property $settings
70 * @type {Object}
71 */
72 public $settings = array();
73
74 /**
75 * Array with fallback directories for auto-loading.
76 *
77 * @property $fallback_directories
78 * @type {Object}
79 */
80 public $fallback_directories = array();
81
82 /**
83 * Array of stored namespace prefixes.
84 *
85 * @property $prefixes
86 * @type {Object}
87 */
88 public $prefixes = array();
89
90 /**
91 * Array of stored class mappings.
92 *
93 * @property $class_map
94 * @type {Object}
95 */
96 public $class_map = array();
97
98 /**
99 * Constructor for the Loader class.
100 *
101 * @method __construct
102 * @for Loader
103 * @constructor
104 *
105 * @param $settings array
106 *
107 * @return \UsabilityDynamics\Loader
108 * @version 0.0.2
109 * @since 0.0.2
110 */
111 function __construct( $settings = array() ) {
112
113 // Save Loader Settings.
114 $this->settings = json_decode( json_encode( $settings ) );
115
116 // Load libraries that use namespaces.
117 if( isset( $this->settings->controllers ) ) {
118 $this->set_namespace( $this->settings->controllers );
119 }
120
121 // Loads libraries that do not use namespaces.
122 if( isset( $this->settings->helpders ) ) {
123 $this->add_class_map( $this->settings->helpers );
124 }
125
126 // Register Autoloader.
127 $this->register( true );
128
129 // Prepare Filters.
130 add_filter( 'template_redirect', array( $this, 'template_redirect' ) );
131
132 // Utility.
133 add_filter( 'extra_theme_headers', array( $this, 'extra_theme_headers' ) );
134
135 // @chainable
136 return $this;
137
138 }
139
140 /**
141 * Fronted Setup
142 *
143 * @return $this
144 *
145 * @method template_redirect
146 * @for Loader
147 *
148 * @author potanin@UD
149 * @version 0.0.2
150 * @since 0.0.2
151 */
152 function template_redirect() {
153
154 // Unrefister Autoloader.
155 $this->unregister();
156
157 // @chainable
158 return $this;
159
160 }
161
162 /**
163 * Add Color Scheme
164 *
165 * @method extra_theme_headers
166 * @for Loader
167 *
168 * @author potanin@UD
169 * @version 0.0.2
170 * @since 0.0.2
171 */
172 function extra_theme_headers() {
173 return (array) $this->headers;
174 }
175
176 /**
177 * Add Class Map
178 *
179 * @param array $class_map Class to filename map
180 *
181 * @return $this
182 *
183 * @method add_class_map
184 * @for Loader
185 *
186 * @author potanin@UD
187 * @version 0.0.2
188 * @since 0.0.2
189 */
190 public function add_class_map( array $class_map ) {
191
192 if( $this->class_map ) {
193 $this->class_map = array_merge( $this->class_map, $class_map );
194 } else {
195 $this->class_map = $class_map;
196 }
197
198 return $this;
199
200 }
201
202 /**
203 * Registers a set of classes, merging with any others previously set.
204 *
205 * @param string $prefix The classes prefix
206 * @param array|string $paths The location(s) of the classes
207 * @param bool $prepend Prepend the location(s)
208 *
209 * @return $this
210 *
211 * @method add_class
212 * @for Loader
213 *
214 * @author potanin@UD
215 * @version 0.0.2
216 * @since 0.0.2
217 */
218 public function add_class( $prefix, $paths, $prepend = false ) {
219
220 if( !$prefix ) {
221 if( $prepend ) {
222 $this->fallback_directories = array_merge( (array) $paths, (array) $this->fallback_directories );
223 } else {
224 $this->fallback_directories = array_merge( (array) $this->fallback_directories, (array) $paths );
225 }
226
227 return $this;
228 }
229
230 $first = $prefix[ 0 ];
231
232 if( !isset( $this->prefixes[ $first ][ $prefix ] ) ) {
233 $this->prefixes[ $first ][ $prefix ] = (array) $paths;
234
235 return $this;
236 }
237 if( $prepend ) {
238 $this->prefixes[ $first ][ $prefix ] = array_merge( (array) $paths, $this->prefixes[ $first ][ $prefix ] );
239 } else {
240 $this->prefixes[ $first ][ $prefix ] = array_merge( $this->prefixes[ $first ][ $prefix ], (array) $paths );
241 }
242
243 return $this;
244
245 }
246
247 /**
248 * Registers a set of classes, replacing any others previously set.
249 *
250 * @param string|array $prefix The classes prefix or an object containing prefixes and strings.
251 * @param array|string $paths The location(s) of the classes
252 *
253 * @return $this
254 *
255 * @method set_namespace
256 * @for Loader
257 *
258 * @author potanin@UD
259 * @version 0.0.2
260 * @since 0.0.2
261 */
262 public function set_namespace( $prefix, $paths = null ) {
263
264 if( is_array( $prefix ) || is_object( $prefix ) ) {
265
266 foreach( $prefix as $namespace => $path ) {
267 $this->set_namespace( $namespace, $path );
268 }
269
270 return $this;
271
272 }
273
274 if( !$prefix ) {
275 $this->fallback_directories = (array) $paths;
276 } else {
277 $this->prefixes[ substr( $prefix, 0, 1 ) ][ $prefix ] = (array) $paths;
278 }
279
280 return $this;
281
282 }
283
284 /**
285 * Registers this instance as an autoloader.
286 *
287 * @param callback $autoload_function [optional]. The autoload function being registered.
288 * @param bool $throw This parameter specifies whether spl_autoload_register() should throw exceptions when the autoload_function cannot be registered.
289 * @param bool $prepend If true, spl_autoload_register() will prepend the autoloader on the autoload stack instead of appending it.
290 *
291 * @method register
292 * @for Loader
293 *
294 * @author potanin@UD
295 * @version 0.0.2
296 * @since 0.0.2
297 */
298 public function register( $throw = false, $prepend = false ) {
299 spl_autoload_register( array( $this, 'load_class' ), $throw, $prepend );
300 }
301
302 /**
303 * Unregisters this instance as an autoloader.
304 *
305 * @method unregister
306 * @for Loader
307 *
308 * @author potanin@UD
309 * @version 0.0.2
310 * @since 0.0.2
311 */
312 public function unregister() {
313 spl_autoload_unregister( array( $this, 'load_class' ) );
314 }
315
316 /**
317 * Loads the given class or interface.
318 *
319 * @param string $class The name of the class
320 *
321 * @return bool|null True if loaded, null otherwise
322 *
323 * @method load_class
324 * @for Loader
325 *
326 * @author potanin@UD
327 * @version 0.0.2
328 * @since 0.0.2
329 */
330 public function load_class( $class ) {
331
332 if( $file = $this->find_file( $class ) ) {
333 include $file;
334
335 return true;
336 }
337
338 }
339
340 /**
341 * Finds the path to the file where the class is defined.
342 *
343 * - $namespace Raw namespace.
344 * - $class_path Fully reoslved name.
345 * - $class_name Just the class name.
346 *
347 * @param string $class The name of the class
348 *
349 * @return string|false The path if found, false otherwise
350 *
351 * @method find_file
352 * @for Loader
353 *
354 * @author potanin@UD
355 * @version 0.0.2
356 * @since 0.0.2
357 */
358 public function find_file( $class ) {
359
360 // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
361 if( '\\' == $class[ 0 ] ) {
362 $class = substr( $class, 1 );
363 }
364
365 // $class = str_replace( 'Flawless\\', '\\', $class );
366
367 if( isset( $this->class_map[ $class ] ) ) {
368 return $this->class_map[ $class ];
369 }
370
371 if( false !== $pos = strrpos( $class, '\\' ) ) {
372 $class_path = strtr( substr( $class, 0, $pos ), '\\', DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
373 $class_name = substr( $class, $pos + 1 );
374 } else {
375 $class_path = null;
376 $class_name = $class;
377 }
378
379 $namespace = $class_path;
380 $class_path .= strtr( $class_name, '_', DIRECTORY_SEPARATOR ) . '.php';
381
382 $first = $class[ 0 ];
383
384 if( isset( $this->prefixes[ $first ] ) ) {
385
386 //die( '<pre>' . print_r( $this->prefixes[ $first ], true ) . '</pre>' );
387
388 foreach( $this->prefixes[ $first ] as $prefix => $dirs ) {
389 if( 0 === strpos( $class, $prefix ) ) {
390 foreach( $dirs as $dir ) {
391
392 if( file_exists( $dir . DIRECTORY_SEPARATOR . $class_path ) ) {
393 return $dir . DIRECTORY_SEPARATOR . $class_path;
394 }
395
396 // If file not found, try with the namespace stripped
397 if( file_exists( $dir . DIRECTORY_SEPARATOR . str_replace( $namespace, '', $class_path ) ) ) {
398 return $dir . DIRECTORY_SEPARATOR . str_replace( $namespace, '', $class_path );
399 }
400
401 }
402 }
403 }
404 }
405
406 foreach( $this->fallback_directories as $dir ) {
407 if( file_exists( $dir . DIRECTORY_SEPARATOR . $class_path ) ) {
408 return $dir . DIRECTORY_SEPARATOR . $class_path;
409 }
410 }
411
412 return $this->class_map[ $class ] = false;
413
414 }
415
416 /**
417 * Parse File Headers
418 *
419 * @example
420 *
421 * Loader::get_file_data( 'style.css' );
422 * Loader::get_file_data( 'my-module/my-module.php', 'module' );
423 *
424 * @method get_file_data
425 * @for Loader
426 *
427 * @author potanin@UD
428 * @version 0.0.2
429 * @since 0.0.2
430 *
431 * @param string $path Full path to the target file.
432 * @param string $type Type of target file, defaults to theme, must be defined in Loader::$headers.
433 *
434 * @return array
435 */
436 public static function get_file_data( $path = '', $type = 'theme' ) {
437 return array_filter( (array) get_file_data( $path, is_string( $type ) ? Loader::$headers[ $type ] : Loader::$headers[ 'theme' ], $type ) );
438 }
439
440 }
441
442 }
443
444 }
445
446