Skip to content

Commit

Permalink
Autoload function moved to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejlew committed May 17, 2014
1 parent ed72242 commit 87f6db0
Showing 1 changed file with 3 additions and 94 deletions.
97 changes: 3 additions & 94 deletions index.php
Expand Up @@ -50,7 +50,6 @@
$CONFIG['directories']['sys_dir'] = (!isset($CONFIG['directories']['sys_dir']) ? getcwd() : $CONFIG['directories']['sys_dir']);
$CONFIG['directories']['lib_dir'] = (!isset($CONFIG['directories']['lib_dir']) ? $CONFIG['directories']['sys_dir'].'/lib' : $CONFIG['directories']['lib_dir']);
$CONFIG['directories']['doc_dir'] = (!isset($CONFIG['directories']['doc_dir']) ? $CONFIG['directories']['sys_dir'].'/documents' : $CONFIG['directories']['doc_dir']);
$CONFIG['directories']['cache_dir'] = (!isset($CONFIG['directories']['cache_dir']) ? $CONFIG['directories']['sys_dir'].'/cache' : $CONFIG['directories']['cache_dir']);
$CONFIG['directories']['modules_dir'] = (!isset($CONFIG['directories']['modules_dir']) ? $CONFIG['directories']['sys_dir'].'/modules' : $CONFIG['directories']['modules_dir']);
$CONFIG['directories']['backup_dir'] = (!isset($CONFIG['directories']['backup_dir']) ? $CONFIG['directories']['sys_dir'].'/backups' : $CONFIG['directories']['backup_dir']);
$CONFIG['directories']['config_templates_dir'] = (!isset($CONFIG['directories']['config_templates_dir']) ? $CONFIG['directories']['sys_dir'].'/config_templates' : $CONFIG['directories']['config_templates_dir']);
Expand All @@ -60,108 +59,18 @@
define('SYS_DIR', $CONFIG['directories']['sys_dir']);
define('LIB_DIR', $CONFIG['directories']['lib_dir']);
define('DOC_DIR', $CONFIG['directories']['doc_dir']);
define('CACHE_DIR', $CONFIG['directories']['cache_dir']);
define('BACKUP_DIR', $CONFIG['directories']['backup_dir']);
define('MODULES_DIR', $CONFIG['directories']['modules_dir']);
define('SMARTY_COMPILE_DIR', $CONFIG['directories']['smarty_compile_dir']);
define('SMARTY_TEMPLATES_DIR', $CONFIG['directories']['smarty_templates_dir']);

// Do some checks and load config defaults
// Load autloader
require_once(LIB_DIR.'/autoloader.php');

// Do some checks and load config defaults
require_once(LIB_DIR.'/checkdirs.php');
require_once(LIB_DIR.'/config.php');

/**
* Autoloader function.
*
* Loads classes "on the fly". Require or incluse statements are no longer needed.
* Class name should be the same as name of file where it is stored. Class files
* should have ".php" extension. Already known class paths are stored in special
* cache file. That cache file is stored in CACHE_DIR path, by default in
* SYS_DIR/cache, so that path should be writeable for apache. You can change that
* path in lms.ini file in directories section. You should clear cache file each
* time you move class file to another location.
*
* This function should be registered with spl_autoload_register function before
* first usage.
*
* @param string $class Class name
* @package LMS
*/
function application_autoloader($class) {

$base_classes = array(
'LMSDB_common' => 'LMSDB_common.class.php',
'LMSDB_driver_mysql' => 'LMSDB_driver_mysql.class.php',
'LMSDB_driver_mysqli' => 'LMSDB_driver_mysqli.class.php',
'LMSDB_driver_postgres' => 'LMSDB_driver_postgres.class.php',
'LMS' => 'LMS.class.php',
'Auth' => 'Auth.class.php',
'ExecStack' => 'ExecStack.class.php',
'Session' => 'Session.class.php',
'Sysinfo' => 'Sysinfo.class.php',
'TCPDFpl' => 'tcpdf.php',
'Smarty' => 'Smarty/Smarty.class.php',
'SmartyBC' => 'Smarty/SmartyBC.class.php',
'Cezpdf' => 'ezpdf/class.ezpdf.php',
'Cpdf' => 'ezpdf/class.pdf.php',
'HTML2PDF' => 'html2pdf/html2pdf.class.php',
'TCPDF' => 'tcpdf/tcpdf.php'
);

if (array_key_exists($class, $base_classes)) {
require_once LIB_DIR . DIRECTORY_SEPARATOR . $base_classes[$class];
} else {
// set cache file path
$cache_file = CACHE_DIR . "/classpaths.cache";
// read cache
$path_cache = (file_exists($cache_file)) ? unserialize(file_get_contents($cache_file)) : array();
// create empty cache container if cache is empty
if (!is_array($path_cache)) {
$path_cache = array();
}

// check if class path exists in cache
if (array_key_exists($class, $path_cache)) {
// try to load file
if (file_exists($path_cache[$class])) {
require_once $path_cache[$class];
}
} else {
// try to find class file in LIB_DIR
$directories = new RecursiveDirectoryIterator(LIB_DIR);
$suspicious_file_names = array(
$class.'.php',
$class.'.class.php',
strtolower($class).'.php',
strtolower($class).'.class.php',
strtoupper($class).'.php',
strtoupper($class).'.class.php',
);
foreach (new RecursiveIteratorIterator($directories) as $file) {
if (in_array($file->getFilename(), $suspicious_file_names)) {
// get class file path
$full_path = $file->getRealPath();
// store path in cache
$path_cache[$class] = $full_path;
// load class file
require_once $full_path;
break;
}
}
}
// serialize cache
$serialized_paths = serialize($path_cache);
// if cache changed save it
if ($serialized_paths != $path_cache) {
file_put_contents($cache_file, $serialized_paths);
}
}
}

// register autoloader
spl_autoload_register('application_autoloader');

// Init database

$_DBTYPE = $CONFIG['database']['type'];
Expand Down

0 comments on commit 87f6db0

Please sign in to comment.