Class loader rewritten to SPL classes
authorRoland Häder <roland@mxchange.org>
Wed, 11 Jun 2008 19:07:17 +0000 (19:07 +0000)
committerRoland Häder <roland@mxchange.org>
Wed, 11 Jun 2008 19:07:17 +0000 (19:07 +0000)
application/selector/loader.php
application/ship-simu/loader.php
inc/classes.php
inc/includes.php
inc/loader/class_ClassLoader.php

index aafbd380a732c2fba4820823debe8ca72d0b4043..ace39f5aeea890c36acd349785e38028137a283e 100644 (file)
@@ -30,9 +30,6 @@ if (!isset($application)) {
 // Load all classes
 ClassLoader::getInstance()->loadClasses(sprintf("%s/%s/", FrameworkConfiguration::getInstance()->readConfig('application_path'), $application));
 
 // Load all classes
 ClassLoader::getInstance()->loadClasses(sprintf("%s/%s/", FrameworkConfiguration::getInstance()->readConfig('application_path'), $application));
 
-// Include all classes
-ClassLoader::getInstance()->includeAllClasses();
-
 // Clean up the global namespace
 unset($lowerClasses);
 unset($class);
 // Clean up the global namespace
 unset($lowerClasses);
 unset($class);
index 240c01aec9344f478869b108de57018de6281ec1..0ffd93f30c6340c2aff1538a11df0616abda7dd8 100644 (file)
@@ -60,9 +60,6 @@ foreach ($lowerClasses as $class) {
        }
 }
 
        }
 }
 
-// Include all classes
-ClassLoader::getInstance()->includeAllClasses();
-
 // Clean up the global namespace
 unset($lowerClasses);
 unset($class);
 // Clean up the global namespace
 unset($lowerClasses);
 unset($class);
index 27034212b2dc4f79f9b2081463d9ee9ff63c236e..1c52372f65cc9b5d1bbf614ad9df9f2dec6d11fa 100644 (file)
@@ -53,9 +53,6 @@ foreach ($lowerClasses as $class) {
        }
 }
 
        }
 }
 
-// Include all classes
-ClassLoader::getInstance()->includeAllClasses();
-
 // Clean up the global namespace
 unset($lowerClasses[3]); // Applications shall not have any middleware
 unset($class);
 // Clean up the global namespace
 unset($lowerClasses[3]); // Applications shall not have any middleware
 unset($class);
