]> git.mxchange.org Git - core.git/blobdiff - framework/loader/class_ClassLoader.php
Continued:
[core.git] / framework / loader / class_ClassLoader.php
index 15a7361221db1a893fcc92fda4dd9c87c13de6fe..3f4422cd291a5c5864ffd61defac14b9fe2fdf29 100644 (file)
@@ -4,19 +4,21 @@ namespace Org\Mxchange\CoreFramework\Loader;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 
 // Import SPL stuff
 use \InvalidArgumentException;
 use \RecursiveDirectoryIterator;
 use \RecursiveIteratorIterator;
 use \SplFileInfo;
+use \UnexpectedValueException;
 
 /**
  * This class loads class include files with a specific prefix and suffix
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
- * @version            1.6.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
+ * @version            1.7.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -34,6 +36,10 @@ use \SplFileInfo;
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  *
  * ----------------------------------
+ * 1.7.0
+ *  - "Cached" more like config instance and root/application base path for
+ *    shorter call stacks and lesser methods invoked
+ *  - More debug logging
  * 1.6.0
  *  - This class loader is now 100% singleton, no other instance is really
  *    required, therefore the factory method can be removed safely
@@ -67,9 +73,14 @@ final class ClassLoader {
        private static $selfInstance = NULL;
 
        /**
-        * Cached configuration entry 'developer_mode_enabled'
+        * Instance of a FrameworkConfiguration class
         */
-       private static $developerModeEnabled = false;
+       private static $configInstance = NULL;
+
+       /**
+        * Cached configuration entry 'is_developer_mode_enabled'
+        */
+       private static $developerModeEnabled = NULL;
 
        /**
         * Array with all valid but pending for loading file names (class,
@@ -150,6 +161,11 @@ final class ClassLoader {
         */
        private static $testPaths = [];
 
