AutoLoader Class

This commit is contained in:
Steffen Schröder 2014-06-17 21:46:32 +02:00
parent 83aa2246c7
commit 2ff88c230b
2 changed files with 47 additions and 19 deletions

View File

@ -98,25 +98,9 @@ checkRequirements();
// Make sure garbage collection is enabled // Make sure garbage collection is enabled
gc_enable(); gc_enable();
// Autoload Function that loads ManiaControl Class Files on Demand // Register AutoLoader
spl_autoload_register(function ($className) { require_once ManiaControlDir . 'core' . DIRECTORY_SEPARATOR . 'AutoLoader.php';
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className); \ManiaControl\AutoLoader::register();
// Core file
$classDirectoryPath = preg_replace('/ManiaControl/', 'core', $classPath, 1);
$filePath = ManiaControlDir . $classDirectoryPath . '.php';
if (file_exists($filePath)) {
require_once $filePath;
return;
}
// Plugin file
$filePath = ManiaControlDir . 'plugins' . DIRECTORY_SEPARATOR . $classPath . '.php';
if (file_exists($filePath)) {
include_once $filePath;
return;
}
});
// Start ManiaControl // Start ManiaControl
$maniaControl = new \ManiaControl\ManiaControl(); $maniaControl = new \ManiaControl\ManiaControl();

View File

@ -0,0 +1,44 @@
<?php
namespace ManiaControl;
/**
* ManiaControl AutoLoader
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class AutoLoader {
/**
* Register the Auto Loader
*/
public static function register() {
spl_autoload_register(array(get_class(), 'autoload'));
}
/**
* Try to autoload the Class with the given Name
*
* @param string $className
*/
public static function autoload($className) {
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className);
// Core file
$classDirectoryPath = preg_replace('/ManiaControl/', 'core', $classPath, 1);
$filePath = ManiaControlDir . $classDirectoryPath . '.php';
if (file_exists($filePath)) {
require_once $filePath;
return;
}
// Plugin file
$filePath = ManiaControlDir . 'plugins' . DIRECTORY_SEPARATOR . $classPath . '.php';
if (file_exists($filePath)) {
include_once $filePath;
return;
}
}
}