Repository navigation
Expand file tree
/
Copy pathProviderConfig.php
More file actions
85 lines (73 loc) · 2.4 KB
/
Copy pathProviderConfig.php
File metadata and controls
85 lines (73 loc) · 2.4 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
<?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\Config;
use Hyperf\Di\Definition\PriorityDefinition;
use Hyperf\Support\Composer;
use function class_exists;
use function is_string;
use function method_exists;
/**
* Provider config allow the components set the configs to application.
*/
class ProviderConfig
{
private static array $providerConfigs = [];
/**
* Load and merge all provider configs from components.
* Notice that this method will cached the config result into a static property,
* call ProviderConfig::clear() method if you want to reset the static property.
*/
public static function load(): array
{
if (! static::$providerConfigs) {
$providers = Composer::getMergedExtra('hyperf')['config'] ?? [];
static::$providerConfigs = static::loadProviders($providers);
}
return static::$providerConfigs;
}
public static function clear(): void
{
static::$providerConfigs = [];
}
protected static function loadProviders(array $providers): array
{
$providerConfigs = [];
foreach ($providers as $provider) {
if (is_string($provider) && class_exists($provider) && method_exists($provider, '__invoke')) {
$providerConfigs[] = (new $provider())();
}
}
return static::merge(...$providerConfigs);
}
protected static function merge(...$arrays): array
{
if (empty($arrays)) {
return [];
}
$result = array_merge_recursive(...$arrays);
if (isset($result['dependencies'])) {
$result['dependencies'] = [];
foreach ($arrays as $item) {
foreach ($item['dependencies'] ?? [] as $key => $value) {
$depend = $result['dependencies'][$key] ?? null;
if (! $depend instanceof PriorityDefinition) {
$result['dependencies'][$key] = $value;
continue;
}
if ($value instanceof PriorityDefinition) {
$depend->merge($value);
}
}
}
}
return $result;
}
}