Added namespace support to class loader. If not present, an E_USER_WARNING is
authorRoland Haeder <roland@mxchange.org>
Mon, 13 Feb 2017 20:46:20 +0000 (21:46 +0100)
committerRoland Haeder <roland@mxchange.org>
Tue, 28 Feb 2017 21:09:54 +0000 (22:09 +0100)
being issued.

Signed-off-by: Roland H├Ąder <roland@mxchange.org>
inc/loader/class_ClassLoader.php

index 8ce7f4f..8886bd1 100644 (file)
@@ -14,7 +14,7 @@ use \RecursiveIteratorIterator;
  * This class loads class include files with a specific prefix and suffix
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
- * @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 <http://www.gnu.org/licenses/>.
  *
  * ----------------------------------
+ * 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);