-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoload.php
75 lines (67 loc) · 2.26 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
70
71
72
73
74
75
<?php declare(strict_types=1);
/**
* This file is part of the BoxManifest package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Laurent Laville
* @since Release 4.0.0
*/
namespace Bartlett\BoxManifest;
use RuntimeException;
use function basename;
use function dirname;
use function file_exists;
use function implode;
use function spl_autoload_register;
use function sprintf;
use const DIRECTORY_SEPARATOR;
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);
}