+       /**
+        * Cached includes that needs to be flushed
+        */
+       private $flushCache = [];
+
        /**
         * The protected constructor. Please use the factory method below, or use
         * getSelfInstance() for singleton
@@ -157,8 +173,18 @@ final class ClassLoader {
         * @return      void
         */
        private function __construct () {
-               // Cache config entry
-               self::$developerModeEnabled = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('developer_mode_enabled');
+               // Is developerModeEnabled set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CONSTRUCTED!' . PHP_EOL, __METHOD__, __LINE__);
+               if (is_null(self::$developerModeEnabled)) {
+                       // Cache config instance
+                       self::$configInstance = FrameworkBootstrap::getConfigurationInstance();
+
+                       // Cache config entry
+                       self::$developerModeEnabled = self::$configInstance->isEnabled('developer_mode');
+               }
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::developerModeEnabled=%d - EXIT!' . PHP_EOL, __METHOD__, __LINE__, intval(self::$developerModeEnabled));
        }
 
        /**
@@ -168,41 +194,67 @@ final class ClassLoader {
         */
        public function __destruct () {
                // Skip here if dev-mode
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::developerModeEnabled=%d - DESTRUCTED!' . PHP_EOL, __METHOD__, __LINE__, intval(self::$developerModeEnabled));
                if (self::$developerModeEnabled) {
+                       // Is enabled, don't cache
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Developer mode enabled, not caching classes - EXIT!' . PHP_EOL, __METHOD__, __LINE__);
                        return;
                }
 
+               // Init content
+               $cacheContent = '';
+
                // Skip here if already cached
+               //* NOISY-DEBUG: */ printf('[%s:%d]: this->listCached=%d' . PHP_EOL, __METHOD__, __LINE__, intval($this->listCached));
                if ($this->listCached === false) {
                        // Writes the cache file of our list away
                        $cacheContent = json_encode($this->pendingFiles);
 
                        // Open cache instance
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: cacheContent()=%d' . PHP_EOL, __METHOD__, __LINE__, strlen($cacheContent));
                        $fileObject = $this->listCacheFile->openFile('w');
 
                        // And write whole list
                        $fileObject->fwrite($cacheContent);
+
+                       // Close it
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Closing file %s ...' . PHP_EOL, __METHOD__, __LINE__, $fileObject->getPathName());
+                       unset($fileObject);
                }
 
+               // Init content
+               $cacheContent = '';
+
                // Skip here if already cached
+               //* NOISY-DEBUG: */ printf('[%s:%d]: this->classesCached=%d' . PHP_EOL, __METHOD__, __LINE__, intval($this->classesCached));
                if ($this->classesCached === false) {
                        // Generate a full-cache of all classes
-                       $cacheContent = '';
-                       foreach (array_keys($this->loadedClasses) as $fileInstance) {
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: this->flushCache()=%d' . PHP_EOL, __METHOD__, __LINE__, count($this->flushCache));
+                       foreach ($this->flushCache as $key => $fileInstance) {
                                // Open file
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: key=%s,fileInstance[]=%s' . PHP_EOL, __METHOD__, __LINE__, $key, gettype($fileInstance));
                                $fileObject = $fileInstance->openFile('r');
 
                                // Load the file
                                // @TODO Add some uglifying code (compress) here
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: Adding fileInstance->size=%d bytes ...' . PHP_EOL, __METHOD__, __LINE__, $fileInstance->getSize());
                                $cacheContent .= $fileObject->fread($fileInstance->getSize());
                        }
 
                        // Open file
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: cacheContent()=%d' . PHP_EOL, __METHOD__, __LINE__, strlen($cacheContent));
                        $fileObject = $this->classCacheFile->openFile('w');
 
                        // And write it away
                        $fileObject->fwrite($cacheContent);
+
+                       // Close it
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Closing file %s ...' . PHP_EOL, __METHOD__, __LINE__, $fileObject->getPathName());
+                       unset($fileObject);
                }
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -216,8 +268,7 @@ final class ClassLoader {
                $loaderInstance = self::getSelfInstance();
 
                // "Cache" configuration instance and framework base path
-               $configInstance = FrameworkBootstrap::getConfigurationInstance();
-               $frameworkBasePath = $configInstance->getConfigEntry('framework_base_path');
+               $frameworkBasePath = self::$configInstance->getConfigEntry('framework_base_path');
 
                // Load all classes
                //* NOISY-DEBUG: */ printf('[%s:%d]: frameworkBasePath=%s,self::$frameworkPaths()=%d,' . PHP_EOL, __METHOD__, __LINE__, $frameworkBasePath, count(self::$frameworkPaths));
@@ -254,71 +305,47 @@ final class ClassLoader {
         * Scans for application's classes, etc.
         *
         * @return      void
+        * @throws      UnexpectedValueException        If a given path isn't one or not readable
         */
        public static function scanApplicationClasses () {
                // Get loader instance
                //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $loaderInstance = self::getSelfInstance();
 
-               // "Cache" configuration instance
-               $configInstance = FrameworkBootstrap::getConfigurationInstance();
+               // "Cache" application base path
+               $basePath = self::$configInstance->getConfigEntry('application_base_path');
 
                // Load all classes for the application
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::frameworkPaths()=%d,basePath=%s' . PHP_EOL, __METHOD__, __LINE__, count(self::$frameworkPaths), $basePath);
                foreach (self::$frameworkPaths as $shortPath) {
                        // Create path name
                        //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
-                       $pathName = realpath(sprintf(
+                       $realPathName = realpath(sprintf(
                                '%s%s%s%s%s',
-                               $configInstance->getConfigEntry('application_base_path'),
+                               $basePath,
                                DIRECTORY_SEPARATOR,
                                FrameworkBootstrap::getDetectedApplicationName(),
                                DIRECTORY_SEPARATOR,
                                $shortPath
                        ));
 
-                       // Is the path readable?
-                       //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
-                       if (is_dir($pathName)) {
-                               // Try to load the application classes
-                               $loaderInstance->scanClassPath($pathName);
-                       }
-               }
-
-               // Trace message
-               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
-       }
-
-       /**
-        * Scans for test classes, etc.
-        *
-        * @return      void
-        */
-       public static function scanTestsClasses () {
-               // "Cache" configuration instance
-               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
-               $configInstance = FrameworkBootstrap::getConfigurationInstance();
-
-               // Load all classes for the application
-               foreach (self::$testPaths as $shortPath) {
-                       // Construct path name
-                       //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
-                       $pathName = sprintf(
-                               '%s%s%s',
-                               $configInstance->getConfigEntry('root_base_path'),
-                               DIRECTORY_SEPARATOR,
-                               $shortPath
-                       );
-
-                       // Try to find it
-                       //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s - BEFORE!' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
-                       $realPathName = realpath($pathName);
-
                        // Is the path readable?
                        //* NOISY-DEBUG: */ printf('[%s:%d]: realPathName[%s]=%s - AFTER!' . PHP_EOL, __METHOD__, __LINE__, gettype($realPathName), $realPathName);
-                       if ((is_dir($realPathName)) && (is_readable($realPathName))) {
-                               // Try to load the application classes
-                               ClassLoader::getSelfInstance()->scanClassPath($realPathName);
+                       if (!is_string($realPathName)) {
+                               // Skip this cone
+                               //* NOISY-DEBUG: */ printf('[%s:%d]: realPathName[]=%s - SKIPPED!' . PHP_EOL, __METHOD__, __LINE__, gettype($realPathName));
+                               continue;
+                       } elseif (!is_dir($realPathName)) {
+                               // Is not a directory
+                               throw new UnexpectedValueException(sprintf('realPathName=%s is not a directory', $realPathName), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
+                       } elseif (!is_readable($realPathName)) {
+                               // Not readable
+                               throw new UnexpectedValueException(sprintf('realPathName=%s cannot be read from', $realPathName), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
                        }
+
+                       // Try to load the application classes
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Scanning for classes/interfaces at realPathName=%s ...' . PHP_EOL, __METHOD__, __LINE__, $realPathName);
+                       $loaderInstance->scanClassPath($realPathName);
                }
 
                // Trace message
@@ -335,22 +362,6 @@ final class ClassLoader {
                self::$strictNamingConvention = $strictNamingConvention;
        }
 
-       /**
-        * Registeres given relative path where test classes reside. For regular
-        * framework uses, they should not be loaded (and used).
-        *
-        * @param       $relativePath   Relative path to test classes
-        * @return      void
-        */
-       public static function registerTestsPath (string $relativePath) {
-               // "Register" it
-               //* NOISY-DEBUG: */ printf('[%s:%d]: relativePath=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $relativePath);
-               self::$testPaths[$relativePath] = $relativePath;
-
-               // Trace message
-               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
-       }
-
        /**
         * Autoload-function
         *
@@ -363,20 +374,21 @@ final class ClassLoader {
                //* NOISY-DEBUG: */ printf('[%s:%d] className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $className);
                if (empty($className)) {
                        // Should not be empty
-                       throw new InvalidArgumentException('Parameter "className" is empty');
+                       throw new InvalidArgumentException('Parameter "className" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
                // The class name MUST be at least Tld\Domain\Project\Package\SomeFooBar so split at them
                $classNameParts = explode("\\", $className);
 
                // At least 3 parts should be there
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::strictNamingConvention=%d,classNameParts()=%d' . PHP_EOL, __METHOD__, __LINE__, intval(self::$strictNamingConvention), count($classNameParts));
                if ((self::$strictNamingConvention === true) && (count($classNameParts) < 5)) {
                        // Namespace scheme is: Tld\Domain\Project\Package[\SubPackage...]
                        throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $className));
                }
 
                // Try to include this class
-               //* NOISY-DEBUG: */ printf('[%s:%d]: Calling self->loadClassFile(%s) ...' . PHP_EOL, __METHOD__, __LINE__, $className);
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking self->loadClassFile(%s) ...' . PHP_EOL, __METHOD__, __LINE__, $className);
                self::getSelfInstance()->loadClassFile($className);
 
                // Trace message
@@ -390,13 +402,16 @@ final class ClassLoader {
         */
        public static final function getSelfInstance () {
                // Is the instance there?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::selfInstance[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, gettype(self::$selfInstance));
                if (is_null(self::$selfInstance)) {
                        // Get a new one and initialize it
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing class loader ...' . PHP_EOL, __METHOD__, __LINE__);
                        self::$selfInstance = new ClassLoader();
                        self::$selfInstance->initClassLoader();
                }
 
                // Return the instance
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::selfInstance=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, get_class(self::$selfInstance));
                return self::$selfInstance;
        }
 
@@ -417,7 +432,7 @@ final class ClassLoader {
        public function getPrintableIncludeList () {
                // Prepare the list
                $includeList = '';
-               foreach ($this->loadedClasses as $classFile) {
+               foreach (array_keys($this->loadedClasses) as $classFile) {
                        $includeList .= basename($classFile) . '<br />' . PHP_EOL;
                }
 
@@ -431,11 +446,15 @@ final class ClassLoader {
         * @param       $basePath               The relative base path to 'framework_base_path' constant for all classes
         * @param       $ignoreList             An optional list (array forced) of directory and file names which shall be ignored
         * @return      void
+        * @throws      InvalidArgumentException If a parameter is invalid
         */
        protected function scanClassPath (string $basePath, array $ignoreList = [] ) {
                // Is a list has been restored from cache, don't read it again
                //* NOISY-DEBUG: */ printf('[%s:%d] basePath=%s,ignoreList()=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $basePath, count($ignoreList));
-               if ($this->listCached === true) {
+               if (empty($basePath)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "basePath" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif ($this->listCached === true) {
                        // Abort here
                        //* NOISY-DEBUG: */ printf('[%s:%d] this->listCache=true - EXIT!' . PHP_EOL, __METHOD__, __LINE__);
                        return;
@@ -449,16 +468,13 @@ final class ClassLoader {
                 * should be used here so is_dir(), is_file() and so on will always
                 * find the correct files and dirs.
                 */
-               $basePath2 = realpath($basePath);
+               $basePath = realpath($basePath);
 
                // If the basePath is false it is invalid
-               //* NOISY-DEBUG: */ printf('[%s:%d] basePath2[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($basePath2), $basePath2);
-               if ($basePath2 === false) {
+               //* NOISY-DEBUG: */ printf('[%s:%d] basePath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($basePath), $basePath);
+               if (!is_string($basePath)) {
                        /* @TODO: Do not exit here. */
                        exit(__METHOD__ . ': Cannot read ' . $basePath . ' !' . PHP_EOL);
-               } else {
-                       // Set base path
-                       $basePath = $basePath2;
                }
 
                // Get a new iterator
@@ -471,9 +487,11 @@ final class ClassLoader {
                        $currentEntry = $iteratorInstance->current();
 
                        // Get filename from iterator which is the class' name (according naming-convention)
+                       //* NOISY-DEBUG: */ printf('[%s:%d] currentEntry=%s,currentEntry->size=%d' . PHP_EOL, __METHOD__, __LINE__, $currentEntry->__toString(), $currentEntry->getSize());
                        $fileName = $currentEntry->getFilename();
 
                        // Current entry must be a file, not smaller than 100 bytes and not on ignore list
+                       //* NOISY-DEBUG: */ printf('[%s:%d] fileName=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
                        if (!$currentEntry->isFile() || isset($this->ignoreList[$fileName]) || $currentEntry->getSize() < 100) {
                                // Advance to next entry
                                $iteratorInstance->next();
@@ -509,28 +527,33 @@ final class ClassLoader {
         * @return      void
         */
        private function initClassLoader () {
-               // Construct the FQFN for the cache
-               if (!self::$developerModeEnabled) {
-                       // Init cache instances
-                       $this->listCacheFile  = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . 'list-' . FrameworkBootstrap::getDetectedApplicationName() . '.cache');
-                       $this->classCacheFile = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('local_database_path') . 'class-' . FrameworkBootstrap::getDetectedApplicationName() . '.cache');
-               }
-
                // Set suffix and prefix from configuration
-               $this->suffix = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('class_suffix');
-               $this->prefix = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('class_prefix');
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
+               $this->suffix = self::$configInstance->getConfigEntry('class_suffix');
+               $this->prefix = self::$configInstance->getConfigEntry('class_prefix');
 
                // Set own instance
+               //* NOISY-DEBUG: */ printf('[%s:%d]: this->suffix=%s,this->prefix=%s' . PHP_EOL, __METHOD__, __LINE__, $this->suffix, $this->prefix);
                self::$selfInstance = $this;
 
                // Skip here if no dev-mode
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::developerModeEnabled=%d' . PHP_EOL, __METHOD__, __LINE__, intval(self::$developerModeEnabled));
                if (self::$developerModeEnabled) {
+                       // Developer mode is enabled
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Developer mode is enabled - EXIT!' . PHP_EOL, __METHOD__, __LINE__);
                        return;
                }
 
+               // Init cache instances
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing cache file instances ...' . PHP_EOL, __METHOD__, __LINE__);
+               $this->listCacheFile  = new SplFileInfo(self::$configInstance->getConfigEntry('local_database_path') . 'list-' . FrameworkBootstrap::getDetectedApplicationName() . '.cache');
+               $this->classCacheFile = new SplFileInfo(self::$configInstance->getConfigEntry('local_database_path') . 'class-' . FrameworkBootstrap::getDetectedApplicationName() . '.cache');
+
                // Is the cache there?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Checking this->listCacheFile=%s ...' . PHP_EOL, __METHOD__, __LINE__, $this->listCacheFile);
                if (FrameworkBootstrap::isReadableFile($this->listCacheFile)) {
                        // Load and convert it
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Loading %s ...' . PHP_EOL, __METHOD__, __LINE__, $this->listCacheFile);
                        $this->pendingFiles = json_decode(file_get_contents($this->listCacheFile->getPathname()));
 
                        // List has been restored from cache!
@@ -538,13 +561,18 @@ final class ClassLoader {
                }
 
                // Does the class cache exist?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Checking this->classCacheFile=%s ...' . PHP_EOL, __METHOD__, __LINE__, $this->classCacheFile);
                if (FrameworkBootstrap::isReadableFile($this->classCacheFile)) {
                        // Then include it
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: Invoking FrameworkBootstrap::loadInclude(%s) ...' . PHP_EOL, __METHOD__, __LINE__, $this->classCacheFile);
                        FrameworkBootstrap::loadInclude($this->classCacheFile);
 
                        // Mark the class cache as loaded
                        $this->classesCached = true;
                }
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -558,16 +586,19 @@ final class ClassLoader {
         */
        private function loadClassFile (string $className) {
                // The class name should contain at least 2 back-slashes, so split at them
-               //* NOISY-DEBUG: */ printf('[%s:%d] className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $className);
+               //* NOISY-DEBUG: */ printf('[%s:%d]: className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $className);
                $classNameParts = explode("\\", $className);
 
                // Get last element
+               //* NOISY-DEBUG: */ printf('[%s:%d]: classNameParts()=%d' . PHP_EOL, __METHOD__, __LINE__, count($classNameParts));
                $shortClassName = array_pop($classNameParts);
 
                // Create a name with prefix and suffix
+               //* NOISY-DEBUG: */ printf('[%s:%d]: this->prefix=%s,shortClassName=%s,this->suffix=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $this->prefix, $shortClassName, $this->suffix);
                $fileName = sprintf('%s%s%s', $this->prefix, $shortClassName, $this->suffix);
 
                // Now look it up in our index
+               //* DEBUG-DIE: */ die(sprintf('[%s:%d]: this->pendingFiles=%s', __METHOD__, __LINE__, print_r($this->pendingFiles, TRUE)));
                //* NOISY-DEBUG: */ printf('[%s:%d] ISSET: fileName=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
                if ((isset($this->pendingFiles[$fileName])) && (!isset($this->loadedClasses[$this->pendingFiles[$fileName]->getPathname()]))) {
                        // File is found and not loaded so load it only once
@@ -579,18 +610,21 @@ final class ClassLoader {
                        $this->total++;
 
                        // Mark this class as loaded for other purposes than loading it.
+                       //* NOISY-DEBUG: */ printf('[%s:%d] LOAD: fileName=%s marked as loaded ...' . PHP_EOL, __METHOD__, __LINE__, $fileName);
                        $this->loadedClasses[$this->pendingFiles[$fileName]->getPathname()] = true;
 
-                       // Remove it from classes list so it won't be found twice.
-                       //* NOISY-DEBUG: */ printf('[%s:%d] UNSET: fileName=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
-                       unset($this->pendingFiles[$fileName]);
-
                        // Developer mode excludes caching (better debugging)
+                       //* NOISY-DEBUG: */ printf('[%s:%d] self::developerModeEnabled=%d' . PHP_EOL, __METHOD__, __LINE__, intval(self::$developerModeEnabled));
                        if (!self::$developerModeEnabled) {
-                               // Reset cache
-                               //* NOISY-DEBUG: */ printf('[%s:%d] classesCached=false' . PHP_EOL, __METHOD__, __LINE__);
+                               // Reset cache and mark file for flushing
+                               //* NOISY-DEBUG: */ printf('[%s:%d] Setting this->classesCached=false ...' . PHP_EOL, __METHOD__, __LINE__);
                                $this->classesCached = false;
+                               $this->flushCache[$fileName] = $this->pendingFiles[$fileName];
                        }
+
+                       // Remove it from classes list so it won't be found twice.
+                       //* NOISY-DEBUG: */ printf('[%s:%d] UNSET: fileName=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName);
+                       unset($this->pendingFiles[$fileName]);
                } else {
                        // Not found
                        //* NOISY-DEBUG: */ printf('[%s:%d] 404: fileName=%s' . PHP_EOL, __METHOD__, __LINE__, $fileName);