* @version 0.0.0
* @copyright Copyright(c) 2007, 2008 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.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";
/**
* Length of the suffix. Will be overwritten later.
*/
private $suffixLen = 0;
/**
* Length of the prefix. Will be overwritten later.
*/
private $prefixLen = 0;
/**
* 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 = "";
/**
* The *public* constructor
*
* @param $cfgInstance Configuration class instance
* @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');
// Estimate length of prefix and suffix for substr() function (cache)
$this->suffixLen = strlen($this->suffix);
$this->prefixLen = strlen($this->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
}
/**
* 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
}
/**
* 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 = new ClassLoader(FrameworkConfiguration::getInstance());
} // END - if
// Return the instance
return self::$selfInstance;
}
/**
* 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 PATH constant for all classes
* @param $ignoreList An optional list (array or string) of directory names which shall be ignored
* @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);
// Directories which our class loader ignores by default while
// deep-scanning the directory structure. See scanLocalPath() for
// details.
$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.
debug_print_backtrace();
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, $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}
\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-";
$this->prefixLen = strlen($this->prefix);
// Set base directory
$basePath = sprintf("%sinc/config/", PATH);
// Load all classes from the config directory
$this->loadClasses($basePath);
// Include these extra configs now
$this->includeExtraConfigs();
// Set the prefix back
$this->prefix = $oldPrefix;
$this->prefixLen = strlen($this->prefix);
}
/**
* 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])) {
// 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];
// 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, $this->prefixLen) == $this->prefix) {
// Then include it
require($fqfn);
// Remove it from the list
unset($this->classes[$fileName]);
} // END - if
} // END - foreach
}
}
// [EOF]
?>