浏览代码

Add support for using classmap to autoload Hack enums

fixes composer/composer#3823

Ran tests with both PHP5.5.9-1ubuntu4.5 and HHVM 3.6. Test fails on HHVM only
if I back out the ClassMapGenerator.php change.
Fred Emmott 10 年之前
父节点
当前提交
33ea86573e

+ 12 - 2
src/Composer/Autoload/ClassMapGenerator.php

@@ -113,6 +113,10 @@ class ClassMapGenerator
     private static function findClasses($path)
     private static function findClasses($path)
     {
     {
         $traits = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
         $traits = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
+        $enums = '';
+        if (defined('HPHP_VERSION') && version_compare(HPHP_VERSION, '3.3', '>=')) {
+            $enums = '|enum';
+        }
 
 
         try {
         try {
             $contents = @php_strip_whitespace($path);
             $contents = @php_strip_whitespace($path);
@@ -129,7 +133,7 @@ class ClassMapGenerator
         }
         }
 
 
         // return early if there is no chance of matching anything in this file
         // return early if there is no chance of matching anything in this file
-        if (!preg_match('{\b(?:class|interface'.$traits.')\s}i', $contents)) {
+        if (!preg_match('{\b(?:class|interface'.$traits.$enums.')\s}i', $contents)) {
             return array();
             return array();
         }
         }
 
 
@@ -154,7 +158,7 @@ class ClassMapGenerator
 
 
         preg_match_all('{
         preg_match_all('{
             (?:
             (?:
-                 \b(?<![\$:>])(?P<type>class|interface'.$traits.') \s+ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*)
+                 \b(?<![\$:>])(?P<type>class|interface'.$traits.$enums.') \s+ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*)
                | \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\s*\\\\\s*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*)? \s*[\{;]
                | \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\s*\\\\\s*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*)? \s*[\{;]
             )
             )
         }ix', $contents, $matches);
         }ix', $contents, $matches);
@@ -170,6 +174,12 @@ class ClassMapGenerator
                 if ($name[0] === ':') {
                 if ($name[0] === ':') {
                     // This is an XHP class, https://github.com/facebook/xhp
                     // This is an XHP class, https://github.com/facebook/xhp
                     $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
                     $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
+                } else if ($matches['type'][$i] === 'enum') {
+                    // In Hack, something like:
+                    //   enum Foo: int { HERP = '123'; }
+                    // The regex above captures the colon, which isn't part of
+                    // the class name.
+                    $name = rtrim($name, ':');
                 }
                 }
                 $classes[] = ltrim($namespace . $name, '\\');
                 $classes[] = ltrim($namespace . $name, '\\');
             }
             }

+ 6 - 0
tests/Composer/Test/Autoload/ClassMapGeneratorTest.php

@@ -74,6 +74,12 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
                 'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php',
                 'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php',
             ));
             ));
         }
         }
+        if (defined('HPHP_VERSION') && version_compare(HPHP_VERSION, '3.3', '>=')) {
+            $data[] = array(__DIR__.'/Fixtures/hhvm3.3', array(
+                'FooEnum' => __DIR__.'/Fixtures/hhvm3.3/HackEnum.php',
+                'Foo\BarEnum' => __DIR__.'/Fixtures/hhvm3.3/NamespacedHackEnum.php',
+            ));
+        }
 
 
         return $data;
         return $data;
     }
     }

+ 6 - 0
tests/Composer/Test/Autoload/Fixtures/hhvm3.3/HackEnum.php

@@ -0,0 +1,6 @@
+<?hh
+
+enum FooEnum: int {
+  HERP = 1;
+  DERP = 2;
+}

+ 7 - 0
tests/Composer/Test/Autoload/Fixtures/hhvm3.3/NamespacedHackEnum.php

@@ -0,0 +1,7 @@
+<?hh
+
+namespace Foo;
+
+enum BarEnum: string {
+  HERP = 'DERP';
+}