From 58a8fa48c1965d21e00d0b66198d5ecb947e2c89 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 22 May 2012 16:12:04 -0400 Subject: [PATCH] Revert "improved autoloader" This reverts commit cade0f5768f650687633eb6d793a7f813f7381cd. --- sys/common.php | 71 +++++++++++++------ sys/core/{controller.php => Controller.php} | 0 sys/core/{mm.php => MM.php} | 0 sys/core/{model.php => Model.php} | 0 sys/core/{output.php => Output.php} | 0 sys/core/{page.php => Page.php} | 0 sys/core/{minimvc.php => miniMVC.php} | 0 sys/core/traits/{generic.php => Generic.php} | 0 .../traits/{jsobject.php => JSObject.php} | 0 .../traits/{singleton.php => Singleton.php} | 0 10 files changed, 50 insertions(+), 21 deletions(-) rename sys/core/{controller.php => Controller.php} (100%) rename sys/core/{mm.php => MM.php} (100%) rename sys/core/{model.php => Model.php} (100%) rename sys/core/{output.php => Output.php} (100%) rename sys/core/{page.php => Page.php} (100%) rename sys/core/{minimvc.php => miniMVC.php} (100%) rename sys/core/traits/{generic.php => Generic.php} (100%) rename sys/core/traits/{jsobject.php => JSObject.php} (100%) rename sys/core/traits/{singleton.php => Singleton.php} (100%) diff --git a/sys/common.php b/sys/common.php index 0aa2892..931a12d 100644 --- a/sys/common.php +++ b/sys/common.php @@ -31,42 +31,67 @@ namespace miniMVC; * * @param string */ -function _autoload($name) +function sys_autoload($name) { - if ($name == '') return; - - // strip off namespaces - they all go to the same folder - $name = strtolower($name); - $names = explode('\\', trim($name)); - $name = end($names); + $name = str_replace('miniMVC\\', '', $name); - // Paths to load from - $sys_path = MM_SYS_PATH . "core/{$name}.php"; - $trait_path = MM_SYS_PATH . "core/traits/{$name}.php"; - $lib_path = MM_SYS_PATH . "libraries/{$name}.php"; - $class_path = MM_APP_PATH . "classes/{$name}.php"; + $path = MM_SYS_PATH . "/core/{$name}.php"; + $trait_path = MM_SYS_PATH . "/core/traits/{$name}.php"; - if (is_file($sys_path)) + if (is_file($path)) { - require_once($sys_path); + require_once($path); } elseif (is_file($trait_path)) { require_once($trait_path); } - elseif (is_file($lib_path)) +} + +// -------------------------------------------------------------------------- + +/** + * Function to autoload libraries/classes + * + * @param string + */ +function autoload($name) +{ + // strip off namespaces - they all go to the same folder + $names = explode('\\', $name); + $name = end($names); + + // In a subdirectory? No problem + if (strpos("/", $name) !== FALSE) { - require_once($lib_path); + $n = explode("/", $name); + $name = $n[count($n) -1]; } - if (is_file($class_path)) + // Try system library first, then app library + $name = strtolower($name); + $path = MM_SYS_PATH . "libraries/{$name}.php"; + + if ( ! is_file($path)) { - require_once($class_path); + $path = MM_APP_PATH . "classes/{$name}.php"; + } + + // Load the class file if need be + if ( ! class_exists($name)) + { + if (is_file($path)) + { + require_once($path); + } } } -// Start the autoloader -spl_autoload_register('miniMVC\_autoload'); +// Load system libraries/traits +spl_autoload_register('miniMVC\sys_autoload'); + +// Start the library autoloader +spl_autoload_register('miniMVC\autoload'); // -------------------------------------------------------------------------- // ! Error handling / messages @@ -440,9 +465,13 @@ function route() $class = new $controller(); return call_user_func_array([&$class, $func], []); } + + // Function doesn't exist...404 + show_404(); + die(); } - // Function doesn't exist...404 + // If it gets here, it's still a 404 show_404(); } diff --git a/sys/core/controller.php b/sys/core/Controller.php similarity index 100% rename from sys/core/controller.php rename to sys/core/Controller.php diff --git a/sys/core/mm.php b/sys/core/MM.php similarity index 100% rename from sys/core/mm.php rename to sys/core/MM.php diff --git a/sys/core/model.php b/sys/core/Model.php similarity index 100% rename from sys/core/model.php rename to sys/core/Model.php diff --git a/sys/core/output.php b/sys/core/Output.php similarity index 100% rename from sys/core/output.php rename to sys/core/Output.php diff --git a/sys/core/page.php b/sys/core/Page.php similarity index 100% rename from sys/core/page.php rename to sys/core/Page.php diff --git a/sys/core/minimvc.php b/sys/core/miniMVC.php similarity index 100% rename from sys/core/minimvc.php rename to sys/core/miniMVC.php diff --git a/sys/core/traits/generic.php b/sys/core/traits/Generic.php similarity index 100% rename from sys/core/traits/generic.php rename to sys/core/traits/Generic.php diff --git a/sys/core/traits/jsobject.php b/sys/core/traits/JSObject.php similarity index 100% rename from sys/core/traits/jsobject.php rename to sys/core/traits/JSObject.php diff --git a/sys/core/traits/singleton.php b/sys/core/traits/Singleton.php similarity index 100% rename from sys/core/traits/singleton.php rename to sys/core/traits/Singleton.php