From 734e5e2f2bc6ab31532e56cec622ce0201a02089 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Tue, 18 Mar 2014 14:37:37 -0700 Subject: [PATCH] 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 --- src/Composer/Autoload/ClassLoader.php | 21 +++++++++++++++++---- src/Composer/Autoload/ClassMapGenerator.php | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index 47ae2ee92..150cfc881 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -291,8 +291,22 @@ class ClassLoader 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 - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php'; + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; $first = $class[0]; if (isset($this->prefixLengthsPsr4[$first])) { @@ -321,7 +335,7 @@ class ClassLoader . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); } else { // PEAR-like class name - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . '.php'; + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; } if (isset($this->prefixesPsr0[$first])) { @@ -348,8 +362,7 @@ class ClassLoader return $file; } - // Remember that this class does not exist. - return $this->classMap[$class] = false; + return null; } } diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index 73c036a37..1f20274e2 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -54,7 +54,7 @@ class ClassMapGenerator if (is_file($path)) { $path = array(new \SplFileInfo($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 { throw new \RuntimeException( 'Could not scan for classes inside "'.$path. @@ -68,7 +68,7 @@ class ClassMapGenerator foreach ($path as $file) { $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; }