18 protected static $registered =
false;
21 protected static $paths = array();
24 protected static $map = array();
27 protected static $maps = array();
30 protected static $mapfile = null;
33 protected static $unmapped = array();
47 public static function initialize($path = null, $mapfile = null)
51 self::addMap($mapfile);
60 protected static function register()
62 if (!self::$registered) {
64 $autoload = spl_autoload_functions();
65 spl_autoload_register(__CLASS__.
'::load');
66 if (function_exists(
'__autoload') && ($autoload ===
false)) {
69 spl_autoload_register(
'__autoload');
72 self::$registered =
true;
86 $len = strlen(DIRECTORY_SEPARATOR);
88 if (!is_string($path)) {
92 if (substr($path, -$len) == DIRECTORY_SEPARATOR) {
93 $path = substr($path, 0, -$len);
99 if (!in_array($path, self::$paths)) {
100 self::$paths[] = $path;
113 protected static function addMap($mapfile)
115 if (! in_array($mapfile, self::$maps)) {
118 self::$mapfile = $mapfile;
120 if (file_exists($mapfile)) {
121 $map = include $mapfile;
122 if (is_array($map)) {
124 self::$maps[] = $mapfile;
125 self::$map = array_merge(self::$map, $map);
143 if (isset(self::$map[$class])) {
146 if (isset(self::$mapfile) && ! isset(self::$map[$class])) {
147 self::$unmapped[] = $class;
163 public static function load($class)
170 if (strpos($class,
":") !==
false) {
173 throw new \Exception(
'Possible remote code injection detected');
178 $mapped = self::isMapped($class);
180 require_once self::$map[$class];
181 if (!self::loadSuccessful($class)) {
183 self::$unmapped[] = $class;
194 foreach (self::$paths as $path) {
195 if (file_exists($path . DIRECTORY_SEPARATOR . $file)) {
196 require_once $path . DIRECTORY_SEPARATOR . $file;
197 if (!self::loadSuccessful($class)) {
198 throw new \Exception(
199 'Class ' . $class .
' was not present in ' .
200 $path . DIRECTORY_SEPARATOR . $file .
205 if (in_array($class, self::$unmapped)) {
208 $path . DIRECTORY_SEPARATOR . $file
216 'Class ' . $class .
' could not be loaded from ' .
217 $file .
', file does not exist (registered paths="' .
218 implode(PATH_SEPARATOR, self::$paths) .
221 $trace = $e->getTrace();
222 if (isset($trace[2]) && isset($trace[2][
'function']) &&
224 $trace[2][
'function'],
225 array(
'class_exists',
'interface_exists')
229 if (isset($trace[1]) && isset($trace[1][
'function']) &&
231 $trace[1][
'function'],
232 array(
'class_exists',
'interface_exists')
240 if (count(spl_autoload_functions()) == 1) {
258 if (!class_exists($class,
false) &&
259 !interface_exists($class,
false)) {
280 if (is_writable(self::$mapfile) ||
281 is_writable(dirname(self::$mapfile))) {
282 self::$map[$class] = $origin;
286 .
"// Autoload auto-generated classmap\n" 287 .
"return " . var_export(self::$map,
true) .
';',
Autoloader for Erebot's classes and interfaces.
static initialize($path=null, $mapfile=null)
static loadSuccessful($class)
static updateMap($class, $origin)