ScanConfig.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Annotation;
  12. use Hyperf\Config\ProviderConfig;
  13. use function Hyperf\Support\value;
  14. class ScanConfig
  15. {
  16. private static ?ScanConfig $instance = null;
  17. /**
  18. * @param array $paths the paths should be scanned everytime
  19. */
  20. public function __construct(
  21. private bool $cacheable,
  22. private string $configDir,
  23. private array $paths = [],
  24. private array $dependencies = [],
  25. private array $ignoreAnnotations = [],
  26. private array $globalImports = [],
  27. private array $collectors = [],
  28. private array $classMap = []
  29. ) {
  30. }
  31. public function isCacheable(): bool
  32. {
  33. return $this->cacheable;
  34. }
  35. public function getConfigDir(): string
  36. {
  37. return $this->configDir;
  38. }
  39. public function getPaths(): array
  40. {
  41. return $this->paths;
  42. }
  43. public function getCollectors(): array
  44. {
  45. return $this->collectors;
  46. }
  47. public function getIgnoreAnnotations(): array
  48. {
  49. return $this->ignoreAnnotations;
  50. }
  51. public function getGlobalImports(): array
  52. {
  53. return $this->globalImports;
  54. }
  55. public function getDependencies(): array
  56. {
  57. return $this->dependencies;
  58. }
  59. public function getClassMap(): array
  60. {
  61. return $this->classMap;
  62. }
  63. public static function instance(string $configDir): self
  64. {
  65. if (self::$instance) {
  66. return self::$instance;
  67. }
  68. $configDir = rtrim($configDir, '/');
  69. [$config, $serverDependencies, $cacheable] = static::initConfigByFile($configDir);
  70. return self::$instance = new self(
  71. $cacheable,
  72. $configDir,
  73. $config['paths'] ?? [],
  74. $serverDependencies ?? [],
  75. $config['ignore_annotations'] ?? [],
  76. $config['global_imports'] ?? [],
  77. $config['collectors'] ?? [],
  78. $config['class_map'] ?? []
  79. );
  80. }
  81. private static function initConfigByFile(string $configDir): array
  82. {
  83. $config = [];
  84. $configFromProviders = [];
  85. $cacheable = false;
  86. if (class_exists(ProviderConfig::class)) {
  87. $configFromProviders = ProviderConfig::load();
  88. }
  89. $serverDependencies = $configFromProviders['dependencies'] ?? [];
  90. if (file_exists($configDir . '/autoload/dependencies.php')) {
  91. $definitions = include $configDir . '/autoload/dependencies.php';
  92. $serverDependencies = array_replace($serverDependencies, $definitions ?? []);
  93. }
  94. $config = static::allocateConfigValue($configFromProviders['annotations'] ?? [], $config);
  95. // Load the config/autoload/annotations.php and merge the config
  96. if (file_exists($configDir . '/autoload/annotations.php')) {
  97. $annotations = include $configDir . '/autoload/annotations.php';
  98. $config = static::allocateConfigValue($annotations, $config);
  99. }
  100. // Load the config/config.php and merge the config
  101. if (file_exists($configDir . '/config.php')) {
  102. $configContent = include $configDir . '/config.php';
  103. $appEnv = $configContent['app_env'] ?? 'dev';
  104. $cacheable = value($configContent['scan_cacheable'] ?? $appEnv === 'prod');
  105. if (isset($configContent['annotations'])) {
  106. $config = static::allocateConfigValue($configContent['annotations'], $config);
  107. }
  108. }
  109. return [$config, $serverDependencies, $cacheable];
  110. }
  111. private static function allocateConfigValue(array $content, array $config): array
  112. {
  113. if (! isset($content['scan'])) {
  114. return $config;
  115. }
  116. foreach ($content['scan'] as $key => $value) {
  117. if (! isset($config[$key])) {
  118. $config[$key] = [];
  119. }
  120. if (! is_array($value)) {
  121. $value = [$value];
  122. }
  123. $config[$key] = array_merge($config[$key], $value);
  124. }
  125. return $config;
  126. }
  127. }