-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoLoader.class.php
49 lines (42 loc) · 1.85 KB
/
AutoLoader.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
//Original script (now modified) from: http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/
//TODO: make this non-static if possible.
class AutoLoader {
static private $classNames = array();
/**
* Store the filename (sans extension) & full path of all ".php" files found
*/
public static function registerDirectory($dirName) {
$di = new DirectoryIterator($dirName);
foreach ($di as $file) {
if ($file->isDir() && !$file->isLink() && !$file->isDot()) {
// recurse into directories other than a few special ones
self::registerDirectory($file->getPathname());
}
elseif (preg_match('/[Aa]bstract\.class/', $file->getFilename()) && substr($file->getFilename(), -19) === '.abstract.class.php') {
//cs_version.abstract.class.php becomes "cs_versionAbstract"
$className = preg_replace('/.abstract.class.php/', '', $file->getFilename()) . "Abstract";
AutoLoader::registerClass($className, $file->getPathname());
}
elseif (substr($file->getFilename(), -10) === '.class.php') {
$className = preg_replace('/.class.php/', '', $file->getFilename());#substr($file->getFilename(), 0, -10);
AutoLoader::registerClass($className, $file->getPathname());
}
elseif (substr($file->getFilename(), -4) === '.php') {
// save the class name / path of a .php file found
$className = substr($file->getFilename(), 0, -4);
AutoLoader::registerClass($className, $file->getPathname());
}
}
}
public static function registerClass($className, $fileName) {
#echo __METHOD__ .": registering class(". $className .") as (". $fileName .") \n";
AutoLoader::$classNames[$className] = $fileName;
}
public static function loadClass($className) {
if (isset(AutoLoader::$classNames[$className])) {
require_once(AutoLoader::$classNames[$className]);
}
}
}
spl_autoload_register(array('AutoLoader', 'loadClass'));