index 139be1401c604c87a484746ae148e36c26823ab7..c7a949aacb1b44768ec9a871ba2095fd7e7ea152 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-
 // Include the class loader function
 require(sprintf("%sinc/loader/class_ClassLoader%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
 
 // Include the class loader function
 require(sprintf("%sinc/loader/class_ClassLoader%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
 
+/**
+ * Autoload-function
+ *
+ * @param      $className      Name of the class to load
+ * @return     void
+ */
+function __autoload ($className) {
+       // Try to include this class
+       ClassLoader::getInstance()->includeClass($className);
+}
+
 /**
  * Is the devel package included?
  */
 /**
  * Is the devel package included?
  */
index 06d2bb29e97ce02f35a9eaa0645a45af3587a509..a1b2e8230e126e36d0af3dbde6aa0e02e7dd69a3 100644 (file)
@@ -22,6 +22,8 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  *
  * ----------------------------------
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  *
  * ----------------------------------
+ * 1.2
+ *  - ClassLoader rewritten to PHP SPL's own RecursiveIteratorIterator class
  * 1.1
  *  - loadClasses rewritten to fix some notices
  * 1.0
  * 1.1
  *  - loadClasses rewritten to fix some notices
  * 1.0
@@ -35,9 +37,9 @@ class ClassLoader {
        private $cfg = array();
 
        /**
        private $cfg = array();
 
        /**
-        * An ArrayObject for found classes
+        * Array with all classes
         */
         */
-       private $classes = null;
+       private $classes = array();
 
        /**
         * Suffix with extension for all class files
 
        /**
         * Suffix with extension for all class files
@@ -52,12 +54,12 @@ class ClassLoader {
        /**
         * Length of the suffix. Will be overwritten later.
         */
        /**
         * Length of the suffix. Will be overwritten later.
         */
-       private $sufLen = 0;
+       private $suffixLen = 0;
 
        /**
         * Length of the prefix. Will be overwritten later.
         */
 
        /**
         * Length of the prefix. Will be overwritten later.
         */
-       private $preLen = 0;
+       private $prefixLen = 0;
 
        /**
         * A list for directory names (no leading/trailing slashes!) which not be scanned by the path scanner
 
        /**
         * A list for directory names (no leading/trailing slashes!) which not be scanned by the path scanner
@@ -65,26 +67,11 @@ class ClassLoader {
         */
        private $ignoreList = array();
 
         */
        private $ignoreList = array();
 
-       /**
-        * An ArrayList object for include directories
-        */
-       private $dirList = null;
-
        /**
         * Debug this class loader? (true = yes, false = no)
         */
        private $debug = false;
 
        /**
         * Debug this class loader? (true = yes, false = no)
         */
        private $debug = false;
 
-       /**
-        * Counter for scanned directories (debug output)
-        */
-       private $dirCnt = 0;
-
-       /**
-        * Counter for loaded classes (debug output)
-        */
-       private $classCnt = 0;
-
        /**
         * Instance of this class
         */
        /**
         * Instance of this class
         */
@@ -97,23 +84,17 @@ class ClassLoader {
         * @return      void
         */
        public function __construct (FrameworkConfiguration $cfgInstance) {
         * @return      void
         */
        public function __construct (FrameworkConfiguration $cfgInstance) {
-               // Init the array list
-               $this->dirList = new ArrayObject();
-
                // Set suffix and prefix from configuration
                $this->suffix = $cfgInstance->readConfig('class_suffix');
                $this->prefix = $cfgInstance->readConfig('class_prefix');
 
                // Estimate length of prefix and suffix for substr() function (cache)
                // Set suffix and prefix from configuration
                $this->suffix = $cfgInstance->readConfig('class_suffix');
                $this->prefix = $cfgInstance->readConfig('class_prefix');
 
                // Estimate length of prefix and suffix for substr() function (cache)
-               $this->sufLen = strlen($this->suffix);
-               $this->preLen = strlen($this->prefix);
+               $this->suffixLen = strlen($this->suffix);
+               $this->prefixLen = strlen($this->prefix);
 
                // Set configuration instance
                $this->cfgInstance = $cfgInstance;
 
 
                // Set configuration instance
                $this->cfgInstance = $cfgInstance;
 
-               // Initialize the classes list
-               $this->classes = new ArrayObject();
-
                // Set own instance
                self::$selfInstance = $this;
        }
                // Set own instance
                self::$selfInstance = $this;
        }
@@ -170,102 +151,23 @@ class ClassLoader {
                        $basePath = $basePath2;
                }
 
                        $basePath = $basePath2;
                }
 
-               // Load all super classes (backward, why ever this name... :-? )
-               // We don't support sub directories here...
-               $this->scanLocalPath($basePath);
-
-               // While there are directories in our list scan them for classes
-               $cnt = 0;
-               while ($cnt != $this->dirList->count()) {
-                       for ($idx = $this->dirList->getIterator(); $idx->valid(); $idx->next()) {
-                               // Get current path
-                               $currPath = $idx->current();
-
-                               // Remove the current entry or else this will lead into a infinite loop
-                               $this->dirList->offsetSet($idx->key(), "");
-
-                               // Scan the directory
-                               $this->scanLocalPath($currPath);
-                       } // END - for
-
-                       // Check if we can leave
-                       $cnt = 0;
-                       for ($idx = $this->dirList->getIterator(); $idx->valid(); $idx->next()) {
-                               if ($idx->current() == "") $cnt++;
-                       } // END - for
-               } // END - while
-       }
-
-       /**
-       * The local path scanner. A found class will be loaded immediately
-       * @param                $localPath      The local path which shall be recursively scanned for include files
-       * @return               void
-       */
-       private function scanLocalPath ($localPath) {
-               // Empty path names will be silently ignored
-               if (empty($localPath)) return;
-
-               // TODO: No dies here, mayybe this should be rewritten to throw an exception?
-               $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($localPath);
-               while ($dirClass = $dirInstance->readDirectoryExcept($this->ignoreList)) {
-                       // We need the relative dir name as an array index some lines below
-                       $dirClass2 = $dirClass;
-
-                       // A nice replacement for a simple dot ;)
-                       $dirClass = sprintf("%s/%s", $localPath, $dirClass);
-
-                       // Is a readable file with configured prefix and suffix? All other
-                       // files will silently be ignored!
-                       //* DEBUG: */ print "Prefix=".$this->prefix."(".substr($dirClass2, 0 , $this->preLen).")\n";
-                       //* DEBUG: */ print "Suffix=".$this->suffix."(".substr($dirClass2, -$this->sufLen, $this->sufLen).")\n";
-                       //* DEBUG: */ print "ENTRY={$dirClass}\n";
-                       if (
-                                (is_file($dirClass))
-                       && (is_readable($dirClass))
-                       && (substr($dirClass2, 0 , $this->preLen) == $this->prefix)
-                       && (substr($dirClass2, -$this->sufLen, $this->sufLen) == $this->suffix)
-                       ) {
-                               // Class found so load it instantly
-                               //* DEBUG: */ print "CLASS={$dirClass}\n";
-                               $this->classes->append($dirClass);
-                               $this->classCnt++;
-                       } elseif (is_dir($dirClass) && !in_array($dirClass2, $this->ignoreList)) {
-                               // Directory found and added to list
-                               //* DEBUG: */ print "DIR={$dirClass}\n";
-                               $this->dirList->append($dirClass);
-                               $this->dirCnt++;
+               // Get a new iterator
+               //* DEBUG: */ echo "<strong>Base path: {$basePath}</strong><br />\n";
+               $iterator = new RecursiveDirectoryIterator($basePath);
+               $recursive = new RecursiveIteratorIterator($iterator);
+               foreach ($recursive as $entry) {
+                       // Get filename from iterator
+                       $fileName = $entry->getFileName();
+
+                       // Is this file wanted?
+                       //* DEBUG: */ echo "FOUND:{$fileName}<br />\n";
+                       if ((!in_array($fileName, $this->ignoreList)) && (substr($fileName, 0, $this->prefixLen) == $this->prefix) && (substr($fileName, -$this->suffixLen, $this->suffixLen) == $this->suffix)) {
+                               // Get the FQFN and add it to our class list
+                               $fqfn = $entry->getRealPath();
+                               //* DEBUG: */ echo "ADD: {$fileName}<br />\n";
+                               $this->classes[$fileName] = $fqfn;
                        }
                        }
-                       //* DEBUG: */ print "LOOP!\n";
-               } // END - while
-
-               // Close directory handler
-               $dirInstance->closeDirectory();
-               unset($dirInstance);
-
-               // Output counter in debug mode
-               if (defined('DEBUG_MODE')) print(sprintf("[%s:] <strong>%d</strong> Klassendateien in <strong>%d</strong> Verzeichnissen gefunden und geladen.<br />\n",
-                       __CLASS__,
-                       $this->classCnt,
-                       $this->dirCnt
-               ));
-       }
-
-       /**
-        * Includes all found classes
-        * @return      void
-        */
-       public function includeAllClasses () {
-               if (is_object($this->classes)) {
-                       // Load all classes
-                       for ($idx = $this->classes->getIterator(); $idx->valid(); $idx->next()) {
-                               // Load current class
-                               //* DEBUG: */ print "Class=".$idx->current()."\n";
-                               require_once($idx->current());
-                       } // END - for
-
-                       // Re-initialize the classes list
-                       $this->classes = new ArrayObject();
-               } // END - if
+               }
        }
 
        /**
        }
 
        /**
@@ -279,7 +181,7 @@ class ClassLoader {
 
                // Set new prefix (temporary!)
                $this->prefix = "config-";
 
                // Set new prefix (temporary!)
                $this->prefix = "config-";
-               $this->preLen = strlen($this->prefix);
+               $this->prefixLen = strlen($this->prefix);
 
                // Set base directory
                $basePath = sprintf("%sinc/config/", PATH);
 
                // Set base directory
                $basePath = sprintf("%sinc/config/", PATH);
@@ -289,14 +191,33 @@ class ClassLoader {
 
                // Set the prefix back
                $this->prefix = $oldPrefix;
 
                // Set the prefix back
                $this->prefix = $oldPrefix;
-               $this->preLen = strlen($this->prefix);
+               $this->prefixLen = strlen($this->prefix);
        }
        }
-}
 
 
-// Initial load of core classes and the FrameworkDirectoryPointer class
-require_once(sprintf("%sinc/classes/interfaces/class_FrameworkInterface%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
-require_once(sprintf("%sinc/classes/main/class_BaseFrameworkSystem%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
-require_once(sprintf("%sinc/classes/main/io/class_FrameworkDirectoryPointer%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
+       /**
+        * Tries to find the given class in our list. This method ignores silently
+        * missing classes or interfaces. So if you use class_exists() this method
+        * does not interrupt your program.
+        *
+        * @param       $className      The class we shall load
+        * @return      void
+        */
+       public function includeClass ($className) {
+               // Create a name with prefix and suffix
+               $fileName = $this->prefix . $className . $this->suffix;
+
+               // Now look it up in our index
+               if (isset($this->classes[$fileName])) {
+                       if ($this->classes[$fileName] != "loaded") {
+                               // File is found so load it only once
+                               require($this->classes[$fileName]);
+
+                               // Mark it as loaded
+                               $this->classes[$fileName] = "loaded";
+                       } // END - if
+               } // END - if
+       }
+}
 
 // [EOF]
 ?>
 
 // [EOF]
 ?>