3 * This class loads class include files with a specific prefix and suffix
5 * @author Roland Haeder <roland __NOSPAM__ [at] __REMOVE_ME__ mxchange [dot] org>
8 * ----------------------------------
10 * - loadClasses rewritten to fix some notices
13 * ----------------------------------
19 private $cfg = array();
22 * An ArrayObject for found classes
24 private $classes = null;
27 * Suffix with extension for all class files
29 private $prefix = "class_";
32 * Suffix with extension for all class files
34 private $suffix = ".php";
37 * Length of the suffix. Will be overwritten later.
42 * Length of the prefix. Will be overwritten later.
47 * A list for directory names (no leading/trailing slashes!) which not be scanned by the path scanner
50 private $ignoreList = array();
53 * An ArrayList object for include directories
55 private $dirList = null;
58 * Debug this class loader? (true = yes, false = no)
60 private $debug = false;
63 * Counter for scanned directories (debug output)
68 * Counter for loaded classes (debug output)
70 private $classCnt = 0;
73 * Instance of this class
75 private static $thisInstance = null;
78 * The *public* constructor
80 * @param $cfgInstance Configuration class instance
83 public function __construct (FrameworkConfiguration $cfgInstance) {
84 // Init the array list
85 $this->dirList = new ArrayObject();
87 // Set suffix and prefix from configuration
88 $this->suffix = $cfgInstance->readConfig("class_suffix");
89 $this->prefix = $cfgInstance->readConfig("class_prefix");
91 // Estimate length of prefix and suffix for substr() function (cache)
92 $this->sufLen = strlen($this->suffix);
93 $this->preLen = strlen($this->prefix);
95 // Set configuration instance
96 $this->cfgInstance = $cfgInstance;
98 // Initialize the classes list
99 $this->classes = new ArrayObject();
102 self::$thisInstance = $this;
106 * Getter for an instance of this class
108 * @return $thisInstance An instance of this class
110 public final static function getInstance () {
111 return self::$thisInstance;
115 * Scans recursively a local path for class files which must have a prefix and a suffix as given by $this->suffix and $this->prefix
117 * @param $basePath The relative base path to PATH constant for all classes
118 * @param $ignoreList An optional list (array or string) of directory names which shall be ignored
121 public function loadClasses ($basePath, $ignoreList = array() ) {
122 // Convert string to array
123 if (!is_array($ignoreList)) $ignoreList = array($ignoreList);
125 // Directories which our class loader ignores by default while
126 // deep-scanning the directory structure. See scanLocalPath() for
129 $ignoreList[] = "..";
130 $ignoreList[] = ".htaccess";
132 // Keep it in class for later usage
133 $this->ignoreList = $ignoreList;
135 // Set base directory which holds all our classes, we should use an
136 // absolute path here so is_dir(), is_file() and so on will always
137 // find the correct files and dirs.
138 $basePath2 = realpath($basePath);
140 // If the basePath is false it is invalid
141 if ($basePath2 === false) {
142 // TODO: Do not die here.
143 die("Cannot read {$basePath} !");
146 $basePath = $basePath2;
149 // Load all super classes (backward, why ever this name... :-? )
150 // We don't support sub directories here...
151 $this->scanLocalPath($basePath);
153 // While there are directories in our list scan them for classes
155 while ($cnt != $this->dirList->count()) {
156 for ($idx = $this->dirList->getIterator(); $idx->valid(); $idx->next()) {
158 $currPath = $idx->current();
160 // Remove the current entry or else this will lead into a infinite loop
161 $this->dirList->offsetSet($idx->key(), "");
163 // Scan the directory
164 $this->scanLocalPath($currPath);
167 // Check if we can leave
169 for ($idx = $this->dirList->getIterator(); $idx->valid(); $idx->next()) {
170 if ($idx->current() == "") $cnt++;
176 * The local path scanner. A found class will be loaded immediately
177 * @param $localPath The local path which shall be recursively scanned for include files
180 private function scanLocalPath ($localPath) {
181 // Empty path names will be silently ignored
182 if (empty($localPath)) return;
184 // TODO: No dies here, mayybe this should be rewritten to throw an exception?
185 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($localPath);
186 while ($dirClass = $dirInstance->readDirectoryExcept($this->ignoreList)) {
187 // We need the relative dir name as an array index some lines below
188 $dirClass2 = $dirClass;
190 // A nice replacement for a simple dot ;)
191 $dirClass = sprintf("%s/%s", $localPath, $dirClass);
193 // Is a readable file with configured prefix and suffix? All other
194 // files will silently be ignored!
195 //* DEBUG: */ print "Prefix=".$this->prefix."(".substr($dirClass2, 0 , $this->preLen).")\n";
196 //* DEBUG: */ print "Suffix=".$this->suffix."(".substr($dirClass2, -$this->sufLen, $this->sufLen).")\n";
197 //* DEBUG: */ print "ENTRY={$dirClass}\n";
200 && (is_readable($dirClass))
201 && (substr($dirClass2, 0 , $this->preLen) == $this->prefix)
202 && (substr($dirClass2, -$this->sufLen, $this->sufLen) == $this->suffix)
204 // Class found so load it instantly
205 //* DEBUG: */ print "CLASS={$dirClass}\n";
206 $this->classes->append($dirClass);
208 } elseif (is_dir($dirClass) && !in_array($dirClass2, $this->ignoreList)) {
209 // Directory found and added to list
210 //* DEBUG: */ print "DIR={$dirClass}\n";
211 if ($dirClass2 == "interfaces") {
212 $this->scanLocalPath($dirClass);
214 $this->dirList->append($dirClass);
218 //* DEBUG: */ print "LOOP!\n";
221 // Close directory handler
222 $dirInstance->closeDirectory();
224 // Output counter in debug mode
225 if (defined('DEBUG_MODE')) print(sprintf("[%s:] <strong>%d</strong> Klassendateien in <strong>%d</strong> Verzeichnissen gefunden und geladen.<br />\n",
233 * Includes all found classes
236 public function includeAllClasses () {
237 if (is_object($this->classes)) {
239 for ($idx = $this->classes->getIterator(); $idx->valid(); $idx->next()) {
240 // Load current class
241 //* DEBUG: */ print "Class=".$idx->current()."\n";
242 require_once($idx->current());
245 // Re-initialize the classes list
246 $this->classes = new ArrayObject();
251 // Initial load of core classes and the FrameworkDirectoryPointer class
252 require_once(sprintf("%sinc/classes/interfaces/class_FrameworkInterface%s", PATH, FrameworkConfiguration::getInstance()->readConfig("php_extension")));
253 require_once(sprintf("%sinc/classes/main/class_BaseFrameworkSystem%s", PATH, FrameworkConfiguration::getInstance()->readConfig("php_extension")));
254 require_once(sprintf("%sinc/classes/main/io/class_FrameworkDirectoryPointer%s", PATH, FrameworkConfiguration::getInstance()->readConfig("php_extension")));
256 // Initialize the class loader
257 $loader = new ClassLoader(FrameworkConfiguration::getInstance());