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 * Counter for loaded include files
111 * The *public* constructor
113 * @param $cfgInstance Configuration class instance
116 public function __construct (FrameworkConfiguration $cfgInstance) {
117 // Set configuration instance
118 $this->cfgInstance = $cfgInstance;
120 // Construct the FQFN for the cache
121 if (!defined('DEVELOPER')) {
122 $this->listCacheFQFN = PATH . $this->cfgInstance->readConfig('local_db_path') . "list-" . $this->cfgInstance->readConfig('app_name') . ".cache";
123 $this->classCacheFQFN = PATH . $this->cfgInstance->readConfig('local_db_path') . "class-" . $this->cfgInstance->readConfig('app_name') . ".cache";
126 // Set suffix and prefix from configuration
127 $this->suffix = $cfgInstance->readConfig('class_suffix');
128 $this->prefix = $cfgInstance->readConfig('class_prefix');
130 // Estimate length of prefix and suffix for substr() function (cache)
131 $this->suffixLen = strlen($this->suffix);
132 $this->prefixLen = strlen($this->prefix);
135 self::$selfInstance = $this;
137 // Skip here if no dev-mode
138 if (defined('DEVELOPER')) return;
140 // IS the cache there?
141 if (file_exists($this->listCacheFQFN)) {
143 $cacheContent = file_get_contents($this->listCacheFQFN);
146 $this->classes = unserialize($cacheContent);
148 // List has been restored from cache!
149 $this->listCached = true;
152 // Does the class cache exist?
153 if (file_exists($this->classCacheFQFN)) {
155 require($this->classCacheFQFN);
157 // Mark the class cache as loaded
158 $this->classesCached = true;
163 * The destructor makes it sure all caches got flushed
167 public function __destruct () {
168 // Skip here if dev-mode
169 if (defined('DEVELOPER')) return;
171 // Skip here if already cached
172 if ($this->listCached === false) {
173 // Writes the cache file of our list away
174 $cacheContent = serialize($this->classes);
175 file_put_contents($this->listCacheFQFN, $cacheContent);
178 // Skip here if already cached
179 if ($this->classesCached === false) {
180 // Generate a full-cache of all classes
182 foreach ($this->loadedClasses as $fqfn) {
184 $cacheContent .= file_get_contents($fqfn);
188 file_put_contents($this->classCacheFQFN, $cacheContent);
193 * Getter for an instance of this class
195 * @return $selfInstance An instance of this class
197 public final static function getInstance () {
198 // Is the instance there?
199 if (is_null(self::$selfInstance)) {
201 self::$selfInstance = new ClassLoader(FrameworkConfiguration::getInstance());
204 // Return the instance
205 return self::$selfInstance;
209 * Scans recursively a local path for class files which must have a prefix and a suffix as given by $this->suffix and $this->prefix
211 * @param $basePath The relative base path to PATH constant for all classes
212 * @param $ignoreList An optional list (array or string) of directory names which shall be ignored
215 public function loadClasses ($basePath, $ignoreList = array() ) {
216 // Is a list has been restored from cache, don't read it again
217 if ($this->listCached === true) {
222 // Convert string to array
223 if (!is_array($ignoreList)) $ignoreList = array($ignoreList);
225 // Directories which our class loader ignores by default while
226 // deep-scanning the directory structure. See scanLocalPath() for
229 $ignoreList[] = "..";
230 $ignoreList[] = ".htaccess";
231 $ignoreList[] = ".svn";
233 // Keep it in class for later usage
234 $this->ignoreList = $ignoreList;
236 // Set base directory which holds all our classes, we should use an
237 // absolute path here so is_dir(), is_file() and so on will always
238 // find the correct files and dirs.
239 $basePath2 = realpath($basePath);
241 // If the basePath is false it is invalid
242 if ($basePath2 === false) {
243 /* @TODO: Do not die here. */
244 die("Cannot read {$basePath} !");
247 $basePath = $basePath2;
250 // Get a new iterator
251 //* DEBUG: */ echo "<strong>Base path: {$basePath}</strong><br />\n";
252 $iterator = new RecursiveDirectoryIterator($basePath);
253 $recursive = new RecursiveIteratorIterator($iterator);
254 foreach ($recursive as $entry) {
255 // Get filename from iterator
256 $fileName = $entry->getFileName();
258 // Is this file wanted?
259 //* DEBUG: */ echo "FOUND:{$fileName}<br />\n";
260 if ((!in_array($fileName, $this->ignoreList)) && (substr($fileName, 0, $this->prefixLen) == $this->prefix) && (substr($fileName, -$this->suffixLen, $this->suffixLen) == $this->suffix)) {
261 // Get the FQFN and add it to our class list
262 $fqfn = $entry->getRealPath();
263 //* DEBUG: */ echo "ADD: {$fileName}<br />\n";
264 $this->classes[$fileName] = $fqfn;
270 * Load extra config files
274 public function loadExtraConfigs () {
276 $oldPrefix = $this->prefix;
278 // Set new prefix (temporary!)
279 $this->prefix = "config-";
280 $this->prefixLen = strlen($this->prefix);
282 // Set base directory
283 $basePath = sprintf("%sinc/config/", PATH);
285 // Load all classes from the config directory
286 $this->loadClasses($basePath);
288 // Include these extra configs now
289 $this->includeExtraConfigs();
291 // Set the prefix back
292 $this->prefix = $oldPrefix;
293 $this->prefixLen = strlen($this->prefix);
298 * Tries to find the given class in our list. This method ignores silently
299 * missing classes or interfaces. So if you use class_exists() this method
300 * does not interrupt your program.
302 * @param $className The class we shall load
305 public function includeClass ($className) {
306 // Create a name with prefix and suffix
307 $fileName = $this->prefix . $className . $this->suffix;
309 // Now look it up in our index
310 if (isset($this->classes[$fileName])) {
311 // File is found so load it only once
312 require($this->classes[$fileName]);
314 // Count this include
317 // Mark this class as loaded
318 $this->loadedClasses[] = $this->classes[$fileName];
320 // Developer mode excludes caching (better debugging)
321 if (!defined('DEVELOPER')) {
323 $this->classesCached = false;
329 * Includes all extra config files
333 private function includeExtraConfigs () {
334 // Run through all class names (should not be much)
335 foreach ($this->classes as $fileName=>$fqfn) {
337 if (substr($fileName, 0, $this->prefixLen) == $this->prefix) {
341 // Remove it from the list
342 unset($this->classes[$fileName]);
348 * Getter for total include counter
350 * @return $total Total loaded include files
352 public final function getTotal () {
357 * Getter for a printable list of included classes/interfaces/exceptions
359 * @param $includeList A printable include list
361 public function getPrintableIncludeList () {
364 foreach ($this->loadedClasses as $classFile) {
365 $includeList .= basename($classFile)."<br />\n";