-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
68 lines (56 loc) · 2.12 KB
/
Copy pathbootstrap.php
File metadata and controls
68 lines (56 loc) · 2.12 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
<?php
declare(strict_types=1);
$projectRoot = __DIR__;
$vendorAutoloadPath = $projectRoot . '/vendor/autoload.php';
if (is_file($vendorAutoloadPath)) {
require_once $vendorAutoloadPath;
}
spl_autoload_register(static function (string $className) use ($projectRoot): void {
static $prefixes = null;
if ($prefixes === null) {
$prefixes = [];
$composerJsonPath = $projectRoot . '/composer.json';
if (is_file($composerJsonPath)) {
$composerConfig = json_decode((string) file_get_contents($composerJsonPath), true);
$psr4Mappings = is_array($composerConfig) ? ($composerConfig['autoload']['psr-4'] ?? []) : [];
if (is_array($psr4Mappings)) {
foreach ($psr4Mappings as $prefix => $paths) {
$resolvedPaths = [];
foreach ((array) $paths as $path) {
if (!is_string($path) || $path === '') {
continue;
}
$resolvedPath = $projectRoot . '/' . ltrim($path, '/');
if (is_dir($resolvedPath) || is_file($resolvedPath)) {
$resolvedPaths[] = rtrim($resolvedPath, '/');
}
}
if ($resolvedPaths !== []) {
$prefixes[(string) $prefix] = $resolvedPaths;
}
}
}
}
}
foreach ($prefixes as $prefix => $paths) {
if (!str_starts_with($className, $prefix)) {
continue;
}
$relativeClass = substr($className, strlen($prefix));
if (!is_string($relativeClass)) {
continue;
}
$relativeFile = str_replace('\\', '/', $relativeClass) . '.php';
foreach ($paths as $path) {
$candidate = $path . '/' . $relativeFile;
if (is_file($candidate)) {
require_once $candidate;
return;
}
}
}
});
if (!defined('LENGA_PROJECT_ROOT')) {
define('LENGA_PROJECT_ROOT', $projectRoot);
}
echo "Lenga PHP bootstrap OK\n";