Process.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\AnnotationReader;
  13. use Hyperf\Di\Annotation\AspectCollector;
  14. use Hyperf\Di\Annotation\ScanConfig;
  15. use Hyperf\Di\Annotation\Scanner;
  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\Di\ScanHandler\NullScanHandler;
  21. use Hyperf\Support\Composer;
  22. use Hyperf\Support\Filesystem\Filesystem;
  23. use Hyperf\Watcher\Ast\Metadata;
  24. use Hyperf\Watcher\Ast\RewriteClassNameVisitor;
  25. use PhpParser\NodeTraverser;
  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. $scanner = new Scanner($this->config, new NullScanHandler());
  64. $scanner->collect($this->reader, $ref);
  65. $collectors = $this->config->getCollectors();
  66. $data = [];
  67. /** @var MetadataCollector|string $collector */
  68. foreach ($collectors as $collector) {
  69. $data[$collector] = $collector::serialize();
  70. }
  71. $composerLoader = Composer::getLoader();
  72. $composerLoader->addClassMap($this->config->getClassMap());
  73. $this->deleteAspectClasses($aspectClasses, $proxies, $class);
  74. // Reload the proxy class.
  75. $manager = new ProxyManager(array_merge($composerLoader->getClassMap(), $proxies, [$class => $this->file]), BASE_PATH . '/runtime/container/proxy/');
  76. $proxies = $manager->getProxies();
  77. $aspectClasses = $manager->getAspectClasses();
  78. $this->putCache($this->path, serialize([$data, $proxies, $aspectClasses]));
  79. }
  80. protected function putCache($path, $data)
  81. {
  82. if (! $this->filesystem->isDirectory($dir = dirname($path))) {
  83. $this->filesystem->makeDirectory($dir, 0755, true);
  84. }
  85. $this->filesystem->put($path, $data);
  86. }
  87. protected function getMetadata(string $file): ?Metadata
  88. {
  89. $stmts = $this->ast->parse($this->filesystem->get($file));
  90. $meta = new Metadata();
  91. $meta->path = $file;
  92. $traverser = new NodeTraverser();
  93. $traverser->addVisitor(new RewriteClassNameVisitor($meta));
  94. $traverser->traverse($stmts);
  95. if (! $meta->isClass()) {
  96. return null;
  97. }
  98. return $meta;
  99. }
  100. protected function initScanConfig(): ScanConfig
  101. {
  102. return ScanConfig::instance(BASE_PATH . '/config/');
  103. }
  104. protected function deleteAspectClasses($aspectClasses, $proxies, $class): void
  105. {
  106. foreach ($aspectClasses as $aspect => $classes) {
  107. if ($aspect !== $class) {
  108. continue;
  109. }
  110. foreach ($classes as $path) {
  111. if (file_exists($path)) {
  112. unlink($path);
  113. }
  114. }
  115. }
  116. $classesAspects = AspectCollector::get('classes', []);
  117. foreach ($classesAspects as $aspect => $rules) {
  118. if ($aspect !== $class) {
  119. continue;
  120. }
  121. foreach ($rules as $rule) {
  122. if (isset($proxies[$rule]) && file_exists($proxies[$rule])) {
  123. unlink($proxies[$rule]);
  124. }
  125. }
  126. }
  127. }
  128. }