Browse Source

Simplify some ClassLoader code, minor memory improvement, fixes #6937

Jordi Boggiano 7 years ago
parent
commit
3be9591930
2 changed files with 16 additions and 10 deletions
  1. 8 0
      src/Composer/Autoload/AutoloadGenerator.php
  2. 8 10
      src/Composer/Autoload/ClassLoader.php

+ 8 - 0
src/Composer/Autoload/AutoloadGenerator.php

@@ -783,6 +783,14 @@ HEADER;
             }
         }
 
+        // BC handling when converting to a new ClassLoader
+        if (isset($maps['prefixLengthsPsr4'])) {
+            $maps['firstCharsPsr4'] = array_map(function () {
+                return true;
+            }, $maps['prefixLengthsPsr4']);
+            unset($maps['prefixLengthsPsr4']);
+        }
+
         foreach ($maps as $prop => $value) {
             if (count($value) > 32767) {
                 // Static arrays are limited to 32767 values on PHP 5.6

+ 8 - 10
src/Composer/Autoload/ClassLoader.php

@@ -43,7 +43,7 @@ namespace Composer\Autoload;
 class ClassLoader
 {
     // PSR-4
-    private $prefixLengthsPsr4 = array();
+    private $firstCharsPsr4 = array();
     private $prefixDirsPsr4 = array();
     private $fallbackDirsPsr4 = array();
 
@@ -170,11 +170,10 @@ class ClassLoader
             }
         } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
             // Register directories for a new namespace.
-            $length = strlen($prefix);
-            if ('\\' !== $prefix[$length - 1]) {
+            if ('\\' !== substr($prefix, -1)) {
                 throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
             }
-            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
+            $this->firstCharsPsr4[$prefix[0]] = true;
             $this->prefixDirsPsr4[$prefix] = (array) $paths;
         } elseif ($prepend) {
             // Prepend directories for an already registered namespace.
@@ -221,11 +220,10 @@ class ClassLoader
         if (!$prefix) {
             $this->fallbackDirsPsr4 = (array) $paths;
         } else {
-            $length = strlen($prefix);
-            if ('\\' !== $prefix[$length - 1]) {
+            if ('\\' !== substr($prefix, -1)) {
                 throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
             }
-            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
+            $this->firstCharsPsr4[$prefix[0]] = true;
             $this->prefixDirsPsr4[$prefix] = (array) $paths;
         }
     }
@@ -373,15 +371,15 @@ class ClassLoader
         $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
 
         $first = $class[0];
-        if (isset($this->prefixLengthsPsr4[$first])) {
+        if (isset($this->firstCharsPsr4[$first])) {
             $subPath = $class;
             while (false !== $lastPos = strrpos($subPath, '\\')) {
                 $subPath = substr($subPath, 0, $lastPos);
                 $search = $subPath.'\\';
                 if (isset($this->prefixDirsPsr4[$search])) {
-                    $length = $this->prefixLengthsPsr4[$first][$search];
+                    $pathEnd = substr($logicalPathPsr4, $lastPos + 1);
                     foreach ($this->prefixDirsPsr4[$search] as $dir) {
-                        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
+                        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $pathEnd)) {
                             return $file;
                         }
                     }