improved autoload to support namespace-based file hierarchy in plugins directory

This commit is contained in:
Steffen Schröder 2014-02-15 16:09:22 +01:00
parent 99b7e1464f
commit 9915f66b0b

View File

@ -58,10 +58,20 @@ function logMessage($message) {
// Autoload Function that loads ManiaControl Class Files on Demand // Autoload Function that loads ManiaControl Class Files on Demand
spl_autoload_register(function ($className) { spl_autoload_register(function ($className) {
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className); $classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$classPath = preg_replace('/ManiaControl/', 'core', $classPath, 1);
$filePath = ManiaControlDir . DIRECTORY_SEPARATOR . $classPath . '.php'; // Core file
$classDirectoryPath = preg_replace('/ManiaControl/', 'core', $classPath, 1);
$filePath = ManiaControlDir . DIRECTORY_SEPARATOR . $classDirectoryPath . '.php';
if (file_exists($filePath)) { if (file_exists($filePath)) {
require_once $filePath; require_once $filePath;
return;
}
// Plugin file
$filePath = ManiaControlDir . DIRECTORY_SEPARATOR . 'plugins/' . $classPath . '.php';
if (file_exists($filePath)) {
require_once $filePath;
return;
} }
}); });