From 54ac435b892edabd140aadc9f5b07219b3d2344b Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 13 Feb 2017 21:46:20 +0100 Subject: [PATCH] Added namespace support to class loader. If not present, an E_USER_WARNING is being issued. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- inc/loader/class_ClassLoader.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/inc/loader/class_ClassLoader.php b/inc/loader/class_ClassLoader.php index 8ce7f4fe..8886bd18 100644 --- a/inc/loader/class_ClassLoader.php +++ b/inc/loader/class_ClassLoader.php @@ -14,7 +14,7 @@ use \RecursiveIteratorIterator; * This class loads class include files with a specific prefix and suffix * * @author Roland Haeder - * @version 0.0.0 + * @version 1.5.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org @@ -33,6 +33,10 @@ use \RecursiveIteratorIterator; * along with this program. If not, see . * * ---------------------------------- + * 1.5 + * - Namespace scheme Project\Package[\SubPackage...] is fully supported and + * throws an E_USER_WARNING if not present. The last part will be always the + * class' name. * 1.4 * - Some comments improved, other minor improvements * 1.3 @@ -275,7 +279,6 @@ class ClassLoader { */ public static function autoLoad ($className) { // Try to include this class - //* NOISY-DEBUG: */ printf('[%s:%d] className=%s' . PHP_EOL, __METHOD__, __LINE__, $className); self::getSelfInstance()->includeClass($className); } @@ -410,8 +413,20 @@ class ClassLoader { * @return void */ public function includeClass ($className) { + // The class name should contain at least 2 back-slashes, so split at them + $classNameParts = explode("\\", $className); + + // At least 3 parts should be there + if (count($classNameParts) < 3) { + // Namespace scheme is: Project\Package[\SubPackage...] + trigger_error(sprintf('Class name "%s" is not after naming-convention: Project\Package[\SubPackage...]', $className), E_USER_WARNING); + } // END - if + + // Get last element + $shortClassName = array_pop($classNameParts); + // Create a name with prefix and suffix - $fileName = $this->prefix . $className . $this->suffix; + $fileName = $this->prefix . $shortClassName . $this->suffix; // Now look it up in our index //* NOISY-DEBUG: */ printf('[%s:%d] ISSET: %s' . PHP_EOL, __METHOD__, __LINE__, $fileName); -- 2.39.5