Created
September 22, 2010 23:32
-
-
Save radiosilence/592787 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$imported_files = array(); | |
$include_paths = array_merge(array(__DIR__), explode(':', ini_get('include_path'))); | |
class ImportError extends \Exception { | |
public function __construct($class) { | |
trigger_error(sprintf('Class "%s" was not found in any of available paths.', $class), E_USER_ERROR); | |
} | |
} | |
function import($module_name) { | |
global $imported_files; | |
global $include_paths; | |
$module = explode('.', $module_name); | |
$l = array_pop($module); | |
foreach($include_paths as $include_path) { | |
if($l == '*'){ | |
$dir = $include_path . DIRSEP . implode(DIRSEP, $module) . DIRSEP; | |
if(!is_dir($dir)){ | |
continue; | |
} else { | |
foreach(glob( $dir . '*.php') as $file) { | |
include_once $file; | |
} | |
break; | |
} | |
} else { | |
$mp = $include_path . DIRSEP . implode(DIRSEP, $module) . DIRSEP . $l . '.php'; | |
if(in_array($mp,$imported_files)) { | |
break; | |
} else { | |
if(file_exists($mp)){ | |
include_once strtolower($mp); | |
$imported_files[] = $mp; | |
break; | |
} | |
} | |
} | |
throw new ImportError($module_name); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment