1
0
Fork 0

Add autoload support for .hh files (HHVM)

HHVM is adding support for an alternative extension for files using
HHVM-specific features. Support them in the Class Map and PSR4
autoloaders.

Trivial example: https://github.com/fredemmott/hh_extension_toolset
pull/2823/head
Fred Emmott 2014-03-18 14:37:37 -07:00
parent 70a20ebcc1
commit 734e5e2f2b
2 changed files with 19 additions and 6 deletions

View File

@ -291,8 +291,22 @@ class ClassLoader
return $this->classMap[$class]; return $this->classMap[$class];
} }
$file = $this->findFileWithExtension($class, '.php');
if ($file === null) {
// Indicates a Hack file (hacklang.org)
$file = $this->findFileWithExtension($class, '.hh');
}
if ($file === null) {
// Remember that this class does not exist.
return $this->classMap[$class] = false;
}
return $file;
}
private function findFileWithExtension($class, $ext) {
// PSR-4 lookup // PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php'; $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0]; $first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) { if (isset($this->prefixLengthsPsr4[$first])) {
@ -321,7 +335,7 @@ class ClassLoader
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else { } else {
// PEAR-like class name // PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . '.php'; $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
} }
if (isset($this->prefixesPsr0[$first])) { if (isset($this->prefixesPsr0[$first])) {
@ -348,8 +362,7 @@ class ClassLoader
return $file; return $file;
} }
// Remember that this class does not exist. return null;
return $this->classMap[$class] = false;
} }
} }

View File

@ -54,7 +54,7 @@ class ClassMapGenerator
if (is_file($path)) { if (is_file($path)) {
$path = array(new \SplFileInfo($path)); $path = array(new \SplFileInfo($path));
} elseif (is_dir($path)) { } elseif (is_dir($path)) {
$path = Finder::create()->files()->followLinks()->name('/\.(php|inc)$/')->in($path); $path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
} else { } else {
throw new \RuntimeException( throw new \RuntimeException(
'Could not scan for classes inside "'.$path. 'Could not scan for classes inside "'.$path.
@ -68,7 +68,7 @@ class ClassMapGenerator
foreach ($path as $file) { foreach ($path as $file) {
$filePath = $file->getRealPath(); $filePath = $file->getRealPath();
if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc'))) { if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) {
continue; continue;
} }