1
0
Fork 0

Allow "" as autoload prefix for fallback dir

pull/163/head
Jordi Boggiano 2011-12-18 21:09:40 +01:00
parent 10c94c5ba4
commit d22b80d13b
1 changed files with 17 additions and 4 deletions

View File

@ -40,6 +40,7 @@ namespace Composer\Autoload;
class ClassLoader class ClassLoader
{ {
private $prefixes = array(); private $prefixes = array();
private $fallbackDir;
public function getPrefixes() public function getPrefixes()
{ {
@ -54,6 +55,10 @@ class ClassLoader
*/ */
public function add($prefix, $paths) public function add($prefix, $paths)
{ {
if (!$prefix) {
$this->fallbackDir = is_array($paths) ? $paths[0] : $paths;
return;
}
if (isset($this->prefixes[$prefix])) { if (isset($this->prefixes[$prefix])) {
$this->prefixes[$prefix] = array_merge( $this->prefixes[$prefix] = array_merge(
$this->prefixes[$prefix], $this->prefixes[$prefix],
@ -101,24 +106,32 @@ class ClassLoader
if (false !== $pos = strrpos($class, '\\')) { if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name // namespaced class name
$namespace = substr($class, 0, $pos); $classPath = DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos));
$className = substr($class, $pos + 1); $className = substr($class, $pos + 1);
} else { } else {
// PEAR-like class name // PEAR-like class name
$namespace = null; $classPath = null;
$className = $class; $className = $class;
} }
$classPath .= DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
foreach ($this->prefixes as $prefix => $dirs) { foreach ($this->prefixes as $prefix => $dirs) {
foreach ($dirs as $dir) { foreach ($dirs as $dir) {
if (0 === strpos($class, $prefix)) { if (0 === strpos($class, $prefix)) {
$file = $dir . DIRECTORY_SEPARATOR . ($namespace ? str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR : '') $file = $dir . $classPath;
. str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
if (file_exists($file)) { if (file_exists($file)) {
return $file; return $file;
} }
} }
} }
} }
if ($this->fallbackDir) {
$file = $this->fallbackDir . $classPath;
if (file_exists($file)) {
return $file;
}
}
} }
} }