Process.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Watcher;
  12. use Hyperf\Di\Annotation\AnnotationInterface;
  13. use Hyperf\Di\Annotation\AnnotationReader;
  14. use Hyperf\Di\Annotation\AspectCollector;
  15. use Hyperf\Di\Annotation\ScanConfig;
  16. use Hyperf\Di\Aop\Ast;
  17. use Hyperf\Di\Aop\ProxyManager;
  18. use Hyperf\Di\MetadataCollector;
  19. use Hyperf\Di\ReflectionManager;
  20. use Hyperf\Support\Composer;
  21. use Hyperf\Support\Filesystem\Filesystem;
  22. use Hyperf\Watcher\Ast\Metadata;
  23. use Hyperf\Watcher\Ast\RewriteClassNameVisitor;
  24. use PhpParser\NodeTraverser;
  25. use ReflectionClass;
  26. class Process
  27. {
  28. protected AnnotationReader $reader;
  29. protected ScanConfig $config;
  30. protected Filesystem $filesystem;
  31. protected Ast $ast;
  32. protected string $path = BASE_PATH . '/runtime/container/scan.cache';
  33. public function __construct(protected string $file)
  34. {
  35. $this->ast = new Ast();
  36. $this->config = $this->initScanConfig();
  37. $this->reader = new AnnotationReader($this->config->getIgnoreAnnotations());
  38. $this->filesystem = new Filesystem();
  39. }
  40. public function __invoke()
  41. {
  42. $meta = $this->getMetadata($this->file);
  43. if ($meta === null) {
  44. return;
  45. }
  46. $class = $meta->toClassName();
  47. $collectors = $this->config->getCollectors();
  48. [$data, $proxies, $aspectClasses] = file_exists($this->path) ? unserialize(file_get_contents($this->path)) : [[], [], []];
  49. foreach ($data as $collector => $deserialized) {
  50. /** @var MetadataCollector $collector */
  51. if (in_array($collector, $collectors)) {
  52. $collector::deserialize($deserialized);
  53. }
  54. }
  55. if (! empty($this->file)) {
  56. require $this->file;
  57. }
  58. // Collect the annotations.
  59. $ref = ReflectionManager::reflectClass($class);
  60. foreach ($collectors as $collector) {
  61. $collector::clear($class);
  62. }
  63. $this->collect($class, $ref);
  64. $collectors = $this->config->getCollectors();
  65. $data = [];
  66. /** @var MetadataCollector|string $collector */
  67. foreach ($collectors as $collector) {
  68. $data[$collector] = $collector::serialize();
  69. }
  70. $composerLoader = Composer::getLoader();
  71. $composerLoader->addClassMap($this->config->getClassMap());
  72. $this->deleteAspectClasses($aspectClasses, $proxies, $class);
  73. // Reload the proxy class.
  74. $manager = new ProxyManager(array_merge($composerLoader->getClassMap(), $proxies, [$class => $this->file]), BASE_PATH . '/runtime/container/proxy/');
  75. $proxies = $manager->getProxies();
  76. $aspectClasses = $manager->getAspectClasses();
  77. $this->putCache($this->path, serialize([$data, $proxies, $aspectClasses]));
  78. }
  79. public function collect($className, ReflectionClass $reflection)
  80. {
  81. // Parse class annotations
  82. $classAnnotations = $this->reader->getClassAnnotations($reflection);
  83. if (! empty($classAnnotations)) {
  84. foreach ($classAnnotations as $classAnnotation) {
  85. if ($classAnnotation instanceof AnnotationInterface) {
  86. $classAnnotation->collectClass($className);
  87. }
  88. }
  89. }
  90. // Parse properties annotations
  91. $properties = $reflection->getProperties();
  92. foreach ($properties as $property) {
  93. $propertyAnnotations = $this->reader->getPropertyAnnotations($property);
  94. if (! empty($propertyAnnotations)) {
  95. foreach ($propertyAnnotations as $propertyAnnotation) {
  96. if ($propertyAnnotation instanceof AnnotationInterface) {
  97. $propertyAnnotation->collectProperty($className, $property->getName());
  98. }
  99. }
  100. }
  101. }
  102. // Parse methods annotations
  103. $methods = $reflection->getMethods();
  104. foreach ($methods as $method) {
  105. $methodAnnotations = $this->reader->getMethodAnnotations($method);
  106. if (! empty($methodAnnotations)) {
  107. foreach ($methodAnnotations as $methodAnnotation) {
  108. if ($methodAnnotation instanceof AnnotationInterface) {
  109. $methodAnnotation->collectMethod($className, $method->getName());
  110. }
  111. }
  112. }
  113. }
  114. }
  115. protected function putCache($path, $data)
  116. {
  117. if (! $this->filesystem->isDirectory($dir = dirname($path))) {
  118. $this->filesystem->makeDirectory($dir, 0755, true);
  119. }
  120. $this->filesystem->put($path, $data);
  121. }
  122. protected function getMetadata(string $file): ?Metadata
  123. {
  124. $stmts = $this->ast->parse($this->filesystem->get($file));
  125. $meta = new Metadata();
  126. $meta->path = $file;
  127. $traverser = new NodeTraverser();
  128. $traverser->addVisitor(new RewriteClassNameVisitor($meta));
  129. $traverser->traverse($stmts);
  130. if (! $meta->isClass()) {
  131. return null;
  132. }
  133. return $meta;
  134. }
  135. protected function initScanConfig(): ScanConfig
  136. {
  137. return ScanConfig::instance(BASE_PATH . '/config/');
  138. }
  139. protected function deleteAspectClasses($aspectClasses, $proxies, $class): void
  140. {
  141. foreach ($aspectClasses as $aspect => $classes) {
  142. if ($aspect !== $class) {
  143. continue;
  144. }
  145. foreach ($classes as $path) {
  146. if (file_exists($path)) {
  147. unlink($path);
  148. }
  149. }
  150. }
  151. $classesAspects = AspectCollector::get('classes', []);
  152. foreach ($classesAspects as $aspect => $rules) {
  153. if ($aspect !== $class) {
  154. continue;
  155. }
  156. foreach ($rules as $rule) {
  157. if (isset($proxies[$rule]) && file_exists($proxies[$rule])) {
  158. unlink($proxies[$rule]);
  159. }
  160. }
  161. }
  162. }
  163. }