Make performance of the class loader more constant across classes

main
Jordi Boggiano 11 years ago
parent 5ba147663d
commit 94175ce432

@ -49,7 +49,7 @@ class ClassLoader
public function getPrefixes() public function getPrefixes()
{ {
return $this->prefixes; return call_user_func_array('array_merge', $this->prefixes);
} }
public function getFallbackDirs() public function getFallbackDirs()
@ -98,19 +98,20 @@ class ClassLoader
return; return;
} }
if (!isset($this->prefixes[$prefix])) { $first = substr($prefix, 0, 1);
$this->prefixes[$prefix] = (array) $paths; if (!isset($this->prefixes[$first][$prefix])) {
$this->prefixes[$first][$prefix] = (array) $paths;
return; return;
} }
if ($prepend) { if ($prepend) {
$this->prefixes[$prefix] = array_merge( $this->prefixes[$first][$prefix] = array_merge(
(array) $paths, (array) $paths,
$this->prefixes[$prefix] $this->prefixes[$first][$prefix]
); );
} else { } else {
$this->prefixes[$prefix] = array_merge( $this->prefixes[$first][$prefix] = array_merge(
$this->prefixes[$prefix], $this->prefixes[$first][$prefix],
(array) $paths (array) $paths
); );
} }
@ -129,7 +130,7 @@ class ClassLoader
return; return;
} }
$this->prefixes[$prefix] = (array) $paths; $this->prefixes[substr($prefix, 0, 1)][$prefix] = (array) $paths;
} }
/** /**
@ -205,7 +206,7 @@ class ClassLoader
if (false !== $pos = strrpos($class, '\\')) { if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name // namespaced class name
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; $classPath = strtr(substr($class, 0, $pos), '\\', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$className = substr($class, $pos + 1); $className = substr($class, $pos + 1);
} else { } else {
// PEAR-like class name // PEAR-like class name
@ -213,13 +214,16 @@ class ClassLoader
$className = $class; $className = $class;
} }
$classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; $classPath .= strtr($className, '_', DIRECTORY_SEPARATOR) . '.php';
foreach ($this->prefixes as $prefix => $dirs) { $first = substr($class, 0, 1);
if (0 === strpos($class, $prefix)) { if (isset($this->prefixes[$first])) {
foreach ($dirs as $dir) { foreach ($this->prefixes[$first] as $prefix => $dirs) {
if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { if (0 === strpos($class, $prefix)) {
return $dir . DIRECTORY_SEPARATOR . $classPath; foreach ($dirs as $dir) {
if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
} }
} }
} }

Loading…
Cancel
Save