| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Spatie\LaravelIgnition\Support\Composer;
|
|
|
4 |
|
|
|
5 |
use function app_path;
|
|
|
6 |
use function base_path;
|
|
|
7 |
use Illuminate\Support\Str;
|
|
|
8 |
use Symfony\Component\Finder\Finder;
|
|
|
9 |
use Symfony\Component\Finder\SplFileInfo;
|
|
|
10 |
|
|
|
11 |
class ComposerClassMap
|
|
|
12 |
{
|
|
|
13 |
/** @var \Spatie\LaravelIgnition\Support\Composer\Composer */
|
|
|
14 |
protected object $composer;
|
|
|
15 |
|
|
|
16 |
protected string $basePath;
|
|
|
17 |
|
|
|
18 |
public function __construct(?string $autoloaderPath = null)
|
|
|
19 |
{
|
|
|
20 |
$autoloaderPath = $autoloaderPath ?? base_path('/vendor/autoload.php');
|
|
|
21 |
|
|
|
22 |
$this->composer = file_exists($autoloaderPath)
|
|
|
23 |
? require $autoloaderPath
|
|
|
24 |
: new FakeComposer();
|
|
|
25 |
|
|
|
26 |
$this->basePath = app_path();
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
/** @return array<string, string> */
|
|
|
30 |
public function listClasses(): array
|
|
|
31 |
{
|
|
|
32 |
$classes = $this->composer->getClassMap();
|
|
|
33 |
|
|
|
34 |
return array_merge($classes, $this->listClassesInPsrMaps());
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function searchClassMap(string $missingClass): ?string
|
|
|
38 |
{
|
|
|
39 |
foreach ($this->composer->getClassMap() as $fqcn => $file) {
|
|
|
40 |
$basename = basename($file, '.php');
|
|
|
41 |
|
|
|
42 |
if ($basename === $missingClass) {
|
|
|
43 |
return $fqcn;
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
return null;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/** @return array<string, mixed> */
|
|
|
51 |
public function listClassesInPsrMaps(): array
|
|
|
52 |
{
|
|
|
53 |
// TODO: This is incorrect. Doesnt list all fqcns. Need to parse namespace? e.g. App\LoginController is wrong
|
|
|
54 |
|
|
|
55 |
$prefixes = array_merge(
|
|
|
56 |
$this->composer->getPrefixes(),
|
|
|
57 |
$this->composer->getPrefixesPsr4()
|
|
|
58 |
);
|
|
|
59 |
|
|
|
60 |
$classes = [];
|
|
|
61 |
|
|
|
62 |
foreach ($prefixes as $namespace => $directories) {
|
|
|
63 |
foreach ($directories as $directory) {
|
|
|
64 |
if (file_exists($directory)) {
|
|
|
65 |
$files = (new Finder)
|
|
|
66 |
->in($directory)
|
|
|
67 |
->files()
|
|
|
68 |
->name('*.php');
|
|
|
69 |
|
|
|
70 |
foreach ($files as $file) {
|
|
|
71 |
if ($file instanceof SplFileInfo) {
|
|
|
72 |
$fqcn = $this->getFullyQualifiedClassNameFromFile($namespace, $file);
|
|
|
73 |
|
|
|
74 |
$classes[$fqcn] = $file->getRelativePathname();
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
return $classes;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function searchPsrMaps(string $missingClass): ?string
|
|
|
85 |
{
|
|
|
86 |
$prefixes = array_merge(
|
|
|
87 |
$this->composer->getPrefixes(),
|
|
|
88 |
$this->composer->getPrefixesPsr4()
|
|
|
89 |
);
|
|
|
90 |
|
|
|
91 |
foreach ($prefixes as $namespace => $directories) {
|
|
|
92 |
foreach ($directories as $directory) {
|
|
|
93 |
if (file_exists($directory)) {
|
|
|
94 |
$files = (new Finder)
|
|
|
95 |
->in($directory)
|
|
|
96 |
->files()
|
|
|
97 |
->name('*.php');
|
|
|
98 |
|
|
|
99 |
foreach ($files as $file) {
|
|
|
100 |
if ($file instanceof SplFileInfo) {
|
|
|
101 |
$basename = basename($file->getRelativePathname(), '.php');
|
|
|
102 |
|
|
|
103 |
if ($basename === $missingClass) {
|
|
|
104 |
return $namespace . basename($file->getRelativePathname(), '.php');
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
return null;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
protected function getFullyQualifiedClassNameFromFile(string $rootNamespace, SplFileInfo $file): string
|
|
|
116 |
{
|
|
|
117 |
$class = trim(str_replace($this->basePath, '', (string)$file->getRealPath()), DIRECTORY_SEPARATOR);
|
|
|
118 |
|
|
|
119 |
$class = str_replace(
|
|
|
120 |
[DIRECTORY_SEPARATOR, 'App\\'],
|
|
|
121 |
['\\', app()->getNamespace()],
|
|
|
122 |
ucfirst(Str::replaceLast('.php', '', $class))
|
|
|
123 |
);
|
|
|
124 |
|
|
|
125 |
return $rootNamespace . $class;
|
|
|
126 |
}
|
|
|
127 |
}
|