Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Created September 22, 2010 23:32
Show Gist options
  • Save radiosilence/592787 to your computer and use it in GitHub Desktop.
Save radiosilence/592787 to your computer and use it in GitHub Desktop.
<?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