|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypervel\Container; |
| 6 | + |
| 7 | +use Hyperf\Di\Annotation\ScanConfig as HyperfScanConfig; |
| 8 | +use Hypervel\Config\ProviderConfig; |
| 9 | + |
| 10 | +class ScanConfig extends HyperfScanConfig |
| 11 | +{ |
| 12 | + protected static function initConfigByFile(string $configDir): array |
| 13 | + { |
| 14 | + $config = []; |
| 15 | + $configFromProviders = []; |
| 16 | + $cacheable = false; |
| 17 | + if (class_exists(ProviderConfig::class)) { |
| 18 | + $configFromProviders = ProviderConfig::load(); |
| 19 | + } |
| 20 | + |
| 21 | + $serverDependencies = $configFromProviders['dependencies'] ?? []; |
| 22 | + if (file_exists($dependenciesFile = "{$configDir}/dependencies.php")) { |
| 23 | + $definitions = include $dependenciesFile; |
| 24 | + $serverDependencies = array_replace($serverDependencies, $definitions ?? []); |
| 25 | + } |
| 26 | + |
| 27 | + $config = static::allocateConfigValue($configFromProviders['annotations'] ?? [], $config); |
| 28 | + |
| 29 | + // Load the config/annotations.php and merge the config |
| 30 | + if (file_exists($annotationsFile = "{$configDir}/annotations.php")) { |
| 31 | + $annotations = include $annotationsFile; |
| 32 | + $config = static::allocateConfigValue($annotations, $config); |
| 33 | + } |
| 34 | + |
| 35 | + // Load the config/app.php and merge the config |
| 36 | + if (file_exists($appFile = "{$configDir}/app.php")) { |
| 37 | + $configContent = include $appFile; |
| 38 | + $environment = $configContent['env'] ?? 'dev'; |
| 39 | + $cacheable = value($configContent['scan_cacheable'] ?? $environment === 'production'); |
| 40 | + if (isset($configContent['annotations'])) { |
| 41 | + $config = static::allocateConfigValue($configContent['annotations'], $config); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return [$config, $serverDependencies, $cacheable]; |
| 46 | + } |
| 47 | + |
| 48 | + protected static function allocateConfigValue(array $content, array $config): array |
| 49 | + { |
| 50 | + if (! isset($content['scan'])) { |
| 51 | + return $config; |
| 52 | + } |
| 53 | + |
| 54 | + foreach ($content['scan'] as $key => $value) { |
| 55 | + if (! isset($config[$key])) { |
| 56 | + $config[$key] = []; |
| 57 | + } |
| 58 | + if (! is_array($value)) { |
| 59 | + $value = [$value]; |
| 60 | + } |
| 61 | + $config[$key] = array_merge($config[$key], $value); |
| 62 | + } |
| 63 | + |
| 64 | + return $config; |
| 65 | + } |
| 66 | +} |
0 commit comments