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 * - loadClasses rewritten to fix some notices
29 * ----------------------------------
35 private $cfg = array();
38 * An ArrayObject for found classes
40 private $classes = null;
43 * Suffix with extension for all class files
45 private $prefix = "class_";
48 * Suffix with extension for all class files
50 private $suffix = ".php";
53 * Length of the suffix. Will be overwritten later.
58 * Length of the prefix. Will be overwritten later.
63 * A list for directory names (no leading/trailing slashes!) which not be scanned by the path scanner
66 private $ignoreList = array();
69 * An ArrayList object for include directories
71 private $dirList = null;
74 * Debug this class loader? (true = yes, false = no)
76 private $debug = false;
79 * Counter for scanned directories (debug output)
84 * Counter for loaded classes (debug output)
86 private $classCnt = 0;
89 * Instance of this class
91 private static $thisInstance = null;
94 * The *public* constructor
96 * @param $cfgInstance Configuration class instance
99 public function __construct (FrameworkConfiguration $cfgInstance) {
100 // Init the array list
101 $this->dirList = new ArrayObject();
103 // Set suffix and prefix from configuration
104 $this->suffix = $cfgInstance->readConfig('class_suffix');
105 $this->prefix = $cfgInstance->readConfig('class_prefix');
107 // Estimate length of prefix and suffix for substr() function (cache)
108 $this->sufLen = strlen($this->suffix);
109 $this->preLen = strlen($this->prefix);
111 // Set configuration instance
112 $this->cfgInstance = $cfgInstance;
114 // Initialize the classes list
115 $this->classes = new ArrayObject();
118 self::$thisInstance = $this;
122 * Getter for an instance of this class
124 * @return $thisInstance An instance of this class
126 public final static function getInstance () {
127 return self::$thisInstance;
131 * Scans recursively a local path for class files which must have a prefix and a suffix as given by $this->suffix and $this->prefix
133 * @param $basePath The relative base path to PATH constant for all classes
134 * @param $ignoreList An optional list (array or string) of directory names which shall be ignored
137 public function loadClasses ($basePath, $ignoreList = array() ) {
138 // Convert string to array
139 if (!is_array($ignoreList)) $ignoreList = array($ignoreList);
141 // Directories which our class loader ignores by default while
142 // deep-scanning the directory structure. See scanLocalPath() for
145 $ignoreList[] = "..";
146 $ignoreList[] = ".htaccess";
147 $ignoreList[] = ".svn";
149 // Keep it in class for later usage
150 $this->ignoreList = $ignoreList;
152 // Set base directory which holds all our classes, we should use an
153 // absolute path here so is_dir(), is_file() and so on will always
154 // find the correct files and dirs.
155 $basePath2 = realpath($basePath);
157 // If the basePath is false it is invalid
158 if ($basePath2 === false) {
159 // TODO: Do not die here.
160 die("Cannot read {$basePath} !");
163 $basePath = $basePath2;
166 // Load all super classes (backward, why ever this name... :-? )
167 // We don't support sub directories here...
168 $this->scanLocalPath($basePath);
170 // While there are directories in our list scan them for classes
172 while ($cnt != $this->dirList->count()) {
173 for ($idx = $this->dirList->getIterator(); $idx->valid(); $idx->next()) {
175 $currPath = $idx->current();
177 // Remove the current entry or else this will lead into a infinite loop
178 $this->dirList->offsetSet($idx->key(), "");
180 // Scan the directory
181 $this->scanLocalPath($currPath);
184 // Check if we can leave
186 for ($idx = $this->dirList->getIterator(); $idx->valid(); $idx->next()) {
187 if ($idx->current() == "") $cnt++;
193 * The local path scanner. A found class will be loaded immediately
194 * @param $localPath The local path which shall be recursively scanned for include files
197 private function scanLocalPath ($localPath) {
198 // Empty path names will be silently ignored
199 if (empty($localPath)) return;
201 // TODO: No dies here, mayybe this should be rewritten to throw an exception?
202 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($localPath);
203 while ($dirClass = $dirInstance->readDirectoryExcept($this->ignoreList)) {
204 // We need the relative dir name as an array index some lines below
205 $dirClass2 = $dirClass;
207 // A nice replacement for a simple dot ;)
208 $dirClass = sprintf("%s/%s", $localPath, $dirClass);
210 // Is a readable file with configured prefix and suffix? All other
211 // files will silently be ignored!
212 //* DEBUG: */ print "Prefix=".$this->prefix."(".substr($dirClass2, 0 , $this->preLen).")\n";
213 //* DEBUG: */ print "Suffix=".$this->suffix."(".substr($dirClass2, -$this->sufLen, $this->sufLen).")\n";
214 //* DEBUG: */ print "ENTRY={$dirClass}\n";
217 && (is_readable($dirClass))
218 && (substr($dirClass2, 0 , $this->preLen) == $this->prefix)
219 && (substr($dirClass2, -$this->sufLen, $this->sufLen) == $this->suffix)
221 // Class found so load it instantly
222 //* DEBUG: */ print "CLASS={$dirClass}\n";
223 $this->classes->append($dirClass);
225 } elseif (is_dir($dirClass) && !in_array($dirClass2, $this->ignoreList)) {
226 // Directory found and added to list
227 //* DEBUG: */ print "DIR={$dirClass}\n";
228 if ($dirClass2 == "interfaces") {
229 $this->scanLocalPath($dirClass);
231 $this->dirList->append($dirClass);
235 //* DEBUG: */ print "LOOP!\n";
238 // Close directory handler
239 $dirInstance->closeDirectory();
241 // Output counter in debug mode
242 if (defined('DEBUG_MODE')) print(sprintf("[%s:] <strong>%d</strong> Klassendateien in <strong>%d</strong> Verzeichnissen gefunden und geladen.<br />\n",
250 * Includes all found classes
253 public function includeAllClasses () {
254 if (is_object($this->classes)) {
256 for ($idx = $this->classes->getIterator(); $idx->valid(); $idx->next()) {
257 // Load current class
258 //* DEBUG: */ print "Class=".$idx->current()."\n";
259 require_once($idx->current());
262 // Re-initialize the classes list
263 $this->classes = new ArrayObject();
268 * Load extra config files
272 public function loadExtraConfigs () {
274 $oldPrefix = $this->prefix;
276 // Set new prefix (temporary!)
277 $this->prefix = "config-";
278 $this->preLen = strlen($this->prefix);
280 // Set base directory
281 $basePath = sprintf("%sinc/config/", PATH);
283 // Load all classes from the config directory
284 $this->loadClasses($basePath);
286 // Set the prefix back
287 $this->prefix = $oldPrefix;
288 $this->preLen = strlen($this->prefix);
292 // Initial load of core classes and the FrameworkDirectoryPointer class
293 require_once(sprintf("%sinc/classes/interfaces/class_FrameworkInterface%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
294 require_once(sprintf("%sinc/classes/main/class_BaseFrameworkSystem%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
295 require_once(sprintf("%sinc/classes/main/io/class_FrameworkDirectoryPointer%s", PATH, FrameworkConfiguration::getInstance()->readConfig('php_extension')));
297 // Initialize the class loader
298 $loader = new ClassLoader(FrameworkConfiguration::getInstance());