Autoloader: add support for custom namespace paths

This commit is contained in:
Robin Appelman 2013-05-07 22:39:35 +02:00
parent 72ed74f28a
commit 6d903cf7ae
1 changed files with 19 additions and 1 deletions

View File

@ -11,8 +11,20 @@ namespace OC;
class Autoloader {
private $useGlobalClassPath = true;
private $namespacePaths = array();
private $classPaths = array();
/**
* Add a custom namespace to the autoloader
*
* @param string $namespace
* @param string $path
*/
public function registerNamespace($namespace, $path) {
$this->namespacePaths[$namespace] = $path;
}
/**
* Add a custom classpath to the autoloader
*
@ -60,6 +72,8 @@ class Autoloader {
$paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]);
}
} elseif (strpos($class, 'OC_') === 0) {
// first check for legacy classes if underscores are used
$paths[] = 'legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
$paths[] = strtolower(str_replace('_', '/', substr($class, 3)) . '.php');
} elseif (strpos($class, 'OC\\') === 0) {
$paths[] = strtolower(str_replace('\\', '/', substr($class, 3)) . '.php');
@ -82,7 +96,11 @@ class Autoloader {
} elseif (strpos($class, 'Test\\') === 0) {
$paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
} else {
return false;
foreach ($this->namespacePaths as $namespace => $dir) {
if (0 === strpos($class, $namespace)) {
$paths[] = $dir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
}
}
}
return $paths;
}