Allow the autoloader to try mutliple possible paths for each path

This commit is contained in:
Robin Appelman 2013-05-07 22:21:59 +02:00
parent 19cfe74bf5
commit d1fcb7eb52
1 changed files with 17 additions and 24 deletions

View File

@ -30,10 +30,11 @@ class Autoloader {
public function load($class) { public function load($class) {
$class = trim($class, '\\'); $class = trim($class, '\\');
$paths = array();
if (array_key_exists($class, $this->classPaths)) { if (array_key_exists($class, $this->classPaths)) {
$path = $this->classPaths[$class]; $paths[] = $this->classPaths[$class];
} else if (array_key_exists($class, \OC::$CLASSPATH)) { } else if (array_key_exists($class, \OC::$CLASSPATH)) {
$path = \OC::$CLASSPATH[$class]; $paths[] = \OC::$CLASSPATH[$class];
/** /**
* @TODO: Remove this when necessary * @TODO: Remove this when necessary
* Remove "apps/" from inclusion path for smooth migration to mutli app dir * Remove "apps/" from inclusion path for smooth migration to mutli app dir
@ -43,44 +44,36 @@ class Autoloader {
$path = str_replace('apps/', '', $path); $path = str_replace('apps/', '', $path);
} }
} elseif (strpos($class, 'OC_') === 0) { } elseif (strpos($class, 'OC_') === 0) {
$path = strtolower(str_replace('_', '/', substr($class, 3)) . '.php'); $paths[] = strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
} elseif (strpos($class, 'OC\\') === 0) { } elseif (strpos($class, 'OC\\') === 0) {
$path = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php'); $paths[] = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
} elseif (strpos($class, 'OCP\\') === 0) { } elseif (strpos($class, 'OCP\\') === 0) {
$path = 'public/' . strtolower(str_replace('\\', '/', substr($class, 3)) . '.php'); $paths[] = 'public/' . strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
} elseif (strpos($class, 'OCA\\') === 0) { } elseif (strpos($class, 'OCA\\') === 0) {
foreach (\OC::$APPSROOTS as $appDir) { foreach (\OC::$APPSROOTS as $appDir) {
$path = strtolower(str_replace('\\', '/', substr($class, 4)) . '.php'); $paths[] = $appDir['path'] . '/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
$fullPath = stream_resolve_include_path($appDir['path'] . '/' . $path);
if (file_exists($fullPath)) {
require_once $fullPath;
return false;
}
// If not found in the root of the app directory, insert '/lib' after app id and try again. // If not found in the root of the app directory, insert '/lib' after app id and try again.
$libpath = substr($path, 0, strpos($path, '/')) . '/lib' . substr($path, strpos($path, '/')); $paths[] = $appDir['path'] . '/lib/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
$fullPath = stream_resolve_include_path($appDir['path'] . '/' . $libpath);
if (file_exists($fullPath)) {
require_once $fullPath;
return false;
}
} }
} elseif (strpos($class, 'Sabre_') === 0) { } elseif (strpos($class, 'Sabre_') === 0) {
$path = str_replace('_', '/', $class) . '.php'; $paths[] = str_replace('_', '/', $class) . '.php';
} elseif (strpos($class, 'Symfony\\Component\\Routing\\') === 0) { } elseif (strpos($class, 'Symfony\\Component\\Routing\\') === 0) {
$path = 'symfony/routing/' . str_replace('\\', '/', $class) . '.php'; $paths[] = 'symfony/routing/' . str_replace('\\', '/', $class) . '.php';
} elseif (strpos($class, 'Sabre\\VObject') === 0) { } elseif (strpos($class, 'Sabre\\VObject') === 0) {
$path = str_replace('\\', '/', $class) . '.php'; $paths[] = str_replace('\\', '/', $class) . '.php';
} elseif (strpos($class, 'Test_') === 0) { } elseif (strpos($class, 'Test_') === 0) {
$path = 'tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php'); $paths[] = 'tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php');
} elseif (strpos($class, 'Test\\') === 0) { } elseif (strpos($class, 'Test\\') === 0) {
$path = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php'); $paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
} else { } else {
return false; return false;
} }
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;
} }
} }