* @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 {
/**
* Configuration array
*/
private $cfg = array();
/**
* Array with all classes
*/
private $classes = 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;
/**
* Instance of this class
*/
private static $selfInstance = null;
/**
* The *public* constructor
*
* @param $cfgInstance Configuration class instance
* @return void
*/
public function __construct (FrameworkConfiguration $cfgInstance) {
// 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 configuration instance
$this->cfgInstance = $cfgInstance;
// Set own instance
self::$selfInstance = $this;
}
/**
* 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() ) {
// 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.
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;
}
}
}
/**
* 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);
// 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])) {
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]
?>