|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Matchish\ScoutElasticSearch\Searchable; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Laravel\Scout\Searchable; |
|
10
|
|
|
use PhpParser\Error; |
|
11
|
|
|
use PhpParser\Node; |
|
12
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
13
|
|
|
use PhpParser\NodeFinder; |
|
14
|
|
|
use PhpParser\NodeTraverser; |
|
15
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
|
16
|
|
|
use PhpParser\ParserFactory; |
|
17
|
|
|
use Roave\BetterReflection\BetterReflection; |
|
18
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
|
19
|
|
|
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound; |
|
20
|
|
|
use Symfony\Component\Finder\Finder; |
|
21
|
|
|
|
|
22
|
|
|
final class SearchableListFactory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $searchableClasses; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $namespace; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $appPath; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $errors = []; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var ClassReflector |
|
42
|
|
|
*/ |
|
43
|
|
|
private $classReflector; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $namespace |
|
47
|
|
|
*/ |
|
48
|
13 |
|
public function __construct(string $namespace, string $appPath) |
|
49
|
|
|
{ |
|
50
|
13 |
|
$this->namespace = $namespace; |
|
51
|
13 |
|
$this->appPath = $appPath; |
|
52
|
13 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getErrors(): array |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->errors; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return Collection |
|
64
|
|
|
*/ |
|
65
|
13 |
|
public function make(): Collection |
|
66
|
|
|
{ |
|
67
|
13 |
|
return new Collection($this->find()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get a list of searchable models. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string[] |
|
74
|
|
|
*/ |
|
75
|
13 |
|
private function find(): array |
|
76
|
|
|
{ |
|
77
|
13 |
|
$appNamespace = $this->namespace; |
|
78
|
|
|
|
|
79
|
|
|
return array_values(array_filter($this->getSearchableClasses(), function (string $class) use ($appNamespace) { |
|
80
|
13 |
|
return Str::startsWith($class, $appNamespace); |
|
81
|
13 |
|
})); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string[] |
|
86
|
|
|
*/ |
|
87
|
13 |
|
private function getSearchableClasses(): array |
|
88
|
|
|
{ |
|
89
|
13 |
|
if (self::$searchableClasses === null) { |
|
|
|
|
|
|
90
|
1 |
|
$projectClasses = $this->getProjectClasses(); |
|
91
|
|
|
|
|
92
|
|
|
self::$searchableClasses = $projectClasses->filter(function ($class) { |
|
93
|
1 |
|
return $this->findSearchableTraitRecursively($class); |
|
94
|
1 |
|
})->toArray(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
13 |
|
return self::$searchableClasses; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return Collection |
|
102
|
|
|
*/ |
|
103
|
1 |
|
private function getProjectClasses(): Collection |
|
104
|
|
|
{ |
|
105
|
1 |
|
$nodeFinder = new NodeFinder(); |
|
106
|
|
|
/** @var Class_[] $nodes */ |
|
107
|
|
|
$nodes = $nodeFinder->find($this->getStmts(), function (Node $node) { |
|
108
|
1 |
|
return $node instanceof Class_; |
|
109
|
1 |
|
}); |
|
110
|
|
|
|
|
111
|
|
|
return Collection::make($nodes)->map(function ($node) { |
|
112
|
1 |
|
return $node->namespacedName->toCodeString(); |
|
113
|
1 |
|
}); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
1 |
|
private function getStmts(): array |
|
120
|
|
|
{ |
|
121
|
1 |
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
|
122
|
1 |
|
$nameResolverVisitor = new NameResolver(); |
|
123
|
1 |
|
$nodeTraverser = new NodeTraverser(); |
|
124
|
1 |
|
$nodeTraverser->addVisitor($nameResolverVisitor); |
|
125
|
1 |
|
$stmts = []; |
|
126
|
1 |
|
$finder = Finder::create()->files()->name('*.php')->in($this->appPath); |
|
127
|
|
|
|
|
128
|
1 |
|
foreach ($finder as $file) { |
|
129
|
|
|
try { |
|
130
|
1 |
|
$stmts[] = $parser->parse($file->getContents()); |
|
131
|
|
|
} catch (Error $e) { |
|
132
|
|
|
$this->errors[] = $e->getMessage(); |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
$stmts = Collection::make($stmts)->flatten(1)->toArray(); |
|
138
|
1 |
|
$stmts = $nodeTraverser->traverse($stmts); |
|
139
|
|
|
|
|
140
|
1 |
|
return $stmts; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $class |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
1 |
|
private function findSearchableTraitRecursively(string $class): bool |
|
148
|
|
|
{ |
|
149
|
|
|
try { |
|
150
|
1 |
|
$reflection = $this->classReflector()->reflect($class); |
|
151
|
|
|
|
|
152
|
1 |
|
if (in_array(Searchable::class, $traits = $reflection->getTraitNames())) { |
|
153
|
1 |
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
foreach ($traits as $trait) { |
|
157
|
1 |
|
if ($this->findSearchableTraitRecursively($trait)) { |
|
158
|
1 |
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
if ($parent = $reflection->getParentClass()) { |
|
163
|
1 |
|
if ($this->findSearchableTraitRecursively($parent->getName())) { |
|
164
|
1 |
|
return true; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
return false; |
|
169
|
1 |
|
} catch (IdentifierNotFound $e) { |
|
170
|
1 |
|
$this->errors[] = $e->getMessage(); |
|
171
|
|
|
|
|
172
|
1 |
|
return false; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return ClassReflector |
|
178
|
|
|
*/ |
|
179
|
1 |
|
private function classReflector(): ClassReflector |
|
180
|
|
|
{ |
|
181
|
1 |
|
if (null === $this->classReflector) { |
|
182
|
1 |
|
$this->classReflector = (new BetterReflection())->classReflector(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
return $this->classReflector; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|