Skip to content

Commit ae75f04

Browse files
authored
ENGCOM-7830: improve and make more strict autoload.php #28923
2 parents fd48b47 + ced4144 commit ae75f04

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

app/autoload.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,48 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
declare(strict_types=1);
9+
810
use Magento\Framework\Autoload\AutoloaderRegistry;
911
use Magento\Framework\Autoload\ClassLoaderWrapper;
1012

1113
/**
1214
* Shortcut constant for the root directory
1315
*/
14-
define('BP', dirname(__DIR__));
16+
\define('BP', \dirname(__DIR__));
1517

16-
define('VENDOR_PATH', BP . '/app/etc/vendor_path.php');
18+
\define('VENDOR_PATH', BP . '/app/etc/vendor_path.php');
1719

18-
if (!file_exists(VENDOR_PATH)) {
20+
if (!\is_readable(VENDOR_PATH)) {
1921
throw new \Exception(
2022
'We can\'t read some files that are required to run the Magento application. '
2123
. 'This usually means file permissions are set incorrectly.'
2224
);
2325
}
2426

25-
$vendorDir = require VENDOR_PATH;
26-
$vendorAutoload = BP . "/{$vendorDir}/autoload.php";
27+
$vendorAutoload = (
28+
static function (): ?string {
29+
$vendorDir = require VENDOR_PATH;
30+
31+
$vendorAutoload = BP . "/{$vendorDir}/autoload.php";
32+
if (\is_readable($vendorAutoload)) {
33+
return $vendorAutoload;
34+
}
35+
36+
$vendorAutoload = "{$vendorDir}/autoload.php";
37+
if (\is_readable($vendorAutoload)) {
38+
return $vendorAutoload;
39+
}
40+
41+
return null;
42+
}
43+
)();
2744

28-
/* 'composer install' validation */
29-
if (file_exists($vendorAutoload)) {
30-
$composerAutoloader = include $vendorAutoload;
31-
} else if (file_exists("{$vendorDir}/autoload.php")) {
32-
$vendorAutoload = "{$vendorDir}/autoload.php";
33-
$composerAutoloader = include $vendorAutoload;
34-
} else {
45+
if ($vendorAutoload === null) {
3546
throw new \Exception(
3647
'Vendor autoload is not found. Please run \'composer install\' under application root directory.'
3748
);
3849
}
3950

51+
$composerAutoloader = include $vendorAutoload;
4052
AutoloaderRegistry::registerAutoloader(new ClassLoaderWrapper($composerAutoloader));

0 commit comments

Comments
 (0)