Last active
August 29, 2015 14:14
-
-
Save nguyenvanduocit/8980fc88a05074083222 to your computer and use it in GitHub Desktop.
Util class to autoload another class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !class_exists( 'AEM_Autoload' ) ) { | |
/** | |
* Auto load core class and module class | |
* | |
* Class WFA_Autoload | |
* @author nguyenvanduocit | |
*/ | |
class AEM_Autoload | |
{ | |
/** | |
* @param $class bane | |
*/ | |
public static function autoload ( $class ) | |
{ | |
$path = NULL; | |
$class = strtolower( $class ); | |
$file = 'class-'.str_replace( '_', '-', $class ).'.php'; | |
//base module | |
if ( ( $class == 'aem_module_base' ) || ( $class == 'aem_module_factory' ) ) { | |
$path = AEM_PLUGIN_PATH."/inc/module/"; | |
} //module class | |
elseif ( strpos( $class, 'aem_module_' ) === 0 ) { | |
$last_dash = strripos( $class, '_' ); | |
if ( $last_dash > 10 ) { | |
$path = AEM_PLUGIN_PATH."/inc/module/".trailingslashit( substr( str_replace( '_', '-', $class ),11, $last_dash - 11 ) ); | |
} else { | |
$path = AEM_PLUGIN_PATH."/inc/module/".trailingslashit( substr( str_replace( '_', '-', $class ), 11 ) ); | |
} | |
} elseif ( strpos( $class, 'ae_custom_type_' ) === 0 ) { | |
$path = AEM_PLUGIN_PATH."/inc/custom_fields/"; | |
} elseif ( strpos( $class, 'aem_' ) === 0 ) { | |
$path = AEM_PLUGIN_PATH."/inc/"; | |
} else { | |
return; | |
} | |
if ( $path && is_readable( $path.$file ) ) { | |
include_once( $path.$file ); | |
return; | |
} | |
} | |
} | |
// register class autoloader | |
spl_autoload_register( array ( 'AEM_Autoload', 'autoload' ) ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment