]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/loader/class_ClassLoader.php
Caching of class content and developer mode added
[shipsimu.git] / inc / loader / class_ClassLoader.php
index a1b2e8230e126e36d0af3dbde6aa0e02e7dd69a3..79e9f05ce1d9086fd7b057cc37ea356f493646ef 100644 (file)
  * ----------------------------------
  */
 class ClassLoader {
+       /**
+        * Instance of this class
+        */
+       private static $selfInstance = null;
+
        /**
         * Configuration array
         */
@@ -41,6 +46,11 @@ class ClassLoader {
         */
        private $classes = array();
 
+       /**
+        * List of loaded classes
+        */
+       private $loadedClasses = array();
+
        /**
         * Suffix with extension for all class files
         */
@@ -73,9 +83,24 @@ class ClassLoader {
        private $debug = false;
 
        /**
-        * Instance of this class
+        * Wether the file list is cached or not
         */
-       private static $selfInstance = null;
+       private $listCached = false;
+
+       /**
+        * Wethe class content has been cached
+        */
+       private $classesCached = false;
+
+       /**
+        * Filename for the list cache
+        */
+       private $listCacheFQFN = "";
+
+       /**
+        * Cache for class content
+        */
+       private $classCacheFQFN = "";
 
        /**
         * The *public* constructor
@@ -84,6 +109,15 @@ class ClassLoader {
         * @return      void
         */
        public function __construct (FrameworkConfiguration $cfgInstance) {
+               // Set configuration instance
+               $this->cfgInstance = $cfgInstance;
+
+               // Construct the FQFN for the cache
+               if (!defined('DEVELOPER')) {
+                       $this->listCacheFQFN  = PATH . $this->cfgInstance->readConfig('local_db_path') . "list-" . $this->cfgInstance->readConfig('app_name') . ".cache";
+                       $this->classCacheFQFN = PATH . $this->cfgInstance->readConfig('local_db_path') . "class-" . $this->cfgInstance->readConfig('app_name') . ".cache";
+               } // END - if
+
                // Set suffix and prefix from configuration
                $this->suffix = $cfgInstance->readConfig('class_suffix');
                $this->prefix = $cfgInstance->readConfig('class_prefix');
@@ -92,11 +126,62 @@ class ClassLoader {
                $this->suffixLen = strlen($this->suffix);
                $this->prefixLen = strlen($this->prefix);
 
-               // Set configuration instance
-               $this->cfgInstance = $cfgInstance;
-
                // Set own instance
                self::$selfInstance = $this;
+
+               // Skip here if no dev-mode
+               if (defined('DEVELOPER')) return;
+
+               // IS the cache there?
+               if (file_exists($this->listCacheFQFN)) {
+                       // Get content
+                       $cacheContent = file_get_contents($this->listCacheFQFN);
+
+                       // And convert it
+                       $this->classes = unserialize($cacheContent);
+
+                       // List has been restored from cache!
+                       $this->listCached = true;
+               } // END - if
+
+               // Does the class cache exist?
+               if (file_exists($this->classCacheFQFN)) {
+                       // Then include it
+                       require($this->classCacheFQFN);
+
+                       // Mark the class cache as loaded
+                       $this->classesCached = true;
+               } // END - if
+       }
+
+       /**
+        * The destructor makes it sure all caches got flushed
+        *
+        * @return      void
+        */
+       public function __destruct () {
+               // Skip here if dev-mode
+               if (defined('DEVELOPER')) return;
+
+               // Skip here if already cached
+               if ($this->listCached === false) {
+                       // Writes the cache file of our list away
+                       $cacheContent = serialize($this->classes);
+                       file_put_contents($this->listCacheFQFN, $cacheContent);
+               } // END - if
+
+               // Skip here if already cached
+               if ($this->classesCached === false) {
+                       // Generate a full-cache of all classes
+                       $cacheContent = "";
+                       foreach ($this->loadedClasses as $fqfn) {
+                               // Load the file
+                               $cacheContent .= file_get_contents($fqfn);
+                       } // END - foreach
+
+                       // And write it away
+                       file_put_contents($this->classCacheFQFN, $cacheContent);
+               } // END - if
        }
 
        /**
@@ -123,6 +208,12 @@ class ClassLoader {
         * @return      void
         */
        public function loadClasses ($basePath, $ignoreList = array() ) {
+               // Is a list has been restored from cache, don't read it again
+               if ($this->listCached === true) {
+                       // Abort here
+                       return;
+               }
+
                // Convert string to array
                if (!is_array($ignoreList)) $ignoreList = array($ignoreList);
 
@@ -166,8 +257,8 @@ class ClassLoader {
                                $fqfn = $entry->getRealPath();
                                //* DEBUG: */ echo "ADD: {$fileName}<br />\n";
                                $this->classes[$fileName] = $fqfn;
-                       }
-               }
+                       } // END - if
+               } // END - foreach
        }
 
        /**
@@ -208,12 +299,16 @@ class ClassLoader {
 
                // 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]);
+                       // File is found so load it only once
+                       require($this->classes[$fileName]);
+
+                       // Developer mode excludes caching (better debugging)
+                       if (!defined('DEVELOPER')) {
+                               // Mark this class as loaded
+                               $this->loadedClasses[] = $this->classes[$fileName];
 
-                               // Mark it as loaded
-                               $this->classes[$fileName] = "loaded";
+                               // Reset cache
+                               $this->classesCached = false;
                        } // END - if
                } // END - if
        }