Repository navigation
Expand file tree
/
Copy pathautoload.php
More file actions
92 lines (77 loc) · 2.93 KB
/
Copy pathautoload.php
File metadata and controls
92 lines (77 loc) · 2.93 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?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;
use DirectoryIterator;
use Phar;
use RuntimeException;
use function class_exists;
use function dirname;
use function file_exists;
use function implode;
use function spl_autoload_register;
use function sprintf;
if (class_exists(__NAMESPACE__ . '\Autoload', false) === false) {
class Autoload
{
/**
* The composer autoloader(s).
*/
private static ?\Composer\Autoload\ClassLoader $composerAutoloader = null;
public static function load(string $class): void
{
if (self::$composerAutoloader === null) {
$autoloader = '/vendor/autoload.php';
$possibleAutoloaderPaths = [
// local dev repository
__DIR__ . $autoloader,
// dependency
dirname(__DIR__, 3) . $autoloader,
];
if (isset($GLOBALS['_composer_autoload_path'])) {
// @link https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary
$possibleAutoloaderPaths[] = $GLOBALS['_composer_autoload_path'];
}
// [!CAUTION]
// https://www.php.net/manual/en/phar.using.stream.php#104320
$baseDir = Phar::running() ?: __DIR__;
// checks to register optional autoloader
if (file_exists($baseDir . '/vendor-bin')) {
foreach (new DirectoryIterator($baseDir . '/vendor-bin') as $directory) {
if ($directory->isDot()) {
continue;
}
$autoloadFile = $directory->getPathname() . $autoloader;
if (file_exists($autoloadFile)) {
require $autoloadFile;
}
}
}
self::$composerAutoloader = require self::getAutoloadFile($possibleAutoloaderPaths);
}
self::$composerAutoloader->loadClass($class);
}
private static function getAutoloadFile(array $possibleAutoloaderPaths): string
{
foreach ($possibleAutoloaderPaths as $possibleAutoloaderPath) {
if (file_exists($possibleAutoloaderPath)) {
return $possibleAutoloaderPath;
}
}
throw new RuntimeException(
sprintf(
'Unable to find an autoloader in "%s" paths.',
implode('", "', $possibleAutoloaderPaths)
)
);
}
}
spl_autoload_register(__NAMESPACE__ . '\Autoload::load', true, true);
}