Autoloader: split getting the class paths and loading the class

This commit is contained in:
Robin Appelman 2013-05-07 22:26:55 +02:00
parent cac86bb4db
commit 72ed74f28a
1 changed files with 20 additions and 7 deletions

View File

@ -38,12 +38,12 @@ class Autoloader {
} }
/** /**
* Load the specified class * get the possible paths for a class
* *
* @param string $class * @param string $class
* @return bool * @return array|bool an array of possible paths or false if the class is not part of ownCloud
*/ */
public function load($class) { public function findClass($class) {
$class = trim($class, '\\'); $class = trim($class, '\\');
$paths = array(); $paths = array();
@ -57,7 +57,7 @@ class Autoloader {
*/ */
if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) { if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) {
\OC_Log::write('core', 'include path for class "' . $class . '" starts with "apps/"', \OC_Log::DEBUG); \OC_Log::write('core', 'include path for class "' . $class . '" starts with "apps/"', \OC_Log::DEBUG);
$path = str_replace('apps/', '', \OC::$CLASSPATH[$class]); $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
} }
} elseif (strpos($class, 'OC_') === 0) { } elseif (strpos($class, 'OC_') === 0) {
$paths[] = strtolower(str_replace('_', '/', substr($class, 3)) . '.php'); $paths[] = strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
@ -84,12 +84,25 @@ class Autoloader {
} else { } else {
return false; return false;
} }
return $paths;
}
/**
* Load the specified class
*
* @param string $class
* @return bool
*/
public function load($class) {
$paths = $this->findClass($class);
if (is_array($paths)) {
foreach ($paths as $path) { foreach ($paths as $path) {
if ($fullPath = stream_resolve_include_path($path)) { if ($fullPath = stream_resolve_include_path($path)) {
require_once $fullPath; require_once $fullPath;
} }
} }
}
return false; return false;
} }
} }