/
Autoloader.php
115 lines (103 loc) · 3.11 KB
/
Autoloader.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace SparkLib;
define('SLASH', DIRECTORY_SEPARATOR);
/**
* A simple but configurable autoloader. Expects everything to
* live under the directory specified by LIBDIR, with most classes
* under LIBDIR/classes/.
*
* <code>
* define('LIBDIR', '/path/to/your/code/lib/');
* require LIBDIR . 'classes/SparkLib/Autoloader.php';
* </code>
*/
class Autoloader {
/**
* Set this to declare explicit include paths, relative to
* AUTOLOADER_INCLUDE_ROOT, for things that don't conform to
* the classes/ClassName.php pattern. For example:
*
* <code>
* \SparkLib\Autoloader::$classPath = array(
* 'CupsPrintIPP' => 'phpprintipp/php_classes/CupsPrintIPP.php',
* 'SphinxClient' => 'classes/sphinxapi.php',
* );
* </code>
*/
public static $classPath = array();
/**
* Set this to declare special directories for classes containing
* a given substring.
*
* <code>
* \SparkLib\Autoloader::$searchPaths = array(
* 'Saurus' => 'classes/dinosaurs/',
* );
* </code>
*/
public static $searchPaths = array();
/**
* Set this to define any extra files which should be included
* if we fail to find a class.
*/
public static $extraAutoloaders = array();
/**
* Install this puppy as an autoloader, do some basic configuration.
*/
public static function setup ()
{
// TODO
// Get this in full conformance with:
// http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1
// ...and see about just setting include_path() instead of knowing an absolute
// path to things.
// If LIBDIR isn't defined, assume that this file lives in a path like
// /somepath/lib/classes/SparkLib/, where we want somepath/lib/ to be
// our include root...
if (constant('LIBDIR')) {
define('AUTOLOADER_INCLUDE_ROOT', LIBDIR);
} else {
define(
'AUTOLOADER_INCLUDE_ROOT',
realpath(dirname(__FILE__) . SLASH . '..' . SLASH . '..' . SLASH)
);
}
// Register autoloaders
spl_autoload_register(array('\SparkLib\Autoloader', 'load'));
}
/**
* Do the actual business of autoloading.
*
* @param string name of class
*/
public static function load ($class)
{
if (isset(self::$classPath[$class])) {
$path = self::$classPath[$class];
}
else {
// TODO: All these strpos() calls are probably expensive.
foreach (static::$searchPaths as $substr => $dir) {
if (false !== strpos($class, $substr)) {
$path = $dir . $class . '.php';
break;
}
}
}
// If we got here, try for a corresponding file in classes
if (! isset($path)) {
$class = str_replace('\\', SLASH, $class);
$path = 'classes' . SLASH . $class . '.php';
}
$full_path = AUTOLOADER_INCLUDE_ROOT . SLASH . $path;
if (is_file($full_path)) {
include $full_path;
} else {
// If we've got any fallback autoloaders, pull 'em in so
// they can try to find the class in question
foreach (static::$extraAutoloaders as $autoloader_path) {
include_once($autoloader_path);
}
}
}
}