Repository navigation
Expand file tree
/
Copy pathScanner.php
More file actions
313 lines (266 loc) · 10.5 KB
/
Copy pathScanner.php
File metadata and controls
313 lines (266 loc) · 10.5 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di\Annotation;
use Hyperf\Config\ProviderConfig;
use Hyperf\Di\Aop\ProxyManager;
use Hyperf\Di\Exception\DirectoryNotExistException;
use Hyperf\Di\MetadataCollector;
use Hyperf\Di\ReflectionManager;
use Hyperf\Di\ScanHandler\ScanHandlerInterface;
use Hyperf\Support\Composer;
use Hyperf\Support\Filesystem\Filesystem;
use ReflectionClass;
class Scanner
{
protected Filesystem $filesystem;
protected string $path = BASE_PATH . '/runtime/container/scan.cache';
public function __construct(protected ScanConfig $scanConfig, protected ScanHandlerInterface $handler)
{
$this->filesystem = new Filesystem();
}
public function collect(AnnotationReader $reader, ReflectionClass $reflection): void
{
$className = $reflection->getName();
if ($path = $this->scanConfig->getClassMap()[$className] ?? null) {
if ($reflection->getFileName() !== $path) {
// When the original class is dynamically replaced, the original class should not be collected.
return;
}
}
// Parse class annotations
foreach ($reader->getAttributes($reflection) as $classAnnotation) {
if ($classAnnotation instanceof AnnotationInterface) {
$classAnnotation->collectClass($className);
}
}
// Parse properties annotations
foreach ($reflection->getProperties() as $property) {
foreach ($reader->getAttributes($property) as $propertyAnnotation) {
if ($propertyAnnotation instanceof AnnotationInterface) {
$propertyAnnotation->collectProperty($className, $property->getName());
}
}
}
// Parse methods annotations
foreach ($reflection->getMethods() as $method) {
foreach ($reader->getAttributes($method) as $methodAnnotation) {
if ($methodAnnotation instanceof AnnotationInterface) {
$methodAnnotation->collectMethod($className, $method->getName());
}
}
}
// Parse class constants annotations
foreach ($reflection->getReflectionConstants() as $classConstant) {
foreach ($reader->getAttributes($classConstant) as $constantAnnotation) {
if ($constantAnnotation instanceof AnnotationInterface) {
$constantAnnotation->collectClassConstant($className, $classConstant->getName());
}
}
}
unset($reflection);
}
public function scan(array $classMap = [], string $proxyDir = ''): array
{
$paths = $this->scanConfig->getPaths();
$collectors = $this->scanConfig->getCollectors();
if (! $paths) {
return [];
}
$lastCacheModified = file_exists($this->path) ? $this->filesystem->lastModified($this->path) : 0;
if ($lastCacheModified > 0 && $this->scanConfig->isCacheable()) {
return $this->deserializeCachedScanData($collectors);
}
$scanned = $this->handler->scan();
if ($scanned->isScanned()) {
return $this->deserializeCachedScanData($collectors);
}
$this->deserializeCachedScanData($collectors);
$annotationReader = new AnnotationReader($this->scanConfig->getIgnoreAnnotations());
$paths = $this->normalizeDir($paths);
$classes = ReflectionManager::getAllClasses($paths);
$this->clearRemovedClasses($collectors, $classes);
$reflectionClassMap = [];
foreach ($classes as $className => $reflectionClass) {
$reflectionClassMap[$className] = $reflectionClass->getFileName();
if ($this->filesystem->lastModified($reflectionClass->getFileName()) >= $lastCacheModified) {
/** @var MetadataCollector $collector */
foreach ($collectors as $collector) {
$collector::clear($className);
}
$this->collect($annotationReader, $reflectionClass);
}
}
$this->loadAspects($lastCacheModified);
$data = [];
/** @var MetadataCollector|string $collector */
foreach ($collectors as $collector) {
$data[$collector] = $collector::serialize();
}
// Get the class map of Composer loader
$classMap = array_merge($reflectionClassMap, $classMap);
$proxyManager = new ProxyManager($classMap, $proxyDir);
$proxies = $proxyManager->getProxies();
$aspectClasses = $proxyManager->getAspectClasses();
$this->putCache($this->path, serialize([$data, $proxies, $aspectClasses]));
exit;
}
/**
* Normalizes given directory names by removing directory not exist.
* @throws DirectoryNotExistException
*/
public function normalizeDir(array $paths): array
{
$result = [];
foreach ($paths as $path) {
if (is_dir($path)) {
$result[] = $path;
}
}
if ($paths && ! $result) {
throw new DirectoryNotExistException('The scanned directory does not exist');
}
return $result;
}
protected function deserializeCachedScanData(array $collectors): array
{
if (! file_exists($this->path)) {
return [];
}
[$data, $proxies] = unserialize(file_get_contents($this->path));
foreach ($data as $collector => $deserialized) {
/** @var MetadataCollector $collector */
if (in_array($collector, $collectors)) {
$collector::deserialize($deserialized);
}
}
return $proxies;
}
/**
* @param ReflectionClass[] $reflections
*/
protected function clearRemovedClasses(array $collectors, array $reflections): void
{
$path = BASE_PATH . '/runtime/container/classes.cache';
$classes = array_keys($reflections);
$data = [];
if ($this->filesystem->exists($path)) {
$data = unserialize($this->filesystem->get($path));
}
$this->putCache($path, serialize($classes));
$removed = array_diff($data, $classes);
foreach ($removed as $class) {
/** @var MetadataCollector $collector */
foreach ($collectors as $collector) {
$collector::clear($class);
}
}
}
protected function putCache(string $path, $data)
{
if (! $this->filesystem->isDirectory($dir = dirname($path))) {
$this->filesystem->makeDirectory($dir, 0755, true);
}
$this->filesystem->put($path, $data);
}
/**
* Load aspects to AspectCollector by configuration files and ConfigProvider.
*/
protected function loadAspects(int $lastCacheModified): void
{
$configDir = $this->scanConfig->getConfigDir();
if (! $configDir) {
return;
}
$aspectsPath = $configDir . '/autoload/aspects.php';
$basePath = $configDir . '/config.php';
$aspects = file_exists($aspectsPath) ? include $aspectsPath : [];
$baseConfig = file_exists($basePath) ? include $basePath : [];
$providerConfig = [];
if (class_exists(ProviderConfig::class)) {
$providerConfig = ProviderConfig::load();
}
if (! isset($aspects) || ! is_array($aspects)) {
$aspects = [];
}
if (! isset($baseConfig['aspects']) || ! is_array($baseConfig['aspects'])) {
$baseConfig['aspects'] = [];
}
if (! isset($providerConfig['aspects']) || ! is_array($providerConfig['aspects'])) {
$providerConfig['aspects'] = [];
}
$aspects = array_merge($providerConfig['aspects'], $baseConfig['aspects'], $aspects);
[$removed, $changed] = $this->getChangedAspects($aspects, $lastCacheModified);
// When the aspect removed from config, it should be removed from AspectCollector.
foreach ($removed as $aspect) {
AspectCollector::clear($aspect);
}
foreach ($aspects as $key => $value) {
if (is_numeric($key)) {
$aspect = $value;
$priority = null;
} else {
$aspect = $key;
$priority = (int) $value;
}
if (! in_array($aspect, $changed)) {
continue;
}
[$instanceClasses, $instanceAnnotations, $instancePriority] = AspectLoader::load($aspect);
$classes = $instanceClasses ?: [];
// Annotations
$annotations = $instanceAnnotations ?: [];
// Priority
$priority = $priority ?: ($instancePriority ?? null);
// Save the metadata to AspectCollector
AspectCollector::setAround($aspect, $classes, $annotations, $priority);
}
}
protected function getChangedAspects(array $aspects, int $lastCacheModified): array
{
$path = BASE_PATH . '/runtime/container/aspects.cache';
$classes = [];
foreach ($aspects as $key => $value) {
if (is_numeric($key)) {
$classes[] = $value;
} else {
$classes[] = $key;
}
}
$data = [];
if ($this->filesystem->exists($path)) {
$data = unserialize($this->filesystem->get($path));
}
$this->putCache($path, serialize($classes));
$diff = array_diff($data, $classes);
$changed = array_diff($classes, $data);
$removed = [];
foreach ($diff as $item) {
$annotation = AnnotationCollector::getClassAnnotation($item, Aspect::class);
if (is_null($annotation)) {
$removed[] = $item;
}
}
foreach ($classes as $class) {
$file = Composer::getLoader()->findFile($class);
if ($file === false) {
echo sprintf('Skip class %s, because it does not exist in composer class loader.', $class) . PHP_EOL;
continue;
}
if ($lastCacheModified <= $this->filesystem->lastModified($file)) {
$changed[] = $class;
}
}
return [
array_values(array_unique($removed)),
array_values(array_unique($changed)),
];
}
}