Added namespace support to class loader. If not present, an E_USER_WARNING is
[core.git] / inc / loader / class_ClassLoader.php
index 8ce7f4fe7732bbae6608045a4ce689c6b0d6a98d..8886bd18e7ded2e7d19e8735303bbbcf73153ed2 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);