1111use function class_exists ;
1212use function explode ;
1313use function file_exists ;
14+ use function getenv ;
1415use function interface_exists ;
1516use function spl_autoload_register ;
1617use function strlen ;
2324 */
2425class Autoloader
2526{
27+ private const UPSTREAM_COMPOSER_VENDOR_DIRECTORY = __DIR__ . '/../../.. ' ;
28+ private const LOCAL_COMPOSER_VENDOR_DIRECTORY = __DIR__ . '/../vendor ' ;
29+
2630 /**
2731 * Attach autoloaders for managing legacy ZF artifacts.
2832 *
@@ -41,10 +45,15 @@ class Autoloader
4145 public static function load ()
4246 {
4347 $ loaded = new ArrayObject ([]);
48+ $ classLoader = self ::getClassLoader ();
49+
50+ if ($ classLoader === null ) {
51+ return ;
52+ }
4453
4554 spl_autoload_register (self ::createPrependAutoloader (
4655 RewriteRules::namespaceReverse (),
47- self :: getClassLoader () ,
56+ $ classLoader ,
4857 $ loaded
4958 ), true , true );
5059
@@ -54,25 +63,15 @@ public static function load()
5463 ));
5564 }
5665
57- /**
58- * @return ClassLoader
59- * @throws RuntimeException
60- */
61- private static function getClassLoader ()
66+ private static function getClassLoader (): ?ClassLoader
6267 {
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 );
7371 }
7472
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 );
7675 }
7776
7877 /**
@@ -163,4 +162,20 @@ class_alias($alias, $class);
163162 }
164163 };
165164 }
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+ }
166181}
0 commit comments