From 2ff88c230bbbff676d3b91b5b77d7411361cee86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Tue, 17 Jun 2014 21:46:32 +0200 Subject: [PATCH] AutoLoader Class --- application/ManiaControl.php | 22 +++-------------- application/core/AutoLoader.php | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 application/core/AutoLoader.php diff --git a/application/ManiaControl.php b/application/ManiaControl.php index 29675913..700f3e52 100644 --- a/application/ManiaControl.php +++ b/application/ManiaControl.php @@ -98,25 +98,9 @@ checkRequirements(); // Make sure garbage collection is enabled gc_enable(); -// Autoload Function that loads ManiaControl Class Files on Demand -spl_autoload_register(function ($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; - } -}); +// Register AutoLoader +require_once ManiaControlDir . 'core' . DIRECTORY_SEPARATOR . 'AutoLoader.php'; +\ManiaControl\AutoLoader::register(); // Start ManiaControl $maniaControl = new \ManiaControl\ManiaControl(); diff --git a/application/core/AutoLoader.php b/application/core/AutoLoader.php new file mode 100644 index 00000000..0b508bc7 --- /dev/null +++ b/application/core/AutoLoader.php @@ -0,0 +1,44 @@ + + * @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; + } + } +}