11
11
use function class_exists ;
12
12
use function explode ;
13
13
use function file_exists ;
14
+ use function getenv ;
14
15
use function interface_exists ;
15
16
use function spl_autoload_register ;
16
17
use function strlen ;
23
24
*/
24
25
class Autoloader
25
26
{
27
+ private const UPSTREAM_COMPOSER_VENDOR_DIRECTORY = __DIR__ . '/../../.. ' ;
28
+ private const LOCAL_COMPOSER_VENDOR_DIRECTORY = __DIR__ . '/../vendor ' ;
29
+
26
30
/**
27
31
* Attach autoloaders for managing legacy ZF artifacts.
28
32
*
@@ -41,10 +45,15 @@ class Autoloader
41
45
public static function load ()
42
46
{
43
47
$ loaded = new ArrayObject ([]);
48
+ $ classLoader = self ::getClassLoader ();
49
+
50
+ if ($ classLoader === null ) {
51
+ return ;
52
+ }
44
53
45
54
spl_autoload_register (self ::createPrependAutoloader (
46
55
RewriteRules::namespaceReverse (),
47
- self :: getClassLoader () ,
56
+ $ classLoader ,
48
57
$ loaded
49
58
), true , true );
50
59
@@ -54,25 +63,15 @@ public static function load()
54
63
));
55
64
}
56
65
57
- /**
58
- * @return ClassLoader
59
- * @throws RuntimeException
60
- */
61
- private static function getClassLoader ()
66
+ private static function getClassLoader (): ?ClassLoader
62
67
{
63
- if (getenv ('COMPOSER_VENDOR_DIR ' ) && file_exists (getenv ('COMPOSER_VENDOR_DIR ' ) . '/autoload.php ' )) {
64
- return include getenv ('COMPOSER_VENDOR_DIR ' ) . '/autoload.php ' ;
65
- }
66
-
67
- if (file_exists (__DIR__ . '/../../../autoload.php ' )) {
68
- return include __DIR__ . '/../../../autoload.php ' ;
69
- }
70
-
71
- if (file_exists (__DIR__ . '/../vendor/autoload.php ' )) {
72
- return include __DIR__ . '/../vendor/autoload.php ' ;
68
+ $ composerVendorDirectory = getenv ('COMPOSER_VENDOR_DIR ' );
69
+ if (is_string ($ composerVendorDirectory )) {
70
+ return self ::getClassLoaderFromVendorDirectory ($ composerVendorDirectory );
73
71
}
74
72
75
- throw new RuntimeException ('Cannot detect composer autoload. Please run composer install ' );
73
+ return self ::getClassLoaderFromVendorDirectory (self ::UPSTREAM_COMPOSER_VENDOR_DIRECTORY )
74
+ ?? self ::getClassLoaderFromVendorDirectory (self ::LOCAL_COMPOSER_VENDOR_DIRECTORY );
76
75
}
77
76
78
77
/**
@@ -163,4 +162,20 @@ class_alias($alias, $class);
163
162
}
164
163
};
165
164
}
165
+
166
+ private static function getClassLoaderFromVendorDirectory (string $ composerVendorDirectory ): ?ClassLoader
167
+ {
168
+ $ filename = rtrim ($ composerVendorDirectory , '/ ' ) . '/autoload.php ' ;
169
+ if (!file_exists ($ filename )) {
170
+ return null ;
171
+ }
172
+
173
+ /** @psalm-suppress MixedAssignment */
174
+ $ loader = include $ filename ;
175
+ if (!$ loader instanceof ClassLoader) {
176
+ return null ;
177
+ }
178
+
179
+ return $ loader ;
180
+ }
166
181
}
0 commit comments