| 1 |
<?php |
| 2 |
|
| 3 |
namespace Timetics; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Etn autoloader. |
| 9 |
* Handles dynamically loading classes only when needed. |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class Autoloader { |
| 14 |
/** |
| 15 |
* Run autoloader. |
| 16 |
* Register a function as `__autoload()` implementation. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
public static function run() { |
| 22 |
spl_autoload_register( array( __CLASS__, 'autoload' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Autoload. |
| 27 |
* For a given class, check if it exist and load it. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @access private |
| 31 |
* @param string $class_name Class name. |
| 32 |
*/ |
| 33 |
private static function autoload( $class_name ) { |
| 34 |
|
| 35 |
// If the class being requested does not start with our prefix |
| 36 |
// we know it's not one in our project. |
| 37 |
if ( 0 !== strpos( $class_name, __NAMESPACE__ ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$file_name = strtolower( |
| 42 |
preg_replace( |
| 43 |
array( '/\b' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ), |
| 44 |
array( '', '$1-$2', '-', DIRECTORY_SEPARATOR ), |
| 45 |
$class_name |
| 46 |
) |
| 47 |
); |
| 48 |
|
| 49 |
// Compile our path from the corosponding location. |
| 50 |
$file = plugin_dir_path( __FILE__ ) . $file_name . '.php'; |
| 51 |
|
| 52 |
// If a file is found. |
| 53 |
if ( file_exists( $file ) ) { |
| 54 |
// Then load it up! |
| 55 |
require_once $file; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
Autoloader::run(); |
| 62 |
|