-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathClassListProvider.php
83 lines (68 loc) · 2.62 KB
/
ClassListProvider.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
76
77
78
79
80
81
82
83
<?php
namespace Imanghafoori\LaravelMicroscope;
use ImanGhafoori\ComposerJson\ComposerJson;
use Imanghafoori\LaravelMicroscope\FileReaders\PhpFinder;
use Imanghafoori\TokenAnalyzer\Str;
class ClassListProvider
{
/**
* @var array<string, array>
*/
public static $allNamespaces = [];
public static function get()
{
if (self::$allNamespaces) {
return self::$allNamespaces;
}
foreach (self::getCandidateSearchPaths() as $baseComposerPath => $psr4) {
foreach ($psr4 as $folder => $psr4Mappings) {
foreach ((array) $psr4Mappings as $namespace => $_psr4Paths) {
foreach ((array) $_psr4Paths as $psr4Path) {
self::calculate($psr4Path, $baseComposerPath, $namespace);
}
}
}
}
return self::$allNamespaces;
}
private static function calculate($psr4Path, $baseComposerPath, $namespace): void
{
foreach (PhpFinder::getAllPhpFiles($psr4Path, $baseComposerPath) as $classFilePath) {
$fileName = $classFilePath->getFilename();
if (substr_count($fileName, '.') > 1) {
continue;
}
$relativePath = str_replace($baseComposerPath ?: base_path(), '', $classFilePath->getRealPath());
[$classBaseName, $fullClassPath] = self::derive($psr4Path, $relativePath, $namespace, $fileName);
self::$allNamespaces[$classBaseName][] = $fullClassPath;
}
}
private static function getCandidateSearchPaths()
{
$sp = DIRECTORY_SEPARATOR;
$path1 = base_path();
$path2 = base_path('vendor'.$sp.'laravel'.$sp.'framework');
return [
$path1 => Analyzers\ComposerJson::make()->readAutoload(),
$path2 => ComposerJson::make($path2)->readAutoload(),
];
}
public static function derive($psr4Path, $relativePath, $namespace, $fileName): array
{
$composerPath = str_replace('/', '\\', $psr4Path);
$relativePath = str_replace('/', '\\', $relativePath);
/**
* // replace composer base_path with composer namespace
* "psr-4": {
* "App\\": "app/"
* }.
*/
// calculate namespace
$ns = Str::replaceFirst(trim($composerPath, '\\'), trim($namespace, '\\/'), $relativePath);
$t = str_replace('.php', '', [$ns, $fileName]);
$t = str_replace('/', '\\', $t); // for linux environments.
$classBaseName = $t[1];
$fullClassPath = $t[0];
return [$classBaseName, trim($fullClassPath, '\\')];
}
}