|
43 | 43 | class ClassLoader |
44 | 44 | { |
45 | 45 | // PSR-4 |
46 | | - private $firstCharsPsr4 = array(); |
| 46 | + private $prefixLengthsPsr4 = array(); |
47 | 47 | private $prefixDirsPsr4 = array(); |
48 | 48 | private $fallbackDirsPsr4 = array(); |
49 | 49 |
|
@@ -170,10 +170,11 @@ public function addPsr4($prefix, $paths, $prepend = false) |
170 | 170 | } |
171 | 171 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
172 | 172 | // Register directories for a new namespace. |
173 | | - if ('\\' !== substr($prefix, -1)) { |
| 173 | + $length = strlen($prefix); |
| 174 | + if ('\\' !== $prefix[$length - 1]) { |
174 | 175 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
175 | 176 | } |
176 | | - $this->firstCharsPsr4[$prefix[0]] = true; |
| 177 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
177 | 178 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
178 | 179 | } elseif ($prepend) { |
179 | 180 | // Prepend directories for an already registered namespace. |
@@ -220,10 +221,11 @@ public function setPsr4($prefix, $paths) |
220 | 221 | if (!$prefix) { |
221 | 222 | $this->fallbackDirsPsr4 = (array) $paths; |
222 | 223 | } else { |
223 | | - if ('\\' !== substr($prefix, -1)) { |
| 224 | + $length = strlen($prefix); |
| 225 | + if ('\\' !== $prefix[$length - 1]) { |
224 | 226 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
225 | 227 | } |
226 | | - $this->firstCharsPsr4[$prefix[0]] = true; |
| 228 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
227 | 229 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
228 | 230 | } |
229 | 231 | } |
@@ -371,15 +373,15 @@ private function findFileWithExtension($class, $ext) |
371 | 373 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
372 | 374 |
|
373 | 375 | $first = $class[0]; |
374 | | - if (isset($this->firstCharsPsr4[$first])) { |
| 376 | + if (isset($this->prefixLengthsPsr4[$first])) { |
375 | 377 | $subPath = $class; |
376 | 378 | while (false !== $lastPos = strrpos($subPath, '\\')) { |
377 | 379 | $subPath = substr($subPath, 0, $lastPos); |
378 | 380 | $search = $subPath.'\\'; |
379 | 381 | if (isset($this->prefixDirsPsr4[$search])) { |
380 | | - $pathEnd = substr($logicalPathPsr4, $lastPos + 1); |
| 382 | + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
381 | 383 | foreach ($this->prefixDirsPsr4[$search] as $dir) { |
382 | | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $pathEnd)) { |
| 384 | + if (file_exists($file = $dir . $pathEnd)) { |
383 | 385 | return $file; |
384 | 386 | } |
385 | 387 | } |
|
0 commit comments