Repository navigation
Expand file tree
/
Copy pathScanConfig.php
More file actions
152 lines (127 loc) · 4.21 KB
/
Copy pathScanConfig.php
File metadata and controls
152 lines (127 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di\Annotation;
use Hyperf\Config\ProviderConfig;
use function Hyperf\Support\value;
class ScanConfig
{
private static ?ScanConfig $instance = null;
/**
* @param array $paths the paths should be scanned everytime
*/
public function __construct(
private bool $cacheable,
private string $configDir,
private array $paths = [],
private array $dependencies = [],
private array $ignoreAnnotations = [],
private array $globalImports = [],
private array $collectors = [],
private array $classMap = []
) {
}
public function isCacheable(): bool
{
return $this->cacheable;
}
public function getConfigDir(): string
{
return $this->configDir;
}
public function getPaths(): array
{
return $this->paths;
}
public function getCollectors(): array
{
return $this->collectors;
}
public function getIgnoreAnnotations(): array
{
return $this->ignoreAnnotations;
}
public function getGlobalImports(): array
{
return $this->globalImports;
}
public function getDependencies(): array
{
return $this->dependencies;
}
public function getClassMap(): array
{
return $this->classMap;
}
public static function instance(string $configDir): self
{
if (self::$instance) {
return self::$instance;
}
$configDir = rtrim($configDir, '/');
[$config, $serverDependencies, $cacheable] = static::initConfigByFile($configDir);
return self::$instance = new self(
$cacheable,
$configDir,
$config['paths'] ?? [],
$serverDependencies ?? [],
$config['ignore_annotations'] ?? [],
$config['global_imports'] ?? [],
$config['collectors'] ?? [],
$config['class_map'] ?? []
);
}
private static function initConfigByFile(string $configDir): array
{
$config = [];
$configFromProviders = [];
$cacheable = false;
if (class_exists(ProviderConfig::class)) {
$configFromProviders = ProviderConfig::load();
}
$serverDependencies = $configFromProviders['dependencies'] ?? [];
if (file_exists($configDir . '/autoload/dependencies.php')) {
$definitions = include $configDir . '/autoload/dependencies.php';
$serverDependencies = array_replace($serverDependencies, $definitions ?? []);
}
$config = static::allocateConfigValue($configFromProviders['annotations'] ?? [], $config);
// Load the config/autoload/annotations.php and merge the config
if (file_exists($configDir . '/autoload/annotations.php')) {
$annotations = include $configDir . '/autoload/annotations.php';
$config = static::allocateConfigValue($annotations, $config);
}
// Load the config/config.php and merge the config
if (file_exists($configDir . '/config.php')) {
$configContent = include $configDir . '/config.php';
$appEnv = $configContent['app_env'] ?? 'dev';
$cacheable = value($configContent['scan_cacheable'] ?? $appEnv === 'prod');
if (isset($configContent['annotations'])) {
$config = static::allocateConfigValue($configContent['annotations'], $config);
}
}
return [$config, $serverDependencies, $cacheable];
}
private static function allocateConfigValue(array $content, array $config): array
{
if (! isset($content['scan'])) {
return $config;
}
foreach ($content['scan'] as $key => $value) {
if (! isset($config[$key])) {
$config[$key] = [];
}
if (! is_array($value)) {
$value = [$value];
}
$config[$key] = array_merge($config[$key], $value);
}
return $config;
}
}