Autoloader: load the 3rdparty libraries using prefixes

This commit is contained in:
Robin Appelman 2013-05-07 23:07:47 +02:00
parent 0d25c0001c
commit 2a01d39940
1 changed files with 16 additions and 17 deletions

View File

@ -11,18 +11,18 @@ namespace OC;
class Autoloader { class Autoloader {
private $useGlobalClassPath = true; private $useGlobalClassPath = true;
private $namespacePaths = array(); private $prefixPaths = array();
private $classPaths = array(); private $classPaths = array();
/** /**
* Add a custom namespace to the autoloader * Add a custom prefix to the autoloader
* *
* @param string $namespace * @param string $prefix
* @param string $path * @param string $path
*/ */
public function registerNamespace($namespace, $path) { public function registerPrefix($prefix, $path) {
$this->namespacePaths[$namespace] = $path; $this->prefixPaths[$prefix] = $path;
} }
/** /**
@ -81,24 +81,23 @@ class Autoloader {
$paths[] = '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) {
$paths[] = $appDir['path'] . '/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php'); list(, $app,) = explode('\\', $class);
// If not found in the root of the app directory, insert '/lib' after app id and try again. if (stream_resolve_include_path($appDir['path'] . '/' . strtolower($app))) {
$paths[] = $appDir['path'] . '/lib/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php'); $paths[] = $appDir['path'] . '/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
// If not found in the root of the app directory, insert '/lib' after app id and try again.
$paths[] = $appDir['path'] . '/lib/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php');
}
} }
} elseif (strpos($class, 'Sabre_') === 0) {
$paths[] = str_replace('_', '/', $class) . '.php';
} elseif (strpos($class, 'Symfony\\Component\\Routing\\') === 0) {
$paths[] = 'symfony/routing/' . str_replace('\\', '/', $class) . '.php';
} elseif (strpos($class, 'Sabre\\VObject') === 0) {
$paths[] = str_replace('\\', '/', $class) . '.php';
} elseif (strpos($class, 'Test_') === 0) { } elseif (strpos($class, 'Test_') === 0) {
$paths[] = '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) {
$paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php'); $paths[] = 'tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php');
} else { } else {
foreach ($this->namespacePaths as $namespace => $dir) { foreach ($this->prefixPaths as $prefix => $dir) {
if (0 === strpos($class, $namespace)) { if (0 === strpos($class, $prefix)) {
$paths[] = $dir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; $path = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
$path = str_replace('_', DIRECTORY_SEPARATOR, $path);
$paths[] = $dir . '/' . $path;
} }
} }
} }