-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
autoload.php
69 lines (59 loc) · 2.01 KB
/
autoload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
/*
* This file is part of the overtrue/phplint package
*
* (c) overtrue
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Overtrue\PHPLint;
if (class_exists(__NAMESPACE__ . '\Autoload', false) === false) {
class Autoload
{
/**
* The composer autoloader.
*
* @var \Composer\Autoload\ClassLoader
*/
private static $composerAutoloader = null;
public static function load(string $class): void
{
if (self::$composerAutoloader === null) {
self::$composerAutoloader = require self::getAutoloadFile();
}
self::$composerAutoloader->loadClass($class);
}
private static function getAutoloadFile(): string
{
if (isset($GLOBALS['_composer_autoload_path'])) {
$possibleAutoloadPaths = [
dirname($GLOBALS['_composer_autoload_path'])
];
$autoloader = basename($GLOBALS['_composer_autoload_path']);
} else {
$possibleAutoloadPaths = [
// local dev repository
__DIR__,
// dependency
dirname(__DIR__, 3),
];
$autoloader = 'vendor/autoload.php';
}
foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
if (file_exists($possibleAutoloadPath . DIRECTORY_SEPARATOR . $autoloader)) {
return $possibleAutoloadPath . DIRECTORY_SEPARATOR . $autoloader;
}
}
throw new \RuntimeException(
sprintf(
'Unable to find "%s" in "%s" paths.',
$autoloader,
implode('", "', $possibleAutoloadPaths)
)
);
}
}
spl_autoload_register(__NAMESPACE__ . '\Autoload::load', true, true);
}