Repository navigation
Expand file tree
/
Copy pathComponentScanner.php
More file actions
176 lines (138 loc) · 4.68 KB
/
Copy pathComponentScanner.php
File metadata and controls
176 lines (138 loc) · 4.68 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace NyonCode\WireBoost\Support;
use Livewire\Component as LivewireComponent;
use NyonCode\WireForms\Forms\WithForms;
use NyonCode\WireTable\Concerns\WithTable;
use ReflectionClass;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
/**
* Scans the application for Livewire components that build wireStack tables,
* forms or infolists, or that extend a wire widget. Each match is classified by
* the wire capability it exposes so an agent can locate the relevant component.
*/
class ComponentScanner
{
/**
* Discover wire-powered components beneath the configured scan paths.
*
* @param array<int, string>|null $paths
* @return array<int, array{class: class-string, file: string, kinds: array<int, string>}>
*/
public function scan(?array $paths = null): array
{
$paths = array_values(array_filter(
$paths ?? (array) config('wire-boost.scan.paths', []),
static fn (string $path): bool => is_dir($path),
));
if ($paths === []) {
return [];
}
$components = [];
foreach (Finder::create()->files()->in($paths)->name('*.php') as $file) {
$class = $this->classFromFile($file);
if ($class === null || ! class_exists($class)) {
continue;
}
$reflection = new ReflectionClass($class);
if ($reflection->isAbstract() || ! $reflection->isSubclassOf(LivewireComponent::class)) {
continue;
}
$kinds = $this->kinds($reflection);
if ($kinds === []) {
continue;
}
$components[] = [
'class' => $class,
'file' => $file->getRealPath() ?: $file->getPathname(),
'kinds' => $kinds,
];
}
usort($components, static fn (array $a, array $b): int => strcmp($a['class'], $b['class']));
return $components;
}
/**
* @param ReflectionClass<LivewireComponent> $reflection
* @return array<int, string>
*/
private function kinds(ReflectionClass $reflection): array
{
$traits = $this->allTraits($reflection);
$kinds = [];
if (in_array(WithTable::class, $traits, true) || $reflection->hasMethod('table')) {
$kinds[] = 'table';
}
if (in_array(WithForms::class, $traits, true) || $reflection->hasMethod('form')) {
$kinds[] = 'form';
}
if ($reflection->hasMethod('infolist')) {
$kinds[] = 'infolist';
}
return $kinds;
}
/**
* @param ReflectionClass<LivewireComponent> $reflection
* @return array<int, string>
*/
private function allTraits(ReflectionClass $reflection): array
{
$traits = [];
for ($class = $reflection; $class !== false; $class = $class->getParentClass()) {
$traits = array_merge($traits, array_keys($class->getTraits()));
}
return $traits;
}
/**
* Resolve the fully-qualified class name declared in a PHP file.
*
* @return class-string|null
*/
private function classFromFile(SplFileInfo $file): ?string
{
$contents = (string) file_get_contents($file->getPathname());
$namespace = '';
$class = null;
$tokens = token_get_all($contents);
foreach ($tokens as $index => $token) {
if (! is_array($token)) {
continue;
}
if ($token[0] === T_NAMESPACE) {
$namespace = $this->readName($tokens, $index + 1);
}
if ($token[0] === T_CLASS) {
$next = $tokens[$index + 2] ?? null;
if (is_array($next)) {
$class = $next[1];
break;
}
}
}
if ($class === null) {
return null;
}
/** @var class-string $fqcn */
$fqcn = $namespace !== '' ? $namespace.'\\'.$class : $class;
return $fqcn;
}
/**
* @param array<int, array{0: int, 1: string}|string> $tokens
*/
private function readName(array $tokens, int $start): string
{
$name = '';
for ($i = $start, $count = count($tokens); $i < $count; $i++) {
$token = $tokens[$i];
if (is_array($token) && in_array($token[0], [T_STRING, T_NAME_QUALIFIED, T_NS_SEPARATOR], true)) {
$name .= $token[1];
continue;
}
if (is_array($token) && $token[0] === T_WHITESPACE) {
continue;
}
break;
}
return trim($name, '\\');
}
}