ProxyManager.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\Di\Aop;
  12. use Hyperf\Di\Annotation\AnnotationCollector;
  13. use Hyperf\Di\Annotation\AspectCollector;
  14. use Hyperf\Support\Filesystem\Filesystem;
  15. class ProxyManager
  16. {
  17. /**
  18. * The classes which be rewritten by proxy.
  19. */
  20. protected array $proxies = [];
  21. protected Filesystem $filesystem;
  22. /**
  23. * @param array $classMap the map to collect the classes with paths
  24. * @param string $proxyDir the directory which the proxy file places in
  25. */
  26. public function __construct(
  27. protected array $classMap = [],
  28. protected string $proxyDir = ''
  29. ) {
  30. $this->filesystem = new Filesystem();
  31. $this->proxies = $this->generateProxyFiles($this->initProxiesByReflectionClassMap(
  32. $this->classMap
  33. ));
  34. }
  35. public function getProxies(): array
  36. {
  37. return $this->proxies;
  38. }
  39. public function getProxyDir(): string
  40. {
  41. return $this->proxyDir;
  42. }
  43. public function getAspectClasses(): array
  44. {
  45. $aspectClasses = [];
  46. $classesAspects = AspectCollector::get('classes', []);
  47. foreach ($classesAspects as $aspect => $rules) {
  48. foreach ($rules as $rule) {
  49. if (isset($this->proxies[$rule])) {
  50. $aspectClasses[$aspect][$rule] = $this->proxies[$rule];
  51. }
  52. }
  53. }
  54. return $aspectClasses;
  55. }
  56. protected function generateProxyFiles(array $proxies = []): array
  57. {
  58. $proxyFiles = [];
  59. if (! $proxies) {
  60. return $proxyFiles;
  61. }
  62. if (! file_exists($this->getProxyDir())) {
  63. mkdir($this->getProxyDir(), 0755, true);
  64. }
  65. // WARNING: Ast class SHOULD NOT use static instance, because it will read the code from file, then would be caused coroutine switch.
  66. $ast = new Ast();
  67. foreach ($proxies as $className => $aspects) {
  68. $proxyFiles[$className] = $this->putProxyFile($ast, $className);
  69. }
  70. return $proxyFiles;
  71. }
  72. protected function putProxyFile(Ast $ast, $className)
  73. {
  74. $proxyFilePath = $this->getProxyFilePath($className);
  75. $modified = true;
  76. if (file_exists($proxyFilePath)) {
  77. $modified = $this->isModified($className, $proxyFilePath);
  78. }
  79. if ($modified) {
  80. $code = $ast->proxy($className);
  81. file_put_contents($proxyFilePath, $code);
  82. }
  83. return $proxyFilePath;
  84. }
  85. protected function isModified(string $className, ?string $proxyFilePath = null): bool
  86. {
  87. $proxyFilePath = $proxyFilePath ?? $this->getProxyFilePath($className);
  88. $time = $this->filesystem->lastModified($proxyFilePath);
  89. $origin = $this->classMap[$className];
  90. if ($time >= $this->filesystem->lastModified($origin)) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. protected function getProxyFilePath($className)
  96. {
  97. return $this->getProxyDir() . str_replace('\\', '_', $className) . '.proxy.php';
  98. }
  99. protected function isMatch(string $rule, string $target): bool
  100. {
  101. if (str_contains($rule, '::')) {
  102. [$rule] = explode('::', $rule);
  103. }
  104. if (! str_contains($rule, '*') && $rule === $target) {
  105. return true;
  106. }
  107. $preg = str_replace(['*', '\\'], ['.*', '\\\\'], $rule);
  108. $pattern = "/^{$preg}$/";
  109. if (preg_match($pattern, $target)) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. protected function initProxiesByReflectionClassMap(array $reflectionClassMap = []): array
  115. {
  116. // According to the data of AspectCollector to parse all the classes that need proxy.
  117. $proxies = [];
  118. if (! $reflectionClassMap) {
  119. return $proxies;
  120. }
  121. $classesAspects = AspectCollector::get('classes', []);
  122. foreach ($classesAspects as $aspect => $rules) {
  123. foreach ($rules as $rule) {
  124. foreach ($reflectionClassMap as $class => $path) {
  125. if (! $this->isMatch($rule, $class)) {
  126. continue;
  127. }
  128. $proxies[$class][] = $aspect;
  129. }
  130. }
  131. }
  132. foreach ($reflectionClassMap as $className => $path) {
  133. // Aggregate the class annotations
  134. $classAnnotations = $this->retrieveAnnotations($className . '._c');
  135. // Aggregate all methods annotations
  136. $methodAnnotations = $this->retrieveAnnotations($className . '._m');
  137. // Aggregate all properties annotations
  138. $propertyAnnotations = $this->retrieveAnnotations($className . '._p');
  139. $annotations = array_unique(array_merge($classAnnotations, $methodAnnotations, $propertyAnnotations));
  140. if ($annotations) {
  141. $annotationsAspects = AspectCollector::get('annotations', []);
  142. foreach ($annotationsAspects as $aspect => $rules) {
  143. foreach ($rules as $rule) {
  144. foreach ($annotations as $annotation) {
  145. if ($this->isMatch($rule, $annotation)) {
  146. $proxies[$className][] = $aspect;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. return $proxies;
  154. }
  155. protected function retrieveAnnotations(string $annotationCollectorKey): array
  156. {
  157. $defined = [];
  158. $annotations = AnnotationCollector::get($annotationCollectorKey, []);
  159. foreach ($annotations as $name => $annotation) {
  160. if (is_object($annotation)) {
  161. $defined[] = $name;
  162. } else {
  163. $defined = array_merge($defined, array_keys($annotation));
  164. }
  165. }
  166. return $defined;
  167. }
  168. }