From cade0f5768f650687633eb6d793a7f813f7381cd Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 22 May 2012 16:07:30 -0400 Subject: [PATCH] improved autoloader --- sys/common.php | 71 ++++++------------- sys/core/{Controller.php => controller.php} | 0 sys/core/{miniMVC.php => minimvc.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/traits/{Generic.php => generic.php} | 0 .../traits/{JSObject.php => jsobject.php} | 0 .../traits/{Singleton.php => singleton.php} | 0 10 files changed, 21 insertions(+), 50 deletions(-) rename sys/core/{Controller.php => controller.php} (100%) rename sys/core/{miniMVC.php => minimvc.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/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 931a12d..0aa2892 100644 --- a/sys/common.php +++ b/sys/common.php @@ -31,67 +31,42 @@ namespace miniMVC; * * @param string */ -function sys_autoload($name) +function _autoload($name) { - $name = str_replace('miniMVC\\', '', $name); + if ($name == '') return; + + // strip off namespaces - they all go to the same folder + $name = strtolower($name); + $names = explode('\\', trim($name)); + $name = end($names); - $path = MM_SYS_PATH . "/core/{$name}.php"; - $trait_path = MM_SYS_PATH . "/core/traits/{$name}.php"; + // 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"; - if (is_file($path)) + if (is_file($sys_path)) { - require_once($path); + require_once($sys_path); } elseif (is_file($trait_path)) { require_once($trait_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) + elseif (is_file($lib_path)) { - $n = explode("/", $name); - $name = $n[count($n) -1]; + require_once($lib_path); } - // Try system library first, then app library - $name = strtolower($name); - $path = MM_SYS_PATH . "libraries/{$name}.php"; - - if ( ! is_file($path)) + if (is_file($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); - } + require_once($class_path); } } -// Load system libraries/traits -spl_autoload_register('miniMVC\sys_autoload'); - -// Start the library autoloader -spl_autoload_register('miniMVC\autoload'); +// Start the autoloader +spl_autoload_register('miniMVC\_autoload'); // -------------------------------------------------------------------------- // ! Error handling / messages @@ -465,13 +440,9 @@ function route() $class = new $controller(); return call_user_func_array([&$class, $func], []); } - - // Function doesn't exist...404 - show_404(); - die(); } - // If it gets here, it's still a 404 + // Function doesn't exist...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/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/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/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