* @version 0.0.0 * @copyright Copyright (c) 2007 - 2009 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * ---------------------------------- * 1.3 * - Constructor is now empty and factory method 'createClassLoader' is created * - renamed loadClasses to scanClassPath * 1.2 * - ClassLoader rewritten to PHP SPL's own RecursiveIteratorIterator class * 1.1 * - loadClasses rewritten to fix some notices * 1.0 * - Initial release * ---------------------------------- */ class ClassLoader { /** * Instance of this class */ private static $selfInstance = null; /** * Configuration array */ private $cfg = array(); /** * Array with all classes */ private $classes = array(); /** * List of loaded classes */ private $loadedClasses = array(); /** * Suffix with extension for all class files */ private $prefix = "class_"; /** * Suffix with extension for all class files */ private $suffix = ".php"; /** * A list for directory names (no leading/trailing slashes!) which not be scanned by the path scanner * @see scanLocalPath */ private $ignoreList = array(); /** * Debug this class loader? (true = yes, false = no) */ private $debug = false; /** * Wether the file list is cached or not */ 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 = ""; /** * Counter for loaded include files */ private $total = 0; /** * The protected constructor. Please use the factory method below, or use * getInstance() for singleton * * @return void */ protected function __construct () { // Is Currently empty } /** * Our renamed factory method * * @param $cfgInstance Configuration class instance * @return void */ public final static function createClassLoader (FrameworkConfiguration $cfgInstance) { // Set configuration instance $this->cfgInstance = $cfgInstance; // Construct the FQFN for the cache if (!defined('DEVELOPER')) { $this->listCacheFQFN = $this->cfgInstance->readConfig('local_db_path') . "list-" . $this->cfgInstance->readConfig('app_name') . ".cache"; $this->classCacheFQFN = $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'); // 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 } /** * Autoload-function * * @param $className Name of the class to load * @return void */ public static function autoLoad ($className) { // Try to include this class self::getInstance()->includeClass($className); } /** * Getter for an instance of this class * * @return $selfInstance An instance of this class */ public final static function getInstance () { // Is the instance there? if (is_null(self::$selfInstance)) { // Get a new one self::$selfInstance = ClassLoader::createClassLoader(FrameworkConfiguration::getInstance()); } // END - if // Return the instance return self::$selfInstance; } /** * 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 } /** * Fall-back method. Please replace loadClasses() with scanClassPath() ! * * @param $basePath The relative base path to 'base_path' constant for all classes * @param $ignoreList An optional list (array forced) of directory and file names which shall be ignored * @return void * @deprecated * @todo Rewrite your apps to scanClassPath() */ public function loadClasses ($basePath, array $ignoreList = array() ) { // This outputs an ugly message because you need to change to scanClassPath print __METHOD__." is deprecated. Use scanClassPath() to make this warning go away.
\n"; // Call our new method $this->scanClassPath($basePath, $ignoreList); } /** * Scans recursively a local path for class files which must have a prefix and a suffix as given by $this->suffix and $this->prefix * * @param $basePath The relative base path to 'base_path' constant for all classes * @param $ignoreList An optional list (array forced) of directory and file names which shall be ignored * @return void */ public function scanClassPath ($basePath, array $ignoreList = array() ) { // Is a list has been restored from cache, don't read it again if ($this->listCached === true) { // Abort here return; } // Directories which our class loader ignores by default while // deep-scanning the directory structure. $ignoreList[] = "."; $ignoreList[] = ".."; $ignoreList[] = ".htaccess"; $ignoreList[] = ".svn"; // Keep it in class for later usage $this->ignoreList = $ignoreList; // Set base directory which holds all our classes, we should use an // absolute path here so is_dir(), is_file() and so on will always // find the correct files and dirs. $basePath2 = realpath($basePath); // If the basePath is false it is invalid if ($basePath2 === false) { /* @todo: Do not die here. */ die("Cannot read {$basePath} !"); } else { // Set base path $basePath = $basePath2; } // Get a new iterator //* DEBUG: */ echo "Base path: {$basePath}
\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}
\n"; if ((!in_array($fileName, $this->ignoreList)) && (substr($fileName, 0, strlen($this->prefix)) == $this->prefix) && (substr($fileName, -strlen($this->suffix), strlen($this->suffix)) == $this->suffix)) { // Get the FQFN and add it to our class list $fqfn = $entry->getRealPath(); //* DEBUG: */ echo "ADD: {$fileName}
\n"; $this->classes[$fileName] = $fqfn; } // END - if } // END - foreach } /** * Load extra config files * * @return void */ public function loadExtraConfigs () { // Backup old prefix $oldPrefix = $this->prefix; // Set new prefix (temporary!) $this->prefix = "config-"; // Set base directory $basePath = sprintf("%sinc/config/", $this->cfgInstance->readConfig('base_path')); // Load all classes from the config directory $this->scanClassPath($basePath); // Include these extra configs now $this->includeExtraConfigs(); // Set the prefix back $this->prefix = $oldPrefix; } /** * 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])) && (!in_array($this->classes[$fileName], $this->loadedClasses))) { // File is found and not loaded so load it only once //* DEBUG: */ echo "LOAD: ".$fileName." - Start
\n"; require($this->classes[$fileName]); //* DEBUG: */ echo "LOAD: ".$fileName." - End
\n"; // Count this include $this->total++; // Mark this class as loaded $this->loadedClasses[] = $this->classes[$fileName]; // Remove it from classes list unset($this->classes[$fileName]); // Developer mode excludes caching (better debugging) if (!defined('DEVELOPER')) { // Reset cache $this->classesCached = false; } // END - if } // END - if } /** * Includes all extra config files * * @return void */ private function includeExtraConfigs () { // Run through all class names (should not be much) foreach ($this->classes as $fileName => $fqfn) { // Is this a config? if (substr($fileName, 0, strlen($this->prefix)) == $this->prefix) { // Then include it require($fqfn); // Remove it from the list unset($this->classes[$fileName]); } // END - if } // END - foreach } /** * Getter for total include counter * * @return $total Total loaded include files */ public final function getTotal () { return $this->total; } /** * Getter for a printable list of included classes/interfaces/exceptions * * @param $includeList A printable include list */ public function getPrintableIncludeList () { // Prepare the list $includeList = ""; foreach ($this->loadedClasses as $classFile) { $includeList .= basename($classFile)."
\n"; } // END - foreach // And return it return $includeList; } } // [EOF] ?>