3 * This class loads class include files with a specific prefix and suffix
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * ----------------------------------
26 * - ClassLoader rewritten to PHP SPL's own RecursiveIteratorIterator class
28 * - loadClasses rewritten to fix some notices
31 * ----------------------------------
35 * Instance of this class
37 private static $selfInstance = null;
42 private $cfg = array();
45 * Array with all classes
47 private $classes = array();
50 * List of loaded classes
52 private $loadedClasses = array();
55 * Suffix with extension for all class files
57 private $prefix = "class_";
60 * Suffix with extension for all class files
62 private $suffix = ".php";
65 * Length of the suffix. Will be overwritten later.
67 private $suffixLen = 0;
70 * Length of the prefix. Will be overwritten later.
72 private $prefixLen = 0;
75 * A list for directory names (no leading/trailing slashes!) which not be scanned by the path scanner
78 private $ignoreList = array();
81 * Debug this class loader? (true = yes, false = no)
83 private $debug = false;
86 * Wether the file list is cached or not
88 private $listCached = false;
91 * Wethe class content has been cached
93 private $classesCached = false;
96 * Filename for the list cache
98 private $listCacheFQFN = "";
101 * Cache for class content
103 private $classCacheFQFN = "";
106 * The *public* constructor
108 * @param $cfgInstance Configuration class instance
111 public function __construct (FrameworkConfiguration $cfgInstance) {
112 // Set configuration instance
113 $this->cfgInstance = $cfgInstance;
115 // Construct the FQFN for the cache
116 if (!defined('DEVELOPER')) {
117 $this->listCacheFQFN = PATH . $this->cfgInstance->readConfig('local_db_path') . "list-" . $this->cfgInstance->readConfig('app_name') . ".cache";
118 $this->classCacheFQFN = PATH . $this->cfgInstance->readConfig('local_db_path') . "class-" . $this->cfgInstance->readConfig('app_name') . ".cache";
121 // Set suffix and prefix from configuration
122 $this->suffix = $cfgInstance->readConfig('class_suffix');
123 $this->prefix = $cfgInstance->readConfig('class_prefix');
125 // Estimate length of prefix and suffix for substr() function (cache)
126 $this->suffixLen = strlen($this->suffix);
127 $this->prefixLen = strlen($this->prefix);
130 self::$selfInstance = $this;
132 // Skip here if no dev-mode
133 if (defined('DEVELOPER')) return;
135 // IS the cache there?
136 if (file_exists($this->listCacheFQFN)) {
138 $cacheContent = file_get_contents($this->listCacheFQFN);
141 $this->classes = unserialize($cacheContent);
143 // List has been restored from cache!
144 $this->listCached = true;
147 // Does the class cache exist?
148 if (file_exists($this->classCacheFQFN)) {
150 require($this->classCacheFQFN);
152 // Mark the class cache as loaded
153 $this->classesCached = true;
158 * The destructor makes it sure all caches got flushed
162 public function __destruct () {
163 // Skip here if dev-mode
164 if (defined('DEVELOPER')) return;
166 // Skip here if already cached
167 if ($this->listCached === false) {
168 // Writes the cache file of our list away
169 $cacheContent = serialize($this->classes);
170 file_put_contents($this->listCacheFQFN, $cacheContent);
173 // Skip here if already cached
174 if ($this->classesCached === false) {
175 // Generate a full-cache of all classes
177 foreach ($this->loadedClasses as $fqfn) {
179 $cacheContent .= file_get_contents($fqfn);
183 file_put_contents($this->classCacheFQFN, $cacheContent);
188 * Getter for an instance of this class
190 * @return $selfInstance An instance of this class
192 public final static function getInstance () {
193 // Is the instance there?
194 if (is_null(self::$selfInstance)) {
196 self::$selfInstance = new ClassLoader(FrameworkConfiguration::getInstance());
199 // Return the instance
200 return self::$selfInstance;
204 * Scans recursively a local path for class files which must have a prefix and a suffix as given by $this->suffix and $this->prefix
206 * @param $basePath The relative base path to PATH constant for all classes
207 * @param $ignoreList An optional list (array or string) of directory names which shall be ignored
210 public function loadClasses ($basePath, $ignoreList = array() ) {
211 // Is a list has been restored from cache, don't read it again
212 if ($this->listCached === true) {
217 // Convert string to array
218 if (!is_array($ignoreList)) $ignoreList = array($ignoreList);
220 // Directories which our class loader ignores by default while
221 // deep-scanning the directory structure. See scanLocalPath() for
224 $ignoreList[] = "..";
225 $ignoreList[] = ".htaccess";
226 $ignoreList[] = ".svn";
228 // Keep it in class for later usage
229 $this->ignoreList = $ignoreList;
231 // Set base directory which holds all our classes, we should use an
232 // absolute path here so is_dir(), is_file() and so on will always
233 // find the correct files and dirs.
234 $basePath2 = realpath($basePath);
236 // If the basePath is false it is invalid
237 if ($basePath2 === false) {
238 // TODO: Do not die here.
239 die("Cannot read {$basePath} !");
242 $basePath = $basePath2;
245 // Get a new iterator
246 //* DEBUG: */ echo "<strong>Base path: {$basePath}</strong><br />\n";
247 $iterator = new RecursiveDirectoryIterator($basePath);
248 $recursive = new RecursiveIteratorIterator($iterator);
249 foreach ($recursive as $entry) {
250 // Get filename from iterator
251 $fileName = $entry->getFileName();
253 // Is this file wanted?
254 //* DEBUG: */ echo "FOUND:{$fileName}<br />\n";
255 if ((!in_array($fileName, $this->ignoreList)) && (substr($fileName, 0, $this->prefixLen) == $this->prefix) && (substr($fileName, -$this->suffixLen, $this->suffixLen) == $this->suffix)) {
256 // Get the FQFN and add it to our class list
257 $fqfn = $entry->getRealPath();
258 //* DEBUG: */ echo "ADD: {$fileName}<br />\n";
259 $this->classes[$fileName] = $fqfn;
265 * Load extra config files
269 public function loadExtraConfigs () {
271 $oldPrefix = $this->prefix;
273 // Set new prefix (temporary!)
274 $this->prefix = "config-";
275 $this->prefixLen = strlen($this->prefix);
277 // Set base directory
278 $basePath = sprintf("%sinc/config/", PATH);
280 // Load all classes from the config directory
281 $this->loadClasses($basePath);
283 // Include these extra configs now
284 $this->includeExtraConfigs();
286 // Set the prefix back
287 $this->prefix = $oldPrefix;
288 $this->prefixLen = strlen($this->prefix);
293 * Tries to find the given class in our list. This method ignores silently
294 * missing classes or interfaces. So if you use class_exists() this method
295 * does not interrupt your program.
297 * @param $className The class we shall load
300 public function includeClass ($className) {
301 // Create a name with prefix and suffix
302 $fileName = $this->prefix . $className . $this->suffix;
304 // Now look it up in our index
305 if (isset($this->classes[$fileName])) {
306 // File is found so load it only once
307 require($this->classes[$fileName]);
309 // Developer mode excludes caching (better debugging)
310 if (!defined('DEVELOPER')) {
311 // Mark this class as loaded
312 $this->loadedClasses[] = $this->classes[$fileName];
315 $this->classesCached = false;
321 * Includes all extra config files
325 private function includeExtraConfigs () {
326 // Run through all class names (should not be much)
327 foreach ($this->classes as $fileName=>$fqfn) {
329 if (substr($fileName, 0, $this->prefixLen) == $this->prefix) {
333 // Remove it from the list
334 unset($this->classes[$fileName]);